UNPKG

2.92 kBPlain TextView Raw
1import {
2 CycleExecution,
3 CycleSetup,
4 DisposeFunction
5} from '@cycle/base';
6import CycleBase from '@cycle/base';
7import RxAdapter from '@cycle/rx-adapter';
8
9/**
10 * A function that prepares the Cycle application to be executed. Takes a `main`
11 * function and prepares to circularly connects it to the given collection of
12 * driver functions. As an output, `Cycle()` returns an object with three
13 * properties: `sources`, `sinks` and `run`. Only when `run()` is called will
14 * the application actually execute. Refer to the documentation of `run()` for
15 * more details.
16 *
17 * **Example:**
18 * ```js
19 * const {sources, sinks, run} = Cycle(main, drivers);
20 * // ...
21 * const dispose = run(); // Executes the application
22 * // ...
23 * dispose();
24 * ```
25 *
26 * @param {Function} main a function that takes `sources` as input
27 * and outputs a collection of `sinks` Observables.
28 * @param {Object} drivers an object where keys are driver names and values
29 * are driver functions.
30 * @return {Object} an object with three properties: `sources`, `sinks` and
31 * `run`. `sources` is the collection of driver sources, `sinks` is the
32 * collection of driver sinks, these can be used for debugging or testing. `run`
33 * is the function that once called will execute the application.
34 * @function Cycle
35 */
36const Cycle: CycleSetup = <CycleSetup>
37 function <Sources, Sinks>(main: (sources: Sources) => Sinks,
38 drivers: {[name: string]: Function}): CycleExecution<Sources, Sinks> {
39 return CycleBase(main, drivers, {streamAdapter: RxAdapter});
40 };
41
42/**
43 * Takes a `main` function and circularly connects it to the given collection
44 * of driver functions.
45 *
46 * **Example:**
47 * ```js
48 * const dispose = Cycle.run(main, drivers);
49 * // ...
50 * dispose();
51 * ```
52 *
53 * The `main` function expects a collection of "source" Observables (returned
54 * from drivers) as input, and should return a collection of "sink" Observables
55 * (to be given to drivers). A "collection of Observables" is a JavaScript
56 * object where keys match the driver names registered by the `drivers` object,
57 * and values are the Observables. Refer to the documentation of each driver to
58 * see more details on what types of sources it outputs and sinks it receives.
59 *
60 * @param {Function} main a function that takes `sources` as input
61 * and outputs a collection of `sinks` Observables.
62 * @param {Object} drivers an object where keys are driver names and values
63 * are driver functions.
64 * @return {Function} a dispose function, used to terminate the execution of the
65 * Cycle.js program, cleaning up resources used.
66 * @function run
67 */
68export function run<Sources, Sinks>(main: (sources: Sources) => Sinks,
69 drivers: {[name: string]: Function}): DisposeFunction {
70 const {run} = CycleBase(main, drivers, {streamAdapter: RxAdapter});
71 return run();
72}
73
74Cycle.run = run;
75
76export default Cycle;