Debian Codegraph @

debian/build_modules/@types/gensync/index.d.ts

raw
1 // Type definitions for gensync 1.0
2 // Project: https://github.com/loganfsmyth/gensync
3 // Definitions by: Jake Bailey <https://github.com/jakebailey>
4 // Nicolò Ribaudo <https://github.com/nicolo-ribaudo>
5 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6 // Minimum TypeScript Version: 4.0
7
8 /**
9 * Returns a function that can be "awaited" (with `yield*`) in another `gensync` generator
10 * function, or executed via
11 *
12 * - `.sync(...args)` - Returns the computed value, or throws.
13 * - `.async(...args)` - Returns a promise for the computed value.
14 * - `.errback(...args, (err, result) => {})` - Calls the callback with the computed value, or error.
15 * @param generatorFnOrOptions A generator function, or options for an existing sync/async function
16 */
17 declare function gensync<A extends unknown[], R, E = unknown>(
18 generatorFnOrOptions: ((...args: A) => Generator<gensync.Handler, R>) | gensync.Options<A, R, E>,
19 ): gensync.Gensync<A, R, E>;
20
21 declare namespace gensync {
22 /**
23 * A generator produced by `gensync`, which can only "await" (with `yield*`) other
24 * generators produced by `gensync`.
25 */
26 type Handler<R = unknown> = Generator<Handler, R>;
27
28 /**
29 * Given a `gensync` generator, produces the "awaited" type of that generator
30 * when "yield*"'d in another `gensync` generator.
31 */
32 type Handled<T> = T extends Handler<infer U> ? U : never;
33
34 /**
35 * A callback function such that if the result is void, there is no result parameter.
36 */
37 // tslint:disable-next-line void-return
38 type Callback<R, E = unknown> = [R] extends [void] ? (err: E) => void : (err: E, result: R) => void;
39
40 /**
41 * A function that can be "awaited" (with `yield*`) in another `gensync` generator,
42 * or executed via
43 *
44 * - `.sync(...args)` - Returns the computed value, or throws.
45 * - `.async(...args)` - Returns a promise for the computed value.
46 * - `.errback(...args, (err, result) => {})` - Calls the callback wit
47 */
48 interface Gensync<A extends unknown[], R, E = unknown> {
49 (...args: A): Handler<R>;
50 sync(...args: A): R;
51 async(...args: A): Promise<R>;
52 errback(...args: [...args: A, callback: Callback<R, E>]): void;
53 }
54
55 interface SyncOptions<A extends unknown[], R> {
56 /**
57 * A function that will be called when `.sync()` is called on the `gensync()`
58 * result, or when the result is passed to `yield*` in another generator that
59 * is being run synchronously.
60 *
61 * Also called for `.async()` calls if no async handlers are provided.
62 */
63 sync: (...args: A) => R;
64
65 /**
66 * A string name to apply to the returned function. If no value is provided,
67 * the name of `errback`/`async`/`sync` functions will be used, with any
68 * `Sync` or `Async` suffix stripped off. If the callback is simply named
69 * with ES6 inference (same name as the options property), the name is ignored.
70 */
71 name?: string | undefined;
72
73 /**
74 * A number for the length to set on the returned function. If no value
75 * is provided, the length will be carried over from the `sync` function's
76 * `length` value.
77 */
78 arity?: number | undefined;
79
80 // Mutually exclusive options.
81 async?: undefined;
82 errback?: undefined;
83 }
84
85 interface AsyncOptions<A extends unknown[], R> extends Omit<SyncOptions<A, R>, 'async'> {
86 /**
87 * A function that will be called when `.async()` or `.errback()` is called on
88 * the `gensync()` result, or when the result is passed to `yield*` in another
89 * generator that is being run asynchronously.
90 *
91 * Must not be specified with `errback`.
92 */
93 async: (...args: A) => Promise<R>;
94 }
95
96 interface ErrbackOptions<A extends unknown[], R, E = unknown> extends Omit<SyncOptions<A, R>, 'errback'> {
97 /**
98 * A function that will be called when `.async()` or `.errback()` is called on
99 * the `gensync()` result, or when the result is passed to `yield*` in another
100 * generator that is being run asynchronously.
101 *
102 * This option allows for simpler compatibility with many existing Node APIs,
103 * and also avoids introducing the extra even loop turns that promises introduce
104 * to access the result value.
105 *
106 * Must not be specified with `async`.
107 */
108 errback: (...args: [...A, Callback<R, E>]) => void;
109 }
110
111 type Options<A extends unknown[], R, E = unknown> =
112 | SyncOptions<A, R>
113 | AsyncOptions<A, R>
114 | ErrbackOptions<A, R, E>;
115
116 // "all" and "race"'s types are pretty much copied from Promise.all and Promise.race,
117 // replacing Awaited with Handled.
118
119 /**
120 * `Promise.all`-like combinator that works with an iterable of generator objects
121 * that could be passed to `yield*` within a gensync generator.
122 * @param args An array of gensync generators
123 * @returns A new gensync generator
124 */
125 function all<T extends readonly Handler[] | []>(args: T): Handler<{ -readonly [P in keyof T]: Handled<T[P]> }>;
126
127 /**
128 * `Promise.all`-like combinator that works with an iterable of generator objects
129 * that could be passed to `yield*` within a gensync generator.
130 * @param args An iterable of gensync generators
131 * @returns A new gensync generator
132 */
133 function all<T>(args: Iterable<Handler<T>>): Handler<T[]>;
134
135 /**
136 * `Promise.race`-like combinator that works with an iterable of generator objects
137 * that could be passed to `yield*` within a gensync generator.
138 * @param args An array of gensync generators
139 * @returns A new gensync generator
140 */
141 function race<T extends readonly Handler[] | []>(args: T): Handler<Handled<T[number]>>;
142
143 /**
144 * `Promise.race`-like combinator that works with an iterable of generator objects
145 * that could be passed to `yield*` within a gensync generator.
146 * @param args An iterable of gensync generators
147 * @returns A new gensync generator
148 */
149 function race<T>(args: Iterable<Handler<T>>): Handler<T>;
150 }
151
152 export = gensync;