rxact
Version:
an observable application management for Javascript apps
116 lines (89 loc) • 3.87 kB
JavaScript
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; }; }();
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"); } }
import { getObservable } from '../observable';
import stateFactory from './stateFactory';
import combineStateStreams from './combineStateSteams';
import eventRunnerFactory from './eventRunnerFactory';
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 = getObservable();
this.state$ = stateFactory.call(this, initialState);
this.state$ = combineStateStreams.call(this, this.state$, streamName, stateStreams);
this.eventRunner = eventRunnerFactory(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;
};
export default StateStream;