UNPKG

9.97 kBJavaScriptView Raw
1(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cycle = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2"use strict";
3
4var base_1 = require('@cycle/base');
5var rx_adapter_1 = require('@cycle/rx-adapter');
6/**
7 * Takes a `main` function and circularly connects it to the given collection
8 * of driver functions.
9 *
10 * **Example:**
11 * ```js
12 * import {run} from '@cycle/rx-run';
13 * const dispose = Cycle.run(main, drivers);
14 * // ...
15 * dispose();
16 * ```
17 *
18 * The `main` function expects a collection of "source" Observables (returned
19 * from drivers) as input, and should return a collection of "sink" Observables
20 * (to be given to drivers). A "collection of Observables" is a JavaScript
21 * object where keys match the driver names registered by the `drivers` object,
22 * and values are the Observables. Refer to the documentation of each driver to
23 * see more details on what types of sources it outputs and sinks it receives.
24 *
25 * @param {Function} main a function that takes `sources` as input
26 * and outputs a collection of `sinks` Observables.
27 * @param {Object} drivers an object where keys are driver names and values
28 * are driver functions.
29 * @return {Function} a dispose function, used to terminate the execution of the
30 * Cycle.js program, cleaning up resources used.
31 * @function run
32 */
33function run(main, drivers) {
34 var run = base_1.default(main, drivers, { streamAdapter: rx_adapter_1.default }).run;
35 return run();
36}
37exports.run = run;
38/**
39 * A function that prepares the Cycle application to be executed. Takes a `main`
40 * function and prepares to circularly connects it to the given collection of
41 * driver functions. As an output, `Cycle()` returns an object with three
42 * properties: `sources`, `sinks` and `run`. Only when `run()` is called will
43 * the application actually execute. Refer to the documentation of `run()` for
44 * more details.
45 *
46 * **Example:**
47 * ```js
48 * import Cycle from '@cycle/rx-run';
49 * const {sources, sinks, run} = Cycle(main, drivers);
50 * // ...
51 * const dispose = run(); // Executes the application
52 * // ...
53 * dispose();
54 * ```
55 *
56 * @param {Function} main a function that takes `sources` as input
57 * and outputs a collection of `sinks` Observables.
58 * @param {Object} drivers an object where keys are driver names and values
59 * are driver functions.
60 * @return {Object} an object with three properties: `sources`, `sinks` and
61 * `run`. `sources` is the collection of driver sources, `sinks` is the
62 * collection of driver sinks, these can be used for debugging or testing. `run`
63 * is the function that once called will execute the application.
64 * @function Cycle
65 */
66var Cycle = function Cycle(main, drivers) {
67 return base_1.default(main, drivers, { streamAdapter: rx_adapter_1.default });
68};
69Cycle.run = run;
70Object.defineProperty(exports, "__esModule", { value: true });
71exports.default = Cycle;
72
73
74},{"@cycle/base":2,"@cycle/rx-adapter":3}],2:[function(require,module,exports){
75"use strict";
76
77var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
78
79function logToConsoleError(err) {
80 var target = err.stack || err;
81 if (console && console.error) {
82 console.error(target);
83 } else if (console && console.log) {
84 console.log(target);
85 }
86}
87function makeSinkProxies(drivers, streamAdapter) {
88 var sinkProxies = {};
89 for (var name_1 in drivers) {
90 if (drivers.hasOwnProperty(name_1)) {
91 var holdSubject = streamAdapter.makeSubject();
92 var driverStreamAdapter = drivers[name_1].streamAdapter || streamAdapter;
93 var stream = driverStreamAdapter.adapt(holdSubject.stream, streamAdapter.streamSubscribe);
94 sinkProxies[name_1] = {
95 stream: stream,
96 observer: holdSubject.observer
97 };
98 }
99 }
100 return sinkProxies;
101}
102function callDrivers(drivers, sinkProxies, streamAdapter) {
103 var sources = {};
104 for (var name_2 in drivers) {
105 if (drivers.hasOwnProperty(name_2)) {
106 var driverOutput = drivers[name_2](sinkProxies[name_2].stream, streamAdapter, name_2);
107 var driverStreamAdapter = drivers[name_2].streamAdapter;
108 if (driverStreamAdapter && driverStreamAdapter.isValidStream(driverOutput)) {
109 sources[name_2] = streamAdapter.adapt(driverOutput, driverStreamAdapter.streamSubscribe);
110 } else {
111 sources[name_2] = driverOutput;
112 }
113 }
114 }
115 return sources;
116}
117function replicateMany(sinks, sinkProxies, streamAdapter) {
118 var results = Object.keys(sinks).filter(function (name) {
119 return !!sinkProxies[name];
120 }).map(function (name) {
121 return streamAdapter.streamSubscribe(sinks[name], {
122 next: function next(x) {
123 sinkProxies[name].observer.next(x);
124 },
125 error: function error(err) {
126 logToConsoleError(err);
127 sinkProxies[name].observer.error(err);
128 },
129 complete: function complete(x) {
130 sinkProxies[name].observer.complete(x);
131 }
132 });
133 });
134 var disposeFunctions = results.filter(function (dispose) {
135 return typeof dispose === 'function';
136 });
137 return function () {
138 disposeFunctions.forEach(function (dispose) {
139 return dispose();
140 });
141 };
142}
143function disposeSources(sources) {
144 for (var k in sources) {
145 if (sources.hasOwnProperty(k) && sources[k] && typeof sources[k].dispose === 'function') {
146 sources[k].dispose();
147 }
148 }
149}
150var isObjectEmpty = function isObjectEmpty(obj) {
151 return Object.keys(obj).length === 0;
152};
153function Cycle(main, drivers, options) {
154 if (typeof main !== "function") {
155 throw new Error("First argument given to Cycle must be the 'main' " + "function.");
156 }
157 if ((typeof drivers === 'undefined' ? 'undefined' : _typeof(drivers)) !== "object" || drivers === null) {
158 throw new Error("Second argument given to Cycle must be an object " + "with driver functions as properties.");
159 }
160 if (isObjectEmpty(drivers)) {
161 throw new Error("Second argument given to Cycle must be an object " + "with at least one driver function declared as a property.");
162 }
163 var streamAdapter = options.streamAdapter;
164 if (!streamAdapter || isObjectEmpty(streamAdapter)) {
165 throw new Error("Third argument given to Cycle must be an options object " + "with the streamAdapter key supplied with a valid stream adapter.");
166 }
167 var sinkProxies = makeSinkProxies(drivers, streamAdapter);
168 var sources = callDrivers(drivers, sinkProxies, streamAdapter);
169 var sinks = main(sources);
170 if (typeof window !== 'undefined') {
171 window.Cyclejs = { sinks: sinks };
172 }
173 var run = function run() {
174 var disposeReplication = replicateMany(sinks, sinkProxies, streamAdapter);
175 return function () {
176 disposeSources(sources);
177 disposeReplication();
178 };
179 };
180 return { sinks: sinks, sources: sources, run: run };
181}
182Object.defineProperty(exports, "__esModule", { value: true });
183exports.default = Cycle;
184
185
186},{}],3:[function(require,module,exports){
187"use strict";
188var Rx = require('rx');
189var RxJSAdapter = {
190 adapt: function (originStream, originStreamSubscribe) {
191 if (this.isValidStream(originStream)) {
192 return originStream;
193 }
194 return Rx.Observable.create(function (destinationObserver) {
195 var originObserver = {
196 next: function (x) { return destinationObserver.onNext(x); },
197 error: function (e) { return destinationObserver.onError(e); },
198 complete: function () { return destinationObserver.onCompleted(); },
199 };
200 var dispose = originStreamSubscribe(originStream, originObserver);
201 return function () {
202 if (typeof dispose === 'function') {
203 dispose.call(null);
204 }
205 };
206 });
207 },
208 remember: function (observable) {
209 return observable.shareReplay(1);
210 },
211 makeSubject: function () {
212 var stream = new Rx.Subject();
213 var observer = {
214 next: function (x) { stream.onNext(x); },
215 error: function (err) { stream.onError(err); },
216 complete: function (x) { stream.onCompleted(); }
217 };
218 return { stream: stream, observer: observer };
219 },
220 isValidStream: function (stream) {
221 return (typeof stream.subscribeOnNext === 'function' &&
222 typeof stream.onValue !== 'function');
223 },
224 streamSubscribe: function (stream, observer) {
225 var subscription = stream.subscribe(function (x) { return observer.next(x); }, function (e) { return observer.error(e); }, function (x) { return observer.complete(x); });
226 return function () {
227 subscription.dispose();
228 };
229 }
230};
231Object.defineProperty(exports, "__esModule", { value: true });
232exports.default = RxJSAdapter;
233
234},{"rx":undefined}]},{},[1])(1)
235});
\No newline at end of file