-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support exposing generator functions
- Loading branch information
Showing
7 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import DebugLogger from "debug" | ||
import { createProxyFunction } from "../common/call-proxy" | ||
import { Callback, RemoteCallback } from "../common/callbacks" | ||
import { MessageRelay } from "../types/common" | ||
import { SerializedIterator, Serializer } from "../types/serializers" | ||
|
||
const debug = DebugLogger("threads:callback:messages") | ||
|
||
export const DefaultIteratorSerializer = (rootSerializer: Serializer): Serializer<SerializedIterator, Iterator<any> | AsyncIterator<any>, AsyncIterator<any>> => ({ | ||
deserialize(message: SerializedIterator, origin: MessageRelay): AsyncIterator<any> & AsyncIterable<any> { | ||
const remoteNext = createProxyFunction<[], IteratorResult<any>>(origin, rootSerializer, message.next_fid, debug) | ||
const remoteCallback = RemoteCallback<() => Promise<IteratorResult<any>>>(remoteNext) | ||
|
||
const next = async () => { | ||
const result = await remoteCallback() | ||
if (result.done) { | ||
remoteCallback.release() | ||
} | ||
return result | ||
} | ||
|
||
const asyncIterator = { | ||
[Symbol.asyncIterator]: () => asyncIterator, | ||
next | ||
} | ||
return asyncIterator | ||
}, | ||
serialize(iter: Iterator<any> | AsyncIterator<any>): SerializedIterator { | ||
const next = Callback(async () => { | ||
const result = await iter.next() | ||
if (result.done) { | ||
next.release() | ||
} | ||
return result | ||
}) | ||
return { | ||
__iterator_marker: "$$iterator", | ||
next_fid: next.id | ||
} | ||
} | ||
}) | ||
|
||
export const isIterator = (thing: any): thing is Iterator<any> | AsyncIterator<any> => | ||
thing && typeof thing === "object" && "next" in thing && typeof thing.next === "function" | ||
|
||
export const isSerializedIterator = (thing: any): thing is SerializedIterator => | ||
thing && typeof thing === "object" && "__iterator_marker" in thing && thing.__iterator_marker === "$$iterator" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import test from "ava" | ||
import { spawn, Callback, Thread, Worker } from "../src/index" | ||
import { AsyncGenerator } from "./workers/async-generator" | ||
import { Generator } from "./workers/generator" | ||
|
||
test("can use a generator function exposed by a worker", async t => { | ||
const generate = await spawn<Generator>(new Worker("./workers/generator")) | ||
|
||
try { | ||
const results: number[] = [] | ||
|
||
for await (const i of await generate(3)) { | ||
results.push(i) | ||
} | ||
|
||
t.deepEqual(results, [1, 2, 3]) | ||
} finally { | ||
await Thread.terminate(generate) | ||
} | ||
}) | ||
|
||
test("can use an async generator function exposed by a worker", async t => { | ||
const generate = await spawn<AsyncGenerator>(new Worker("./workers/async-generator")) | ||
|
||
try { | ||
const results: number[] = [] | ||
|
||
for await (const i of await generate(3)) { | ||
results.push(i) | ||
} | ||
|
||
t.deepEqual(results, [1, 2, 3]) | ||
} finally { | ||
await Thread.terminate(generate) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expose } from "../../src/worker" | ||
|
||
export type AsyncGenerator = (count: number) => AsyncIterator<number> | ||
|
||
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) | ||
|
||
expose(async function* generator(count: number) { | ||
for (let i = 1; i <= count; i++) { | ||
await delay(2) | ||
yield i | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { expose } from "../../src/worker" | ||
|
||
export type Generator = (count: number) => Iterator<number> | ||
|
||
expose(function *generator(count: number) { | ||
for (let i = 1; i <= count; i++) { | ||
yield i | ||
} | ||
}) |