forked from developit/greenlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Generators and Async Generators support
fixes developit#35 tldr; Mainly wrapped the existing promise api to get it to work with async generators. I took atleast two iterations to get to this point. At first I thought to do the job with a readable Stream implementing async iterable on the main thread, but then was afraid of inconsistencies that would arise between the two apis. For example Readable Stream when finished will only return `{ done: true, value: undefined }` whereas async iterables can return `{ done: true, value: any }` when `any` is any value. So, then I decided to make a async generator that could talk to the worker for better compatibility. One thing to note is that the worker data onmessage receives an extra piece for the status to cause the iterator to use. This is similar to the Promise status, but for generators.
- Loading branch information
1 parent
2dd902a
commit 6d69e9a
Showing
4 changed files
with
280 additions
and
42 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
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
type AsyncFunction<S extends any[], T> = (...args: S) => Promise<T>; | ||
interface Options { | ||
useTransferables: boolean | ||
} | ||
|
||
type MaybeAsyncFunction<S extends any[], T> = (...args: S) => (T | Promise<T>); | ||
type AsyncGenFunction<S extends any[], T=unknown, TReturn=any, TNext=unknown> = (...args: S) => AsyncGenerator<T, TReturn, TNext>; | ||
|
||
export default function greenlet<S extends any[], T>(fn: MaybeAsyncFunction<S, T>): AsyncFunction<S, T>; | ||
type AsyncFunction<S extends any[], T=unknown> = (...args: S) => Promise<T>; | ||
|
||
type GenFunction<S extends any[], T=unknown, TReturn=any, TNext=unknown> = (...args: S) => (Generator<T, TReturn, TNext> | AsyncGenerator<T, TReturn, TNext>); | ||
|
||
type MaybeAsyncFunctionButNotGen<S extends any[], T=unknown, TReturn=any,TNext=unknown> = (...args: S) => (Exclude<T, Generator<T, TReturn, TNext> | AsyncGenerator<T, TReturn, TNext>> | Promise<T>); | ||
|
||
type GreenletFnType<S extends any[], T, TReturn = any, TNext = unknown> = MaybeAsyncFunctionButNotGen<S, T, TReturn, TNext> | GenFunction<S, T, TReturn, TNext>; | ||
|
||
export default function greenlet<S extends any[], T=unknown, TReturn=any, TNext=unknown, U extends GreenletFnType<S,T,TReturn,TNext> = GreenletFnType<S, T, TReturn, TNext>>(fn: U, options?: Options): U extends GenFunction<infer S,infer T,infer TReturn,infer TNext> ? AsyncGenFunction<S, T, TReturn, TNext> : U extends MaybeAsyncFunctionButNotGen<infer S,infer T> ? AsyncFunction<S, T> : never; |