UNPKG

rxact

Version:

an observable application management for Javascript apps

63 lines (45 loc) 2.29 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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); } } import StateStream from './index'; import isObservable from '../utils/isObservable'; function combineStateStreams(state$, streamName, stateStreams) { var _this = this; if (!stateStreams || stateStreams.length === 0) { return state$; } var streamNames = {}; var streams = [].concat(_toConsumableArray(stateStreams), [{ streamName: streamName, state$: state$ }]); streams.forEach(function (stream) { if (!(stream instanceof StateStream) && (!isObservable(stream.state$) || !stream.streamName)) { throw new Error('Expected the element of stateStreams to be instance of StateStream.'); } streamNames[stream.streamName] = true; }); if (Object.keys(streamNames).length !== streams.length) { throw new Error('StateStreams\' name should be unique.'); } var combinedState$ = new this.Observable(function (observer) { var currentState = {}; var subscriptions = streams.map(function (source) { return source.state$.subscribe(function (state) { var sourceName = source.streamName; var nextState = _extends({}, currentState, _defineProperty({}, sourceName, state)); currentState = nextState; if (Object.keys(currentState).length === streams.length) { observer.next(currentState); } }); }); _this.subscriptions = _this.subscriptions.concat(subscriptions); return { unsubscribe: function unsubscribe() { subscriptions.forEach(function (subscription) { subscription.unsubscribe(); }); } }; }); return combinedState$; } export default combineStateStreams;