cyclejs-stream
Version:
Observable (events stream) to which you can inject another streams.
134 lines (97 loc) • 6.1 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
exports['default'] = createInjectableStream;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }
require('core-js/fn/object/entries');
require('core-js/fn/array/from');
var _cycleCore = require('@cycle/core');
var _inputProxy = require('./input-proxy');
var _inputProxy2 = _interopRequireDefault(_inputProxy);
function _throwIfNotObservable(thing, msg) {
if (!(thing instanceof _cycleCore.Rx.Observable)) {
throw new TypeError(msg);
}
}
function _passToProxies(sources, proxies, disposable) {
sources.forEach(function (source, index) {
_throwIfNotObservable(source, 'Only Observables can be injected to Stream,\n but argument ' + index + ' does not look to be one');
var proxy = proxies[index];
if (!proxy.wasPassed) {
var subscription = proxy.pass(source);
disposable.add(subscription);
}
});
}
var InjectableStream = (function (_Rx$ReplaySubject) {
_inherits(InjectableStream, _Rx$ReplaySubject);
function InjectableStream(definitionFn) {
_classCallCheck(this, InjectableStream);
_get(Object.getPrototypeOf(InjectableStream.prototype), 'constructor', this).call(this, 1);
if ('function' !== typeof definitionFn) {
throw new TypeError('the first argument must be a function!');
}
this._definitionFn = definitionFn;
this._wasInjected = false;
this._wasSubscribed = false;
this._proxies = Array.from(new Array(this._definitionFn.length), function () {
return new _inputProxy2['default']();
});
this._subscriptions = new _cycleCore.Rx.CompositeDisposable();
}
_createClass(InjectableStream, [{
key: 'dispose',
value: function dispose() {
_get(Object.getPrototypeOf(InjectableStream.prototype), 'dispose', this).call(this);
this._subscriptions.dispose();
}
}, {
key: 'inject',
value: function inject() {
if (this._wasInjected) {
throw new Error('you can inject only once! (do it wisely, then)');
}
for (var _len = arguments.length, dependencies = Array(_len), _key = 0; _key < _len; _key++) {
dependencies[_key] = arguments[_key];
}
if (dependencies.length !== this._definitionFn.length) {
throw new Error('stream requires ' + this._definitionFn.length + ' dependencies,\n but ' + dependencies.length + ' have been provided');
}
this._dependencies = dependencies;
this._wasInjected = true;
if (this._wasSubscribed) {
_passToProxies(this._dependencies, this._proxies, this._subscriptions);
}
return this;
}
}, {
key: 'subscribe',
value: function subscribe() {
if (!this._wasSubscribed) {
var source$ = this._definitionFn.apply(this, _toConsumableArray(this._proxies));
_throwIfNotObservable(source$, 'Stream definition function must return an Observable.');
var subscription = source$.subscribe(this.asObserver());
this._subscriptions.add(subscription);
this._wasSubscribed = true;
if (this._wasInjected) {
_passToProxies(this._dependencies, this._proxies, this._subscriptions);
}
}
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return _get(Object.getPrototypeOf(InjectableStream.prototype), 'subscribe', this).apply(this, args);
}
}]);
return InjectableStream;
})(_cycleCore.Rx.ReplaySubject);
function createInjectableStream(fn) {
return new InjectableStream(fn);
}
module.exports = exports['default'];