| 1 |
import gensync, { type Gensync, type Handler, type Callback } from "gensync"; |
| 2 |
|
| 3 |
type MaybePromise<T> = T | Promise<T>; |
| 4 |
|
| 5 |
const runGenerator: { |
| 6 |
sync<Return>(gen: Handler<Return>): Return; |
| 7 |
async<Return>(gen: Handler<Return>): Promise<Return>; |
| 8 |
errback<Return>(gen: Handler<Return>, cb: Callback<Return>): void; |
| 9 |
} = gensync(function* (item: Handler<any>): Handler<any> { |
| 10 |
return yield* item; |
| 11 |
}); |
| 12 |
|
| 13 |
// This Gensync returns true if the current execution context is |
| 14 |
// asynchronous, otherwise it returns false. |
| 15 |
export const isAsync = gensync({ |
| 16 |
sync: () => false, |
| 17 |
errback: cb => cb(null, true), |
| 18 |
}); |
| 19 |
|
| 20 |
// This function wraps any functions (which could be either synchronous or |
| 21 |
// asynchronous) with a Gensync. If the wrapped function returns a promise |
| 22 |
// but the current execution context is synchronous, it will throw the |
| 23 |
// provided error. |
| 24 |
// This is used to handle user-provided functions which could be asynchronous. |
| 25 |
export function maybeAsync<Args extends unknown[], Return>( |
| 26 |
fn: (...args: Args) => Return, |
| 27 |
message: string, |
| 28 |
): Gensync<Args, Return> { |
| 29 |
return gensync({ |
| 30 |
sync(...args) { |
| 31 |
const result = fn.apply(this, args); |
| 32 |
if (isThenable(result)) throw new Error(message); |
| 33 |
return result; |
| 34 |
}, |
| 35 |
async(...args) { |
| 36 |
return Promise.resolve(fn.apply(this, args)); |
| 37 |
}, |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
const withKind = gensync({ |
| 42 |
sync: cb => cb("sync"), |
| 43 |
async: async cb => cb("async"), |
| 44 |
}) as <T>(cb: (kind: "sync" | "async") => MaybePromise<T>) => Handler<T>; |
| 45 |
|
| 46 |
// This function wraps a generator (or a Gensync) into another function which, |
| 47 |
// when called, will run the provided generator in a sync or async way, depending |
| 48 |
// on the execution context where this forwardAsync function is called. |
| 49 |
// This is useful, for example, when passing a callback to a function which isn't |
| 50 |
// aware of gensync, but it only knows about synchronous and asynchronous functions. |
| 51 |
// An example is cache.using, which being exposed to the user must be as simple as |
| 52 |
// possible: |
| 53 |
// yield* forwardAsync(gensyncFn, wrappedFn => |
| 54 |
// cache.using(x => { |
| 55 |
// // Here we don't know about gensync. wrappedFn is a |
| 56 |
// // normal sync or async function |
| 57 |
// return wrappedFn(x); |
| 58 |
// }) |
| 59 |
// ) |
| 60 |
export function forwardAsync<Args extends unknown[], Return>( |
| 61 |
action: (...args: Args) => Handler<Return>, |
| 62 |
cb: ( |
| 63 |
adapted: (...args: Args) => MaybePromise<Return>, |
| 64 |
) => MaybePromise<Return>, |
| 65 |
): Handler<Return> { |
| 66 |
const g = gensync(action); |
| 67 |
return withKind(kind => { |
| 68 |
const adapted = g[kind]; |
| 69 |
return cb(adapted); |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
// If the given generator is executed asynchronously, the first time that it |
| 74 |
// is paused (i.e. When it yields a gensync generator which can't be run |
| 75 |
// synchronously), call the "firstPause" callback. |
| 76 |
export const onFirstPause = gensync< |
| 77 |
[gen: Handler<unknown>, firstPause: () => void], |
| 78 |
unknown |
| 79 |
>({ |
| 80 |
name: "onFirstPause", |
| 81 |
arity: 2, |
| 82 |
sync: function (item) { |
| 83 |
return runGenerator.sync(item); |
| 84 |
}, |
| 85 |
errback: function (item, firstPause, cb) { |
| 86 |
let completed = false; |
| 87 |
|
| 88 |
runGenerator.errback(item, (err, value) => { |
| 89 |
completed = true; |
| 90 |
cb(err, value); |
| 91 |
}); |
| 92 |
|
| 93 |
if (!completed) { |
| 94 |
firstPause(); |
| 95 |
} |
| 96 |
}, |
| 97 |
}) as <T>(gen: Handler<T>, firstPause: () => void) => Handler<T>; |
| 98 |
|
| 99 |
// Wait for the given promise to be resolved |
| 100 |
export const waitFor = gensync({ |
| 101 |
sync: x => x, |
| 102 |
async: async x => x, |
| 103 |
}) as <T>(p: T | Promise<T>) => Handler<T>; |
| 104 |
|
| 105 |
export function isThenable<T = any>(val: any): val is PromiseLike<T> { |
| 106 |
return ( |
| 107 |
!!val && |
| 108 |
(typeof val === "object" || typeof val === "function") && |
| 109 |
!!val.then && |
| 110 |
typeof val.then === "function" |
| 111 |
); |
| 112 |
} |