UNPKG

@cycle/state

Version:

Wraps your Cycle.js main function with reducer-driven state management

161 lines 6.68 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var xstream_1 = require("xstream"); var adapt_1 = require("@cycle/run/lib/adapt"); var isolate_1 = require("@cycle/isolate"); var pickMerge_1 = require("./pickMerge"); var pickCombine_1 = require("./pickCombine"); /** * An object representing all instances in a collection of components. Has the * methods pickCombine and pickMerge to get the combined sinks of all instances. */ var Instances = /** @class */ (function () { function Instances(instances$) { this._instances$ = instances$; } /** * Like `merge` in xstream, this operator blends multiple streams together, but * picks those streams from a collection of component instances. * * Use the `selector` string to pick a stream from the sinks object of each * component instance, then pickMerge will merge all those picked streams. * * @param {String} selector a name of a channel in a sinks object belonging to * each component in the collection of components. * @return {Function} an operator to be used with xstream's `compose` method. */ Instances.prototype.pickMerge = function (selector) { return adapt_1.adapt(this._instances$.compose(pickMerge_1.pickMerge(selector))); }; /** * Like `combine` in xstream, this operator combines multiple streams together, * but picks those streams from a collection of component instances. * * Use the `selector` string to pick a stream from the sinks object of each * component instance, then pickCombine will combine all those picked streams. * * @param {String} selector a name of a channel in a sinks object belonging to * each component in the collection of components. * @return {Function} an operator to be used with xstream's `compose` method. */ Instances.prototype.pickCombine = function (selector) { return adapt_1.adapt(this._instances$.compose(pickCombine_1.pickCombine(selector))); }; return Instances; }()); exports.Instances = Instances; function defaultItemScope(key) { return { '*': null }; } function instanceLens(itemKey, key) { return { get: function (arr) { if (typeof arr === 'undefined') { return void 0; } else { for (var i = 0, n = arr.length; i < n; ++i) { if ("" + itemKey(arr[i], i) === key) { return arr[i]; } } return void 0; } }, set: function (arr, item) { if (typeof arr === 'undefined') { return [item]; } else if (typeof item === 'undefined') { return arr.filter(function (s, i) { return "" + itemKey(s, i) !== key; }); } else { return arr.map(function (s, i) { if ("" + itemKey(s, i) === key) { return item; } else { return s; } }); } }, }; } var identityLens = { get: function (outer) { return outer; }, set: function (outer, inner) { return inner; }, }; function makeCollection(opts) { return function collectionComponent(sources) { var name = opts.channel || 'state'; var itemKey = opts.itemKey; var itemScope = opts.itemScope || defaultItemScope; var state$ = xstream_1.default.fromObservable(sources[name].stream); var instances$ = state$.fold(function (acc, nextState) { var _a, _b, _c, _d; var dict = acc.dict; if (Array.isArray(nextState)) { var nextInstArray = Array(nextState.length); var nextKeys_1 = new Set(); // add for (var i = 0, n = nextState.length; i < n; ++i) { var key = "" + (itemKey ? itemKey(nextState[i], i) : i); nextKeys_1.add(key); if (!dict.has(key)) { var stateScope = itemKey ? instanceLens(itemKey, key) : "" + i; var otherScopes = itemScope(key); var scopes = typeof otherScopes === 'string' ? (_a = { '*': otherScopes }, _a[name] = stateScope, _a) : __assign({}, otherScopes, (_b = {}, _b[name] = stateScope, _b)); var itemComp = opts.itemFactory ? opts.itemFactory(nextState[i], i) : opts.item; var sinks = isolate_1.default(itemComp, scopes)(sources); dict.set(key, sinks); nextInstArray[i] = sinks; } else { nextInstArray[i] = dict.get(key); } nextInstArray[i]._key = key; } // remove dict.forEach(function (_, key) { if (!nextKeys_1.has(key)) { dict.delete(key); } }); nextKeys_1.clear(); return { dict: dict, arr: nextInstArray }; } else { dict.clear(); var key = "" + (itemKey ? itemKey(nextState, 0) : 'this'); var stateScope = identityLens; var otherScopes = itemScope(key); var scopes = typeof otherScopes === 'string' ? (_c = { '*': otherScopes }, _c[name] = stateScope, _c) : __assign({}, otherScopes, (_d = {}, _d[name] = stateScope, _d)); var itemComp = opts.itemFactory ? opts.itemFactory(nextState, 0) : opts.item; var sinks = isolate_1.default(itemComp, scopes)(sources); dict.set(key, sinks); return { dict: dict, arr: [sinks] }; } }, { dict: new Map(), arr: [] }); return opts.collectSinks(new Instances(instances$)); }; } exports.makeCollection = makeCollection; //# sourceMappingURL=Collection.js.map