@cycle/core
Advanced tools
Comparing version
@@ -7,32 +7,2 @@ (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){ | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
* function and prepares to circularly connects it to the given collection of | ||
* driver functions. As an output, `Cycle()` returns an object with three | ||
* properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
* the application actually execute. Refer to the documentation of `run()` for | ||
* more details. | ||
* | ||
* **Example:** | ||
* ```js | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
* // ... | ||
* const dispose = run(); // Executes the application | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Object} an object with three properties: `sources`, `sinks` and | ||
* `run`. `sources` is the collection of driver sources, `sinks` is the | ||
* collection of driver sinks, these can be used for debugging or testing. `run` | ||
* is the function that once called will execute the application. | ||
* @function Cycle | ||
*/ | ||
var Cycle = function Cycle(main, drivers) { | ||
return base_1.default(main, drivers, { streamAdapter: rx_adapter_1.default }); | ||
}; | ||
/** | ||
* Takes a `main` function and circularly connects it to the given collection | ||
@@ -43,2 +13,3 @@ * of driver functions. | ||
* ```js | ||
* import {run} from '@cycle/rx-run'; | ||
* const dispose = Cycle.run(main, drivers); | ||
@@ -69,2 +40,33 @@ * // ... | ||
exports.run = run; | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
* function and prepares to circularly connects it to the given collection of | ||
* driver functions. As an output, `Cycle()` returns an object with three | ||
* properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
* the application actually execute. Refer to the documentation of `run()` for | ||
* more details. | ||
* | ||
* **Example:** | ||
* ```js | ||
* import Cycle from '@cycle/rx-run'; | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
* // ... | ||
* const dispose = run(); // Executes the application | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Object} an object with three properties: `sources`, `sinks` and | ||
* `run`. `sources` is the collection of driver sources, `sinks` is the | ||
* collection of driver sinks, these can be used for debugging or testing. `run` | ||
* is the function that once called will execute the application. | ||
* @function Cycle | ||
*/ | ||
var Cycle = function Cycle(main, drivers) { | ||
return base_1.default(main, drivers, { streamAdapter: rx_adapter_1.default }); | ||
}; | ||
Cycle.run = run; | ||
@@ -71,0 +73,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -1,1 +0,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){"use strict";var base_1=require("@cycle/base");var rx_adapter_1=require("@cycle/rx-adapter");var Cycle=function Cycle(main,drivers){return base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default})};function run(main,drivers){var run=base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default}).run;return run()}exports.run=run;Cycle.run=run;Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{"@cycle/base":2,"@cycle/rx-adapter":3}],2:[function(require,module,exports){"use strict";var _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};function logToConsoleError(err){var target=err.stack||err;if(console&&console.error){console.error(target)}else if(console&&console.log){console.log(target)}}function makeSinkProxies(drivers,streamAdapter){var sinkProxies={};for(var name_1 in drivers){if(drivers.hasOwnProperty(name_1)){var holdSubject=streamAdapter.makeSubject();var driverStreamAdapter=drivers[name_1].streamAdapter||streamAdapter;var stream=driverStreamAdapter.adapt(holdSubject.stream,streamAdapter.streamSubscribe);sinkProxies[name_1]={stream:stream,observer:holdSubject.observer}}}return sinkProxies}function callDrivers(drivers,sinkProxies,streamAdapter){var sources={};for(var name_2 in drivers){if(drivers.hasOwnProperty(name_2)){var driverOutput=drivers[name_2](sinkProxies[name_2].stream,streamAdapter,name_2);var driverStreamAdapter=drivers[name_2].streamAdapter;if(driverStreamAdapter&&driverStreamAdapter.isValidStream(driverOutput)){sources[name_2]=streamAdapter.adapt(driverOutput,driverStreamAdapter.streamSubscribe)}else{sources[name_2]=driverOutput}}}return sources}function replicateMany(sinks,sinkProxies,streamAdapter){var results=Object.keys(sinks).filter(function(name){return!!sinkProxies[name]}).map(function(name){return streamAdapter.streamSubscribe(sinks[name],{next:function next(x){sinkProxies[name].observer.next(x)},error:function error(err){logToConsoleError(err);sinkProxies[name].observer.error(err)},complete:function complete(x){sinkProxies[name].observer.complete(x)}})});var disposeFunctions=results.filter(function(dispose){return typeof dispose==="function"});return function(){disposeFunctions.forEach(function(dispose){return dispose()})}}function disposeSources(sources){for(var k in sources){if(sources.hasOwnProperty(k)&&sources[k]&&typeof sources[k].dispose==="function"){sources[k].dispose()}}}var isObjectEmpty=function isObjectEmpty(obj){return Object.keys(obj).length===0};function Cycle(main,drivers,options){if(typeof main!=="function"){throw new Error("First argument given to Cycle must be the 'main' "+"function.")}if((typeof drivers==="undefined"?"undefined":_typeof(drivers))!=="object"||drivers===null){throw new Error("Second argument given to Cycle must be an object "+"with driver functions as properties.")}if(isObjectEmpty(drivers)){throw new Error("Second argument given to Cycle must be an object "+"with at least one driver function declared as a property.")}var streamAdapter=options.streamAdapter;if(!streamAdapter||isObjectEmpty(streamAdapter)){throw new Error("Third argument given to Cycle must be an options object "+"with the streamAdapter key supplied with a valid stream adapter.")}var sinkProxies=makeSinkProxies(drivers,streamAdapter);var sources=callDrivers(drivers,sinkProxies,streamAdapter);var sinks=main(sources);if(typeof window!=="undefined"){window.Cyclejs={sinks:sinks}}var run=function run(){var disposeReplication=replicateMany(sinks,sinkProxies,streamAdapter);return function(){disposeSources(sources);disposeReplication()}};return{sinks:sinks,sources:sources,run:run}}Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{}],3:[function(require,module,exports){"use strict";var Rx=require("rx");var RxJSAdapter={adapt:function(originStream,originStreamSubscribe){if(this.isValidStream(originStream)){return originStream}return Rx.Observable.create(function(destinationObserver){var originObserver={next:function(x){return destinationObserver.onNext(x)},error:function(e){return destinationObserver.onError(e)},complete:function(){return destinationObserver.onCompleted()}};var dispose=originStreamSubscribe(originStream,originObserver);return function(){if(typeof dispose==="function"){dispose.call(null)}}})},remember:function(observable){return observable.shareReplay(1)},makeSubject:function(){var stream=new Rx.Subject;var observer={next:function(x){stream.onNext(x)},error:function(err){stream.onError(err)},complete:function(x){stream.onCompleted()}};return{stream:stream,observer:observer}},isValidStream:function(stream){return typeof stream.subscribeOnNext==="function"&&typeof stream.onValue!=="function"},streamSubscribe:function(stream,observer){var subscription=stream.subscribe(function(x){return observer.next(x)},function(e){return observer.error(e)},function(x){return observer.complete(x)});return function(){subscription.dispose()}}};Object.defineProperty(exports,"__esModule",{value:true});exports.default=RxJSAdapter},{rx:undefined}]},{},[1])(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){"use strict";var base_1=require("@cycle/base");var rx_adapter_1=require("@cycle/rx-adapter");function run(main,drivers){var run=base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default}).run;return run()}exports.run=run;var Cycle=function Cycle(main,drivers){return base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default})};Cycle.run=run;Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{"@cycle/base":2,"@cycle/rx-adapter":3}],2:[function(require,module,exports){"use strict";var _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};function logToConsoleError(err){var target=err.stack||err;if(console&&console.error){console.error(target)}else if(console&&console.log){console.log(target)}}function makeSinkProxies(drivers,streamAdapter){var sinkProxies={};for(var name_1 in drivers){if(drivers.hasOwnProperty(name_1)){var holdSubject=streamAdapter.makeSubject();var driverStreamAdapter=drivers[name_1].streamAdapter||streamAdapter;var stream=driverStreamAdapter.adapt(holdSubject.stream,streamAdapter.streamSubscribe);sinkProxies[name_1]={stream:stream,observer:holdSubject.observer}}}return sinkProxies}function callDrivers(drivers,sinkProxies,streamAdapter){var sources={};for(var name_2 in drivers){if(drivers.hasOwnProperty(name_2)){var driverOutput=drivers[name_2](sinkProxies[name_2].stream,streamAdapter,name_2);var driverStreamAdapter=drivers[name_2].streamAdapter;if(driverStreamAdapter&&driverStreamAdapter.isValidStream(driverOutput)){sources[name_2]=streamAdapter.adapt(driverOutput,driverStreamAdapter.streamSubscribe)}else{sources[name_2]=driverOutput}}}return sources}function replicateMany(sinks,sinkProxies,streamAdapter){var results=Object.keys(sinks).filter(function(name){return!!sinkProxies[name]}).map(function(name){return streamAdapter.streamSubscribe(sinks[name],{next:function next(x){sinkProxies[name].observer.next(x)},error:function error(err){logToConsoleError(err);sinkProxies[name].observer.error(err)},complete:function complete(x){sinkProxies[name].observer.complete(x)}})});var disposeFunctions=results.filter(function(dispose){return typeof dispose==="function"});return function(){disposeFunctions.forEach(function(dispose){return dispose()})}}function disposeSources(sources){for(var k in sources){if(sources.hasOwnProperty(k)&&sources[k]&&typeof sources[k].dispose==="function"){sources[k].dispose()}}}var isObjectEmpty=function isObjectEmpty(obj){return Object.keys(obj).length===0};function Cycle(main,drivers,options){if(typeof main!=="function"){throw new Error("First argument given to Cycle must be the 'main' "+"function.")}if((typeof drivers==="undefined"?"undefined":_typeof(drivers))!=="object"||drivers===null){throw new Error("Second argument given to Cycle must be an object "+"with driver functions as properties.")}if(isObjectEmpty(drivers)){throw new Error("Second argument given to Cycle must be an object "+"with at least one driver function declared as a property.")}var streamAdapter=options.streamAdapter;if(!streamAdapter||isObjectEmpty(streamAdapter)){throw new Error("Third argument given to Cycle must be an options object "+"with the streamAdapter key supplied with a valid stream adapter.")}var sinkProxies=makeSinkProxies(drivers,streamAdapter);var sources=callDrivers(drivers,sinkProxies,streamAdapter);var sinks=main(sources);if(typeof window!=="undefined"){window.Cyclejs={sinks:sinks}}var run=function run(){var disposeReplication=replicateMany(sinks,sinkProxies,streamAdapter);return function(){disposeSources(sources);disposeReplication()}};return{sinks:sinks,sources:sources,run:run}}Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{}],3:[function(require,module,exports){"use strict";var Rx=require("rx");var RxJSAdapter={adapt:function(originStream,originStreamSubscribe){if(this.isValidStream(originStream)){return originStream}return Rx.Observable.create(function(destinationObserver){var originObserver={next:function(x){return destinationObserver.onNext(x)},error:function(e){return destinationObserver.onError(e)},complete:function(){return destinationObserver.onCompleted()}};var dispose=originStreamSubscribe(originStream,originObserver);return function(){if(typeof dispose==="function"){dispose.call(null)}}})},remember:function(observable){return observable.shareReplay(1)},makeSubject:function(){var stream=new Rx.Subject;var observer={next:function(x){stream.onNext(x)},error:function(err){stream.onError(err)},complete:function(x){stream.onCompleted()}};return{stream:stream,observer:observer}},isValidStream:function(stream){return typeof stream.subscribeOnNext==="function"&&typeof stream.onValue!=="function"},streamSubscribe:function(stream,observer){var subscription=stream.subscribe(function(x){return observer.next(x)},function(e){return observer.error(e)},function(x){return observer.complete(x)});return function(){subscription.dispose()}}};Object.defineProperty(exports,"__esModule",{value:true});exports.default=RxJSAdapter},{rx:undefined}]},{},[1])(1)}); |
# `Cycle` API | ||
# Cycle Run API with RxJS v4 | ||
- [`run`](#run) | ||
- [`Cycle`](#Cycle) | ||
- [`run`](#run) | ||
### <a id="Cycle"></a> `Cycle(main, drivers)` | ||
### <a id="run"></a> `run(main, drivers)` | ||
A function that prepares the Cycle application to be executed. Takes a `main` | ||
function and prepares to circularly connects it to the given collection of | ||
driver functions. As an output, `Cycle()` returns an object with three | ||
properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
the application actually execute. Refer to the documentation of `run()` for | ||
more details. | ||
Takes a `main` function and circularly connects it to the given collection | ||
of driver functions. | ||
**Example:** | ||
```js | ||
const {sources, sinks, run} = Cycle(main, drivers); | ||
import {run} from '@cycle/rx-run'; | ||
const dispose = Cycle.run(main, drivers); | ||
// ... | ||
const dispose = run(); // Executes the application | ||
// ... | ||
dispose(); | ||
``` | ||
The `main` function expects a collection of "source" Observables (returned | ||
from drivers) as input, and should return a collection of "sink" Observables | ||
(to be given to drivers). A "collection of Observables" is a JavaScript | ||
object where keys match the driver names registered by the `drivers` object, | ||
and values are the Observables. Refer to the documentation of each driver to | ||
see more details on what types of sources it outputs and sinks it receives. | ||
#### Arguments: | ||
@@ -32,27 +34,25 @@ | ||
*(Object)* an object with three properties: `sources`, `sinks` and `run`. `sources` is the collection of driver sources, `sinks` is the | ||
collection of driver sinks, these can be used for debugging or testing. `run` | ||
is the function that once called will execute the application. | ||
*(Function)* a dispose function, used to terminate the execution of the Cycle.js program, cleaning up resources used. | ||
- - - | ||
### <a id="run"></a> `run(main, drivers)` | ||
### <a id="Cycle"></a> `Cycle(main, drivers)` | ||
Takes a `main` function and circularly connects it to the given collection | ||
of driver functions. | ||
A function that prepares the Cycle application to be executed. Takes a `main` | ||
function and prepares to circularly connects it to the given collection of | ||
driver functions. As an output, `Cycle()` returns an object with three | ||
properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
the application actually execute. Refer to the documentation of `run()` for | ||
more details. | ||
**Example:** | ||
```js | ||
const dispose = Cycle.run(main, drivers); | ||
import Cycle from '@cycle/rx-run'; | ||
const {sources, sinks, run} = Cycle(main, drivers); | ||
// ... | ||
const dispose = run(); // Executes the application | ||
// ... | ||
dispose(); | ||
``` | ||
The `main` function expects a collection of "source" Observables (returned | ||
from drivers) as input, and should return a collection of "sink" Observables | ||
(to be given to drivers). A "collection of Observables" is a JavaScript | ||
object where keys match the driver names registered by the `drivers` object, | ||
and values are the Observables. Refer to the documentation of each driver to | ||
see more details on what types of sources it outputs and sinks it receives. | ||
#### Arguments: | ||
@@ -65,3 +65,5 @@ | ||
*(Function)* a dispose function, used to terminate the execution of the Cycle.js program, cleaning up resources used. | ||
*(Object)* an object with three properties: `sources`, `sinks` and `run`. `sources` is the collection of driver sources, `sinks` is the | ||
collection of driver sinks, these can be used for debugging or testing. `run` | ||
is the function that once called will execute the application. | ||
import { CycleSetup, DisposeFunction } from '@cycle/base'; | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
* function and prepares to circularly connects it to the given collection of | ||
* driver functions. As an output, `Cycle()` returns an object with three | ||
* properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
* the application actually execute. Refer to the documentation of `run()` for | ||
* more details. | ||
* | ||
* **Example:** | ||
* ```js | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
* // ... | ||
* const dispose = run(); // Executes the application | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Object} an object with three properties: `sources`, `sinks` and | ||
* `run`. `sources` is the collection of driver sources, `sinks` is the | ||
* collection of driver sinks, these can be used for debugging or testing. `run` | ||
* is the function that once called will execute the application. | ||
* @function Cycle | ||
*/ | ||
declare const Cycle: CycleSetup; | ||
/** | ||
* Takes a `main` function and circularly connects it to the given collection | ||
@@ -36,2 +8,3 @@ * of driver functions. | ||
* ```js | ||
* import {run} from '@cycle/rx-run'; | ||
* const dispose = Cycle.run(main, drivers); | ||
@@ -60,2 +33,31 @@ * // ... | ||
}): DisposeFunction; | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
* function and prepares to circularly connects it to the given collection of | ||
* driver functions. As an output, `Cycle()` returns an object with three | ||
* properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
* the application actually execute. Refer to the documentation of `run()` for | ||
* more details. | ||
* | ||
* **Example:** | ||
* ```js | ||
* import Cycle from '@cycle/rx-run'; | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
* // ... | ||
* const dispose = run(); // Executes the application | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Object} an object with three properties: `sources`, `sinks` and | ||
* `run`. `sources` is the collection of driver sources, `sinks` is the | ||
* collection of driver sinks, these can be used for debugging or testing. `run` | ||
* is the function that once called will execute the application. | ||
* @function Cycle | ||
*/ | ||
declare const Cycle: CycleSetup; | ||
export default Cycle; |
@@ -5,32 +5,2 @@ "use strict"; | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
* function and prepares to circularly connects it to the given collection of | ||
* driver functions. As an output, `Cycle()` returns an object with three | ||
* properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
* the application actually execute. Refer to the documentation of `run()` for | ||
* more details. | ||
* | ||
* **Example:** | ||
* ```js | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
* // ... | ||
* const dispose = run(); // Executes the application | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Object} an object with three properties: `sources`, `sinks` and | ||
* `run`. `sources` is the collection of driver sources, `sinks` is the | ||
* collection of driver sinks, these can be used for debugging or testing. `run` | ||
* is the function that once called will execute the application. | ||
* @function Cycle | ||
*/ | ||
var Cycle = function (main, drivers) { | ||
return base_1.default(main, drivers, { streamAdapter: rx_adapter_1.default }); | ||
}; | ||
/** | ||
* Takes a `main` function and circularly connects it to the given collection | ||
@@ -41,2 +11,3 @@ * of driver functions. | ||
* ```js | ||
* import {run} from '@cycle/rx-run'; | ||
* const dispose = Cycle.run(main, drivers); | ||
@@ -67,2 +38,33 @@ * // ... | ||
exports.run = run; | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
* function and prepares to circularly connects it to the given collection of | ||
* driver functions. As an output, `Cycle()` returns an object with three | ||
* properties: `sources`, `sinks` and `run`. Only when `run()` is called will | ||
* the application actually execute. Refer to the documentation of `run()` for | ||
* more details. | ||
* | ||
* **Example:** | ||
* ```js | ||
* import Cycle from '@cycle/rx-run'; | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
* // ... | ||
* const dispose = run(); // Executes the application | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Object} an object with three properties: `sources`, `sinks` and | ||
* `run`. `sources` is the collection of driver sources, `sinks` is the | ||
* collection of driver sinks, these can be used for debugging or testing. `run` | ||
* is the function that once called will execute the application. | ||
* @function Cycle | ||
*/ | ||
var Cycle = function (main, drivers) { | ||
return base_1.default(main, drivers, { streamAdapter: rx_adapter_1.default }); | ||
}; | ||
Cycle.run = run; | ||
@@ -69,0 +71,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
{ | ||
"name": "@cycle/core", | ||
"version": "7.0.0-rc8", | ||
"version": "7.0.0-rc9", | ||
"author": "Andre Staltz", | ||
@@ -5,0 +5,0 @@ "description": "The Cycle run() function meant to be used with RxJS v4", |
@@ -77,2 +77,10 @@ (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){ | ||
function logToConsoleError(err) { | ||
var target = err.stack || err; | ||
if (console && console.error) { | ||
console.error(target); | ||
} else if (console && console.log) { | ||
console.log(target); | ||
} | ||
} | ||
function makeSinkProxies(drivers, streamAdapter) { | ||
@@ -112,3 +120,14 @@ var sinkProxies = {}; | ||
}).map(function (name) { | ||
return streamAdapter.streamSubscribe(sinks[name], sinkProxies[name].observer); | ||
return streamAdapter.streamSubscribe(sinks[name], { | ||
next: function next(x) { | ||
sinkProxies[name].observer.next(x); | ||
}, | ||
error: function error(err) { | ||
logToConsoleError(err); | ||
sinkProxies[name].observer.error(err); | ||
}, | ||
complete: function complete(x) { | ||
sinkProxies[name].observer.complete(x); | ||
} | ||
}); | ||
}); | ||
@@ -157,3 +176,2 @@ var disposeFunctions = results.filter(function (dispose) { | ||
return function () { | ||
streamAdapter.dispose(sinks, sinkProxies, sources); | ||
disposeSources(sources); | ||
@@ -172,11 +190,2 @@ disposeReplication(); | ||
var Rx = require('rx'); | ||
function logToConsoleError(err) { | ||
var target = err.stack || err; | ||
if (console && console.error) { | ||
console.error(target); | ||
} | ||
else if (console && console.log) { | ||
console.log(target); | ||
} | ||
} | ||
var RxJSAdapter = { | ||
@@ -201,11 +210,4 @@ adapt: function (originStream, originStreamSubscribe) { | ||
}, | ||
dispose: function (sinks, sinkProxies, sources) { | ||
Object.keys(sources).forEach(function (k) { | ||
if (typeof sources[k].dispose === 'function') { | ||
sources[k].dispose(); | ||
} | ||
}); | ||
Object.keys(sinkProxies).forEach(function (k) { | ||
sinkProxies[k].observer.complete(); | ||
}); | ||
remember: function (observable) { | ||
return observable.shareReplay(1); | ||
}, | ||
@@ -216,7 +218,4 @@ makeSubject: function () { | ||
next: function (x) { stream.onNext(x); }, | ||
error: function (err) { | ||
logToConsoleError(err); | ||
stream.onError(err); | ||
}, | ||
complete: function (x) { stream.onCompleted(); }, | ||
error: function (err) { stream.onError(err); }, | ||
complete: function (x) { stream.onCompleted(); } | ||
}; | ||
@@ -230,7 +229,7 @@ return { stream: stream, observer: observer }; | ||
streamSubscribe: function (stream, observer) { | ||
var subscription = stream.subscribe(function (x) { return observer.next(x); }, function (e) { return observer.error(e); }, function () { return observer.complete(); }); | ||
var subscription = stream.subscribe(function (x) { return observer.next(x); }, function (e) { return observer.error(e); }, function (x) { return observer.complete(x); }); | ||
return function () { | ||
subscription.dispose(); | ||
}; | ||
}, | ||
} | ||
}; | ||
@@ -237,0 +236,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -1,1 +0,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){"use strict";var base_1=require("@cycle/base");var rx_adapter_1=require("@cycle/rx-adapter");var Cycle=function Cycle(main,drivers){return base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default})};function run(main,drivers){var run=base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default}).run;return run()}exports.run=run;Cycle.run=run;Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{"@cycle/base":2,"@cycle/rx-adapter":3}],2:[function(require,module,exports){"use strict";var _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};function makeSinkProxies(drivers,streamAdapter){var sinkProxies={};for(var name_1 in drivers){if(drivers.hasOwnProperty(name_1)){var holdSubject=streamAdapter.makeSubject();var driverStreamAdapter=drivers[name_1].streamAdapter||streamAdapter;var stream=driverStreamAdapter.adapt(holdSubject.stream,streamAdapter.streamSubscribe);sinkProxies[name_1]={stream:stream,observer:holdSubject.observer}}}return sinkProxies}function callDrivers(drivers,sinkProxies,streamAdapter){var sources={};for(var name_2 in drivers){if(drivers.hasOwnProperty(name_2)){var driverOutput=drivers[name_2](sinkProxies[name_2].stream,streamAdapter,name_2);var driverStreamAdapter=drivers[name_2].streamAdapter;if(driverStreamAdapter&&driverStreamAdapter.isValidStream(driverOutput)){sources[name_2]=streamAdapter.adapt(driverOutput,driverStreamAdapter.streamSubscribe)}else{sources[name_2]=driverOutput}}}return sources}function replicateMany(sinks,sinkProxies,streamAdapter){var results=Object.keys(sinks).filter(function(name){return!!sinkProxies[name]}).map(function(name){return streamAdapter.streamSubscribe(sinks[name],sinkProxies[name].observer)});var disposeFunctions=results.filter(function(dispose){return typeof dispose==="function"});return function(){disposeFunctions.forEach(function(dispose){return dispose()})}}function disposeSources(sources){for(var k in sources){if(sources.hasOwnProperty(k)&&sources[k]&&typeof sources[k].dispose==="function"){sources[k].dispose()}}}var isObjectEmpty=function isObjectEmpty(obj){return Object.keys(obj).length===0};function Cycle(main,drivers,options){if(typeof main!=="function"){throw new Error("First argument given to Cycle must be the 'main' "+"function.")}if((typeof drivers==="undefined"?"undefined":_typeof(drivers))!=="object"||drivers===null){throw new Error("Second argument given to Cycle must be an object "+"with driver functions as properties.")}if(isObjectEmpty(drivers)){throw new Error("Second argument given to Cycle must be an object "+"with at least one driver function declared as a property.")}var streamAdapter=options.streamAdapter;if(!streamAdapter||isObjectEmpty(streamAdapter)){throw new Error("Third argument given to Cycle must be an options object "+"with the streamAdapter key supplied with a valid stream adapter.")}var sinkProxies=makeSinkProxies(drivers,streamAdapter);var sources=callDrivers(drivers,sinkProxies,streamAdapter);var sinks=main(sources);if(typeof window!=="undefined"){window.Cyclejs={sinks:sinks}}var run=function run(){var disposeReplication=replicateMany(sinks,sinkProxies,streamAdapter);return function(){streamAdapter.dispose(sinks,sinkProxies,sources);disposeSources(sources);disposeReplication()}};return{sinks:sinks,sources:sources,run:run}}Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{}],3:[function(require,module,exports){"use strict";var Rx=require("rx");function logToConsoleError(err){var target=err.stack||err;if(console&&console.error){console.error(target)}else if(console&&console.log){console.log(target)}}var RxJSAdapter={adapt:function(originStream,originStreamSubscribe){if(this.isValidStream(originStream)){return originStream}return Rx.Observable.create(function(destinationObserver){var originObserver={next:function(x){return destinationObserver.onNext(x)},error:function(e){return destinationObserver.onError(e)},complete:function(){return destinationObserver.onCompleted()}};var dispose=originStreamSubscribe(originStream,originObserver);return function(){if(typeof dispose==="function"){dispose.call(null)}}})},dispose:function(sinks,sinkProxies,sources){Object.keys(sources).forEach(function(k){if(typeof sources[k].dispose==="function"){sources[k].dispose()}});Object.keys(sinkProxies).forEach(function(k){sinkProxies[k].observer.complete()})},makeSubject:function(){var stream=new Rx.Subject;var observer={next:function(x){stream.onNext(x)},error:function(err){logToConsoleError(err);stream.onError(err)},complete:function(x){stream.onCompleted()}};return{stream:stream,observer:observer}},isValidStream:function(stream){return typeof stream.subscribeOnNext==="function"&&typeof stream.onValue!=="function"},streamSubscribe:function(stream,observer){var subscription=stream.subscribe(function(x){return observer.next(x)},function(e){return observer.error(e)},function(){return observer.complete()});return function(){subscription.dispose()}}};Object.defineProperty(exports,"__esModule",{value:true});exports.default=RxJSAdapter},{rx:undefined}]},{},[1])(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){"use strict";var base_1=require("@cycle/base");var rx_adapter_1=require("@cycle/rx-adapter");var Cycle=function Cycle(main,drivers){return base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default})};function run(main,drivers){var run=base_1.default(main,drivers,{streamAdapter:rx_adapter_1.default}).run;return run()}exports.run=run;Cycle.run=run;Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{"@cycle/base":2,"@cycle/rx-adapter":3}],2:[function(require,module,exports){"use strict";var _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};function logToConsoleError(err){var target=err.stack||err;if(console&&console.error){console.error(target)}else if(console&&console.log){console.log(target)}}function makeSinkProxies(drivers,streamAdapter){var sinkProxies={};for(var name_1 in drivers){if(drivers.hasOwnProperty(name_1)){var holdSubject=streamAdapter.makeSubject();var driverStreamAdapter=drivers[name_1].streamAdapter||streamAdapter;var stream=driverStreamAdapter.adapt(holdSubject.stream,streamAdapter.streamSubscribe);sinkProxies[name_1]={stream:stream,observer:holdSubject.observer}}}return sinkProxies}function callDrivers(drivers,sinkProxies,streamAdapter){var sources={};for(var name_2 in drivers){if(drivers.hasOwnProperty(name_2)){var driverOutput=drivers[name_2](sinkProxies[name_2].stream,streamAdapter,name_2);var driverStreamAdapter=drivers[name_2].streamAdapter;if(driverStreamAdapter&&driverStreamAdapter.isValidStream(driverOutput)){sources[name_2]=streamAdapter.adapt(driverOutput,driverStreamAdapter.streamSubscribe)}else{sources[name_2]=driverOutput}}}return sources}function replicateMany(sinks,sinkProxies,streamAdapter){var results=Object.keys(sinks).filter(function(name){return!!sinkProxies[name]}).map(function(name){return streamAdapter.streamSubscribe(sinks[name],{next:function next(x){sinkProxies[name].observer.next(x)},error:function error(err){logToConsoleError(err);sinkProxies[name].observer.error(err)},complete:function complete(x){sinkProxies[name].observer.complete(x)}})});var disposeFunctions=results.filter(function(dispose){return typeof dispose==="function"});return function(){disposeFunctions.forEach(function(dispose){return dispose()})}}function disposeSources(sources){for(var k in sources){if(sources.hasOwnProperty(k)&&sources[k]&&typeof sources[k].dispose==="function"){sources[k].dispose()}}}var isObjectEmpty=function isObjectEmpty(obj){return Object.keys(obj).length===0};function Cycle(main,drivers,options){if(typeof main!=="function"){throw new Error("First argument given to Cycle must be the 'main' "+"function.")}if((typeof drivers==="undefined"?"undefined":_typeof(drivers))!=="object"||drivers===null){throw new Error("Second argument given to Cycle must be an object "+"with driver functions as properties.")}if(isObjectEmpty(drivers)){throw new Error("Second argument given to Cycle must be an object "+"with at least one driver function declared as a property.")}var streamAdapter=options.streamAdapter;if(!streamAdapter||isObjectEmpty(streamAdapter)){throw new Error("Third argument given to Cycle must be an options object "+"with the streamAdapter key supplied with a valid stream adapter.")}var sinkProxies=makeSinkProxies(drivers,streamAdapter);var sources=callDrivers(drivers,sinkProxies,streamAdapter);var sinks=main(sources);if(typeof window!=="undefined"){window.Cyclejs={sinks:sinks}}var run=function run(){var disposeReplication=replicateMany(sinks,sinkProxies,streamAdapter);return function(){disposeSources(sources);disposeReplication()}};return{sinks:sinks,sources:sources,run:run}}Object.defineProperty(exports,"__esModule",{value:true});exports.default=Cycle},{}],3:[function(require,module,exports){"use strict";var Rx=require("rx");var RxJSAdapter={adapt:function(originStream,originStreamSubscribe){if(this.isValidStream(originStream)){return originStream}return Rx.Observable.create(function(destinationObserver){var originObserver={next:function(x){return destinationObserver.onNext(x)},error:function(e){return destinationObserver.onError(e)},complete:function(){return destinationObserver.onCompleted()}};var dispose=originStreamSubscribe(originStream,originObserver);return function(){if(typeof dispose==="function"){dispose.call(null)}}})},remember:function(observable){return observable.shareReplay(1)},makeSubject:function(){var stream=new Rx.Subject;var observer={next:function(x){stream.onNext(x)},error:function(err){stream.onError(err)},complete:function(x){stream.onCompleted()}};return{stream:stream,observer:observer}},isValidStream:function(stream){return typeof stream.subscribeOnNext==="function"&&typeof stream.onValue!=="function"},streamSubscribe:function(stream,observer){var subscription=stream.subscribe(function(x){return observer.next(x)},function(e){return observer.error(e)},function(x){return observer.complete(x)});return function(){subscription.dispose()}}};Object.defineProperty(exports,"__esModule",{value:true});exports.default=RxJSAdapter},{rx:undefined}]},{},[1])(1)}); |
{ | ||
"name": "@cycle/rx-run", | ||
"version": "7.0.0-rc7", | ||
"version": "7.0.0-rc8", | ||
"author": "Andre Staltz", | ||
@@ -25,4 +25,4 @@ "description": "The Cycle run() function meant to be used with RxJS v4", | ||
"dependencies": { | ||
"@cycle/base": "^3.0.0", | ||
"@cycle/rx-adapter": "^2.0.0" | ||
"@cycle/base": "^4.0.0", | ||
"@cycle/rx-adapter": "^3.0.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "peerDependencies": { |
@@ -19,5 +19,5 @@ 'use strict'; | ||
generateDocs({ | ||
src: './src/index.ts', | ||
src: './lib/index.js', | ||
output: './docs/api.md', | ||
template: './scripts/cycle-docs-template.md.ejs' | ||
}); |
@@ -10,2 +10,35 @@ import { | ||
/** | ||
* Takes a `main` function and circularly connects it to the given collection | ||
* of driver functions. | ||
* | ||
* **Example:** | ||
* ```js | ||
* import {run} from '@cycle/rx-run'; | ||
* const dispose = Cycle.run(main, drivers); | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* The `main` function expects a collection of "source" Observables (returned | ||
* from drivers) as input, and should return a collection of "sink" Observables | ||
* (to be given to drivers). A "collection of Observables" is a JavaScript | ||
* object where keys match the driver names registered by the `drivers` object, | ||
* and values are the Observables. Refer to the documentation of each driver to | ||
* see more details on what types of sources it outputs and sinks it receives. | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Function} a dispose function, used to terminate the execution of the | ||
* Cycle.js program, cleaning up resources used. | ||
* @function run | ||
*/ | ||
export function run<Sources, Sinks>(main: (sources: Sources) => Sinks, | ||
drivers: {[name: string]: Function}): DisposeFunction { | ||
const {run} = CycleBase(main, drivers, {streamAdapter: RxAdapter}); | ||
return run(); | ||
} | ||
/** | ||
* A function that prepares the Cycle application to be executed. Takes a `main` | ||
@@ -20,2 +53,3 @@ * function and prepares to circularly connects it to the given collection of | ||
* ```js | ||
* import Cycle from '@cycle/rx-run'; | ||
* const {sources, sinks, run} = Cycle(main, drivers); | ||
@@ -44,36 +78,4 @@ * // ... | ||
/** | ||
* Takes a `main` function and circularly connects it to the given collection | ||
* of driver functions. | ||
* | ||
* **Example:** | ||
* ```js | ||
* const dispose = Cycle.run(main, drivers); | ||
* // ... | ||
* dispose(); | ||
* ``` | ||
* | ||
* The `main` function expects a collection of "source" Observables (returned | ||
* from drivers) as input, and should return a collection of "sink" Observables | ||
* (to be given to drivers). A "collection of Observables" is a JavaScript | ||
* object where keys match the driver names registered by the `drivers` object, | ||
* and values are the Observables. Refer to the documentation of each driver to | ||
* see more details on what types of sources it outputs and sinks it receives. | ||
* | ||
* @param {Function} main a function that takes `sources` as input | ||
* and outputs a collection of `sinks` Observables. | ||
* @param {Object} drivers an object where keys are driver names and values | ||
* are driver functions. | ||
* @return {Function} a dispose function, used to terminate the execution of the | ||
* Cycle.js program, cleaning up resources used. | ||
* @function run | ||
*/ | ||
export function run<Sources, Sinks>(main: (sources: Sources) => Sinks, | ||
drivers: {[name: string]: Function}): DisposeFunction { | ||
const {run} = CycleBase(main, drivers, {streamAdapter: RxAdapter}); | ||
return run(); | ||
} | ||
Cycle.run = run; | ||
export default Cycle; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
92148
0.33%1372
0.51%