UNPKG

15.1 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// shim for using process in browser
3var process = module.exports = {};
4
5// cached from whatever global is present so that test runners that stub it
6// don't break things. But we need to wrap it in a try catch in case it is
7// wrapped in strict mode code which doesn't define any globals. It's inside a
8// function because try/catches deoptimize in certain engines.
9
10var cachedSetTimeout;
11var cachedClearTimeout;
12
13function defaultSetTimout() {
14 throw new Error('setTimeout has not been defined');
15}
16function defaultClearTimeout () {
17 throw new Error('clearTimeout has not been defined');
18}
19(function () {
20 try {
21 if (typeof setTimeout === 'function') {
22 cachedSetTimeout = setTimeout;
23 } else {
24 cachedSetTimeout = defaultSetTimout;
25 }
26 } catch (e) {
27 cachedSetTimeout = defaultSetTimout;
28 }
29 try {
30 if (typeof clearTimeout === 'function') {
31 cachedClearTimeout = clearTimeout;
32 } else {
33 cachedClearTimeout = defaultClearTimeout;
34 }
35 } catch (e) {
36 cachedClearTimeout = defaultClearTimeout;
37 }
38} ())
39function runTimeout(fun) {
40 if (cachedSetTimeout === setTimeout) {
41 //normal enviroments in sane situations
42 return setTimeout(fun, 0);
43 }
44 // if setTimeout wasn't available but was latter defined
45 if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
46 cachedSetTimeout = setTimeout;
47 return setTimeout(fun, 0);
48 }
49 try {
50 // when when somebody has screwed with setTimeout but no I.E. maddness
51 return cachedSetTimeout(fun, 0);
52 } catch(e){
53 try {
54 // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
55 return cachedSetTimeout.call(null, fun, 0);
56 } catch(e){
57 // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
58 return cachedSetTimeout.call(this, fun, 0);
59 }
60 }
61
62
63}
64function runClearTimeout(marker) {
65 if (cachedClearTimeout === clearTimeout) {
66 //normal enviroments in sane situations
67 return clearTimeout(marker);
68 }
69 // if clearTimeout wasn't available but was latter defined
70 if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
71 cachedClearTimeout = clearTimeout;
72 return clearTimeout(marker);
73 }
74 try {
75 // when when somebody has screwed with setTimeout but no I.E. maddness
76 return cachedClearTimeout(marker);
77 } catch (e){
78 try {
79 // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
80 return cachedClearTimeout.call(null, marker);
81 } catch (e){
82 // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
83 // Some versions of I.E. have different rules for clearTimeout vs setTimeout
84 return cachedClearTimeout.call(this, marker);
85 }
86 }
87
88
89
90}
91var queue = [];
92var draining = false;
93var currentQueue;
94var queueIndex = -1;
95
96function cleanUpNextTick() {
97 if (!draining || !currentQueue) {
98 return;
99 }
100 draining = false;
101 if (currentQueue.length) {
102 queue = currentQueue.concat(queue);
103 } else {
104 queueIndex = -1;
105 }
106 if (queue.length) {
107 drainQueue();
108 }
109}
110
111function drainQueue() {
112 if (draining) {
113 return;
114 }
115 var timeout = runTimeout(cleanUpNextTick);
116 draining = true;
117
118 var len = queue.length;
119 while(len) {
120 currentQueue = queue;
121 queue = [];
122 while (++queueIndex < len) {
123 if (currentQueue) {
124 currentQueue[queueIndex].run();
125 }
126 }
127 queueIndex = -1;
128 len = queue.length;
129 }
130 currentQueue = null;
131 draining = false;
132 runClearTimeout(timeout);
133}
134
135process.nextTick = function (fun) {
136 var args = new Array(arguments.length - 1);
137 if (arguments.length > 1) {
138 for (var i = 1; i < arguments.length; i++) {
139 args[i - 1] = arguments[i];
140 }
141 }
142 queue.push(new Item(fun, args));
143 if (queue.length === 1 && !draining) {
144 runTimeout(drainQueue);
145 }
146};
147
148// v8 likes predictible objects
149function Item(fun, array) {
150 this.fun = fun;
151 this.array = array;
152}
153Item.prototype.run = function () {
154 this.fun.apply(null, this.array);
155};
156process.title = 'browser';
157process.browser = true;
158process.env = {};
159process.argv = [];
160process.version = ''; // empty string to avoid regexp issues
161process.versions = {};
162
163function noop() {}
164
165process.on = noop;
166process.addListener = noop;
167process.once = noop;
168process.off = noop;
169process.removeListener = noop;
170process.removeAllListeners = noop;
171process.emit = noop;
172
173process.binding = function (name) {
174 throw new Error('process.binding is not supported');
175};
176
177process.cwd = function () { return '/' };
178process.chdir = function (dir) {
179 throw new Error('process.chdir is not supported');
180};
181process.umask = function() { return 0; };
182
183},{}],2:[function(require,module,exports){
184(function (global){
185"use strict";
186Object.defineProperty(exports, "__esModule", { value: true });
187function getGlobal() {
188 var globalObj;
189 if (typeof window !== 'undefined') {
190 globalObj = window;
191 }
192 else if (typeof global !== 'undefined') {
193 globalObj = global;
194 }
195 else {
196 globalObj = this;
197 }
198 globalObj.Cyclejs = globalObj.Cyclejs || {};
199 globalObj = globalObj.Cyclejs;
200 globalObj.adaptStream = globalObj.adaptStream || (function (x) { return x; });
201 return globalObj;
202}
203function setAdapt(f) {
204 getGlobal().adaptStream = f;
205}
206exports.setAdapt = setAdapt;
207function adapt(stream) {
208 return getGlobal().adaptStream(stream);
209}
210exports.adapt = adapt;
211
212}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
213},{}],3:[function(require,module,exports){
214(function (global){
215"use strict";
216Object.defineProperty(exports, "__esModule", { value: true });
217var xstream_1 = (typeof window !== "undefined" ? window['xstream'] : typeof global !== "undefined" ? global['xstream'] : null);
218var adapt_1 = require("./adapt");
219var quicktask_1 = require("quicktask");
220var scheduleMicrotask = quicktask_1.default();
221function makeSinkProxies(drivers) {
222 var sinkProxies = {};
223 for (var name_1 in drivers) {
224 if (drivers.hasOwnProperty(name_1)) {
225 sinkProxies[name_1] = xstream_1.default.create();
226 }
227 }
228 return sinkProxies;
229}
230function callDrivers(drivers, sinkProxies) {
231 var sources = {};
232 for (var name_2 in drivers) {
233 if (drivers.hasOwnProperty(name_2)) {
234 sources[name_2] = drivers[name_2](sinkProxies[name_2], name_2);
235 if (sources[name_2] && typeof sources[name_2] === 'object') {
236 sources[name_2]._isCycleSource = name_2;
237 }
238 }
239 }
240 return sources;
241}
242// NOTE: this will mutate `sources`.
243function adaptSources(sources) {
244 for (var name_3 in sources) {
245 if (sources.hasOwnProperty(name_3) &&
246 sources[name_3] &&
247 typeof sources[name_3]['shamefullySendNext'] === 'function') {
248 sources[name_3] = adapt_1.adapt(sources[name_3]);
249 }
250 }
251 return sources;
252}
253function replicateMany(sinks, sinkProxies) {
254 var sinkNames = Object.keys(sinks).filter(function (name) { return !!sinkProxies[name]; });
255 var buffers = {};
256 var replicators = {};
257 sinkNames.forEach(function (name) {
258 buffers[name] = { _n: [], _e: [] };
259 replicators[name] = {
260 next: function (x) { return buffers[name]._n.push(x); },
261 error: function (err) { return buffers[name]._e.push(err); },
262 complete: function () { },
263 };
264 });
265 var subscriptions = sinkNames.map(function (name) {
266 return xstream_1.default.fromObservable(sinks[name]).subscribe(replicators[name]);
267 });
268 sinkNames.forEach(function (name) {
269 var listener = sinkProxies[name];
270 var next = function (x) {
271 scheduleMicrotask(function () { return listener._n(x); });
272 };
273 var error = function (err) {
274 scheduleMicrotask(function () {
275 (console.error || console.log)(err);
276 listener._e(err);
277 });
278 };
279 buffers[name]._n.forEach(next);
280 buffers[name]._e.forEach(error);
281 replicators[name].next = next;
282 replicators[name].error = error;
283 // because sink.subscribe(replicator) had mutated replicator to add
284 // _n, _e, _c, we must also update these:
285 replicators[name]._n = next;
286 replicators[name]._e = error;
287 });
288 buffers = null; // free up for GC
289 return function disposeReplication() {
290 subscriptions.forEach(function (s) { return s.unsubscribe(); });
291 sinkNames.forEach(function (name) { return sinkProxies[name]._c(); });
292 };
293}
294function disposeSources(sources) {
295 for (var k in sources) {
296 if (sources.hasOwnProperty(k) &&
297 sources[k] &&
298 sources[k].dispose) {
299 sources[k].dispose();
300 }
301 }
302}
303function isObjectEmpty(obj) {
304 return Object.keys(obj).length === 0;
305}
306/**
307 * A function that prepares the Cycle application to be executed. Takes a `main`
308 * function and prepares to circularly connects it to the given collection of
309 * driver functions. As an output, `setup()` returns an object with three
310 * properties: `sources`, `sinks` and `run`. Only when `run()` is called will
311 * the application actually execute. Refer to the documentation of `run()` for
312 * more details.
313 *
314 * **Example:**
315 * ```js
316 * import {setup} from '@cycle/run';
317 * const {sources, sinks, run} = setup(main, drivers);
318 * // ...
319 * const dispose = run(); // Executes the application
320 * // ...
321 * dispose();
322 * ```
323 *
324 * @param {Function} main a function that takes `sources` as input and outputs
325 * `sinks`.
326 * @param {Object} drivers an object where keys are driver names and values
327 * are driver functions.
328 * @return {Object} an object with three properties: `sources`, `sinks` and
329 * `run`. `sources` is the collection of driver sources, `sinks` is the
330 * collection of driver sinks, these can be used for debugging or testing. `run`
331 * is the function that once called will execute the application.
332 * @function setup
333 */
334function setup(main, drivers) {
335 if (typeof main !== "function") {
336 throw new Error("First argument given to Cycle must be the 'main' " + "function.");
337 }
338 if (typeof drivers !== "object" || drivers === null) {
339 throw new Error("Second argument given to Cycle must be an object " +
340 "with driver functions as properties.");
341 }
342 if (isObjectEmpty(drivers)) {
343 throw new Error("Second argument given to Cycle must be an object " +
344 "with at least one driver function declared as a property.");
345 }
346 var sinkProxies = makeSinkProxies(drivers);
347 var sources = callDrivers(drivers, sinkProxies);
348 var adaptedSources = adaptSources(sources);
349 var sinks = main(adaptedSources);
350 if (typeof window !== 'undefined') {
351 window.Cyclejs = window.Cyclejs || {};
352 window.Cyclejs.sinks = sinks;
353 }
354 function _run() {
355 var disposeReplication = replicateMany(sinks, sinkProxies);
356 return function dispose() {
357 disposeSources(sources);
358 disposeReplication();
359 };
360 }
361 return { sinks: sinks, sources: sources, run: _run };
362}
363exports.setup = setup;
364/**
365 * Takes a `main` function and circularly connects it to the given collection
366 * of driver functions.
367 *
368 * **Example:**
369 * ```js
370 * import run from '@cycle/run';
371 * const dispose = run(main, drivers);
372 * // ...
373 * dispose();
374 * ```
375 *
376 * The `main` function expects a collection of "source" streams (returned from
377 * drivers) as input, and should return a collection of "sink" streams (to be
378 * given to drivers). A "collection of streams" is a JavaScript object where
379 * keys match the driver names registered by the `drivers` object, and values
380 * are the streams. Refer to the documentation of each driver to see more
381 * details on what types of sources it outputs and sinks it receives.
382 *
383 * @param {Function} main a function that takes `sources` as input and outputs
384 * `sinks`.
385 * @param {Object} drivers an object where keys are driver names and values
386 * are driver functions.
387 * @return {Function} a dispose function, used to terminate the execution of the
388 * Cycle.js program, cleaning up resources used.
389 * @function run
390 */
391function run(main, drivers) {
392 var program = setup(main, drivers);
393 if (typeof window !== 'undefined' &&
394 window['CyclejsDevTool_startGraphSerializer']) {
395 window['CyclejsDevTool_startGraphSerializer'](program.sinks);
396 }
397 return program.run();
398}
399exports.run = run;
400exports.default = run;
401
402}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
403},{"./adapt":2,"quicktask":4}],4:[function(require,module,exports){
404(function (process){
405"use strict";
406Object.defineProperty(exports, "__esModule", { value: true });
407function microtask() {
408 if (typeof MutationObserver !== 'undefined') {
409 var node_1 = document.createTextNode('');
410 var queue_1 = [];
411 var i_1 = 0;
412 new MutationObserver(function () {
413 while (queue_1.length) {
414 queue_1.shift()();
415 }
416 }).observe(node_1, { characterData: true });
417 return function (fn) {
418 queue_1.push(fn);
419 node_1.data = i_1 = 1 - i_1;
420 };
421 }
422 else if (typeof setImmediate !== 'undefined') {
423 return setImmediate;
424 }
425 else if (typeof process !== 'undefined') {
426 return process.nextTick;
427 }
428 else {
429 return setTimeout;
430 }
431}
432exports.default = microtask;
433
434}).call(this,require('_process'))
435},{"_process":1}]},{},[3])(3)
436});
\No newline at end of file