1 | import { adaptSources, callDrivers, makeSinkProxies, disposeSources, disposeSinkProxies, isObjectEmpty, replicateMany, } from './internals';
|
2 | /**
|
3 | * A function that prepares the Cycle application to be executed. Takes a `main`
|
4 | * function and prepares to circularly connects it to the given collection of
|
5 | * driver functions. As an output, `setup()` returns an object with three
|
6 | * properties: `sources`, `sinks` and `run`. Only when `run()` is called will
|
7 | * the application actually execute. Refer to the documentation of `run()` for
|
8 | * more details.
|
9 | *
|
10 | * **Example:**
|
11 | * ```js
|
12 | * import {setup} from '@cycle/run';
|
13 | * const {sources, sinks, run} = setup(main, drivers);
|
14 | * // ...
|
15 | * const dispose = run(); // Executes the application
|
16 | * // ...
|
17 | * dispose();
|
18 | * ```
|
19 | *
|
20 | * @param {Function} main a function that takes `sources` as input and outputs
|
21 | * `sinks`.
|
22 | * @param {Object} drivers an object where keys are driver names and values
|
23 | * are driver functions.
|
24 | * @return {Object} an object with three properties: `sources`, `sinks` and
|
25 | * `run`. `sources` is the collection of driver sources, `sinks` is the
|
26 | * collection of driver sinks, these can be used for debugging or testing. `run`
|
27 | * is the function that once called will execute the application.
|
28 | * @function setup
|
29 | */
|
30 | export function setup(main, drivers) {
|
31 | if (typeof main !== "function") {
|
32 | throw new Error("First argument given to Cycle must be the 'main' " + "function.");
|
33 | }
|
34 | if (typeof drivers !== "object" || drivers === null) {
|
35 | throw new Error("Second argument given to Cycle must be an object " +
|
36 | "with driver functions as properties.");
|
37 | }
|
38 | if (isObjectEmpty(drivers)) {
|
39 | throw new Error("Second argument given to Cycle must be an object " +
|
40 | "with at least one driver function declared as a property.");
|
41 | }
|
42 | var engine = setupReusable(drivers);
|
43 | var sinks = main(engine.sources);
|
44 | if (typeof window !== 'undefined') {
|
45 | window.Cyclejs = window.Cyclejs || {};
|
46 | window.Cyclejs.sinks = sinks;
|
47 | }
|
48 | function _run() {
|
49 | var disposeRun = engine.run(sinks);
|
50 | return function dispose() {
|
51 | disposeRun();
|
52 | engine.dispose();
|
53 | };
|
54 | }
|
55 | return { sinks: sinks, sources: engine.sources, run: _run };
|
56 | }
|
57 | /**
|
58 | * A partially-applied variant of setup() which accepts only the drivers, and
|
59 | * allows many `main` functions to execute and reuse this same set of drivers.
|
60 | *
|
61 | * Takes an object with driver functions as input, and outputs an object which
|
62 | * contains the generated sources (from those drivers) and a `run` function
|
63 | * (which in turn expects sinks as argument). This `run` function can be called
|
64 | * multiple times with different arguments, and it will reuse the drivers that
|
65 | * were passed to `setupReusable`.
|
66 | *
|
67 | * **Example:**
|
68 | * ```js
|
69 | * import {setupReusable} from '@cycle/run';
|
70 | * const {sources, run, dispose} = setupReusable(drivers);
|
71 | * // ...
|
72 | * const sinks = main(sources);
|
73 | * const disposeRun = run(sinks);
|
74 | * // ...
|
75 | * disposeRun();
|
76 | * // ...
|
77 | * dispose(); // ends the reusability of drivers
|
78 | * ```
|
79 | *
|
80 | * @param {Object} drivers an object where keys are driver names and values
|
81 | * are driver functions.
|
82 | * @return {Object} an object with three properties: `sources`, `run` and
|
83 | * `dispose`. `sources` is the collection of driver sources, `run` is the
|
84 | * function that once called with 'sinks' as argument, will execute the
|
85 | * application, tying together sources with sinks. `dispose` terminates the
|
86 | * reusable resources used by the drivers. Note also that `run` returns a
|
87 | * dispose function which terminates resources that are specific (not reusable)
|
88 | * to that run.
|
89 | * @function setupReusable
|
90 | */
|
91 | export function setupReusable(drivers) {
|
92 | if (typeof drivers !== "object" || drivers === null) {
|
93 | throw new Error("Argument given to setupReusable must be an object " +
|
94 | "with driver functions as properties.");
|
95 | }
|
96 | if (isObjectEmpty(drivers)) {
|
97 | throw new Error("Argument given to setupReusable must be an object " +
|
98 | "with at least one driver function declared as a property.");
|
99 | }
|
100 | var sinkProxies = makeSinkProxies(drivers);
|
101 | var rawSources = callDrivers(drivers, sinkProxies);
|
102 | var sources = adaptSources(rawSources);
|
103 | function _run(sinks) {
|
104 | return replicateMany(sinks, sinkProxies);
|
105 | }
|
106 | function disposeEngine() {
|
107 | disposeSources(sources);
|
108 | disposeSinkProxies(sinkProxies);
|
109 | }
|
110 | return { sources: sources, run: _run, dispose: disposeEngine };
|
111 | }
|
112 | /**
|
113 | * Takes a `main` function and circularly connects it to the given collection
|
114 | * of driver functions.
|
115 | *
|
116 | * **Example:**
|
117 | * ```js
|
118 | * import run from '@cycle/run';
|
119 | * const dispose = run(main, drivers);
|
120 | * // ...
|
121 | * dispose();
|
122 | * ```
|
123 | *
|
124 | * The `main` function expects a collection of "source" streams (returned from
|
125 | * drivers) as input, and should return a collection of "sink" streams (to be
|
126 | * given to drivers). A "collection of streams" is a JavaScript object where
|
127 | * keys match the driver names registered by the `drivers` object, and values
|
128 | * are the streams. Refer to the documentation of each driver to see more
|
129 | * details on what types of sources it outputs and sinks it receives.
|
130 | *
|
131 | * @param {Function} main a function that takes `sources` as input and outputs
|
132 | * `sinks`.
|
133 | * @param {Object} drivers an object where keys are driver names and values
|
134 | * are driver functions.
|
135 | * @return {Function} a dispose function, used to terminate the execution of the
|
136 | * Cycle.js program, cleaning up resources used.
|
137 | * @function run
|
138 | */
|
139 | export function run(main, drivers) {
|
140 | var program = setup(main, drivers);
|
141 | if (typeof window !== 'undefined' &&
|
142 | window.CyclejsDevTool_startGraphSerializer) {
|
143 | window.CyclejsDevTool_startGraphSerializer(program.sinks);
|
144 | }
|
145 | return program.run();
|
146 | }
|
147 | export default run;
|
148 | //# sourceMappingURL=index.js.map |
\ | No newline at end of file |