UNPKG

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