UNPKG

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