UNPKG

rxact

Version:

an observable application management for Javascript apps

133 lines (97 loc) 4.32 kB
'use strict'; 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 _observable = require('../observable'); var _stateFactory = require('./stateFactory'); var _stateFactory2 = _interopRequireDefault(_stateFactory); var _combineStateSteams = require('./combineStateSteams'); var _combineStateSteams2 = _interopRequireDefault(_combineStateSteams); var _eventRunnerFactory = require('./eventRunnerFactory'); var _eventRunnerFactory2 = _interopRequireDefault(_eventRunnerFactory); 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"); } } var StateStream = function () { function StateStream(streamName, initialState, stateStreams) { var _this = this; _classCallCheck(this, StateStream); this.emitters = {}; this.subscriptions = []; this.observers = []; this.dispose = function () { _this.subscriptions.forEach(function (subscription) { subscription.unsubscribe(); }); }; this.installPlugins = function () { var proxy = _this; StateStream.plugins.forEach(function (plugin) { if (typeof plugin !== 'function') { throw new Error('Expected plugin to be a function.'); } var pluginProxy = plugin(proxy); if (pluginProxy) { proxy = pluginProxy; } }); return proxy; }; if (typeof streamName !== 'string' || !streamName) { throw new Error('Expected the streamName to be a not none string.'); } this.streamName = streamName; this.Observable = (0, _observable.getObservable)(); this.state$ = _stateFactory2.default.call(this, initialState); this.state$ = _combineStateSteams2.default.call(this, this.state$, streamName, stateStreams); this.eventRunner = (0, _eventRunnerFactory2.default)(this.Observable, this.getState); return this.installPlugins(); } _createClass(StateStream, [{ key: 'emitter', value: function emitter(name, updater) { var _this2 = this; if (!name) { throw new Error('emitter(): name should not be blank.'); } if (typeof updater !== 'function') { throw new Error('emitter(): expect second parameter to be a function.'); } // $flow-ignore this[name] = function () { _this2.next(updater.apply(undefined, arguments)); }; // $flow-ignore this.emitters[name] = this[name]; } }]); return StateStream; }(); StateStream.plugins = []; StateStream.addPlugin = function () { for (var _len = arguments.length, plugins = Array(_len), _key = 0; _key < _len; _key++) { plugins[_key] = arguments[_key]; } plugins.forEach(function (plugin) { if (typeof plugin !== 'function') { throw new Error('Expected plugin to be a function.'); } }); StateStream.plugins = [].concat(_toConsumableArray(StateStream.plugins), plugins); }; StateStream.removePlugin = function () { for (var _len2 = arguments.length, plugins = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { plugins[_key2] = arguments[_key2]; } var finalPlugins = []; if (plugins.length !== 0) { finalPlugins = StateStream.plugins.filter(function (plugin) { return !plugins.find(function (removedPlugin) { return removedPlugin === plugin; }); }); } StateStream.plugins = finalPlugins; }; exports.default = StateStream;