UNPKG

3.33 kBTypeScriptView Raw
1interface FiberConstructor {
2 /**
3 * Instantiate a new Fiber. You may invoke this either as a function or as
4 * a constructor; the behavior is the same.
5 *
6 * When run() is called on this fiber for the first time, `fn` will be
7 * invoked as the first frame on a new stack. Execution will continue on
8 * this new stack until `fn` returns, or Fiber.yield() is called.
9 *
10 * After the function returns the fiber is reset to original state and
11 * may be restarted with another call to run().
12 */
13 new(fn: Function): Fiber;
14 (fn: Function): Fiber;
15
16 /**
17 * `Fiber.current` will contain the currently-running Fiber. It will be
18 * `undefined` if there is no fiber (i.e. the main stack of execution).
19 *
20 * See "Garbage Collection" for more information on responsible use of
21 * `Fiber.current`.
22 */
23 readonly current?: Fiber | undefined;
24
25 /**
26 * `Fiber.yield()` will halt execution of the current fiber and return control
27 * back to original caller of run(). If an argument is supplied to yield(),
28 * run() will return that value.
29 *
30 * When run() is called again, yield() will return.
31 *
32 * Note that this function is a global to allow for correct garbage
33 * collection. This results in no loss of functionality because it is only
34 * valid to yield from the currently running fiber anyway.
35 *
36 * Note also that `yield` is a reserved word in Javascript. This is normally
37 * not an issue, however some code linters may complain. Rest assured that it
38 * will run fine now and in future versions of Javascript.
39 */
40 yield<T = any, R = any>(param?: R): T;
41}
42
43interface Fiber {
44 /**
45 * run() will start execution of this Fiber, or if it is currently yielding,
46 * it will resume execution. If an argument is supplied, this argument will
47 * be passed to the fiber, either as the first parameter to the main
48 * function [if the fiber has not been started] or as the return value of
49 * yield() [if the fiber is currently yielding].
50 *
51 * This function will return either the parameter passed to yield(), or the
52 * returned value from the fiber's main function.
53 */
54 run<T = any, R = any>(param?: T): R;
55
56 /**
57 * reset() will terminate a running Fiber and restore it to its original
58 * state, as if it had returned execution.
59 *
60 * This is accomplished by causing yield() to throw an exception, and any
61 * futher calls to yield() will also throw an exception. This continues
62 * until the fiber has completely unwound and returns.
63 *
64 * If the fiber returns a value it will be returned by reset().
65 *
66 * If the fiber is not running, reset() will have no effect.
67 */
68 reset<T = any>(): T;
69
70 /**
71 * throwInto() will cause a currently yielding fiber's yield() call to
72 * throw instead of return gracefully. This can be useful for notifying a
73 * fiber that you are no longer interested in its task, and that it should
74 * give up.
75 *
76 * Note that if the fiber does not handle the exception it will continue to
77 * bubble up and throwInto() will throw the exception right back at you.
78 */
79 throwInto(exception: Error): void;
80}
81
82declare const Fiber: FiberConstructor;
83export = Fiber;