UNPKG

rxdeep

Version:
160 lines 6.92 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 }); exports.keyed = exports.KeyedState = void 0; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var state_1 = require("./state"); var changes_1 = require("./types/changes"); var watcher_1 = require("./util/watcher"); var trace_1 = require("./trace"); var KeyedState = /** @class */ (function (_super) { __extends(KeyedState, _super); function KeyedState(state, keyfunc) { var _this = _super.call(this, function (observer) { return _this._changes.pipe(operators_1.map(function (_a) { var change = _a[0], _ = _a[1]; return change.value || []; }), operators_1.startWith(_this.value)).subscribe(observer); }) || this; _this.state = state; _this.keyfunc = keyfunc; _this._value = []; _this._watcher = new watcher_1.Watcher(state.value, keyfunc); _this._value = state.value || []; _this._changesub = new rxjs_1.Subject(); _this._changes = _this.state.downstream.pipe(operators_1.map(function (change) { return [change, _this._watcher.changes(change.value)]; }), operators_1.map(function (_a) { var change = _a[0], listChanges = _a[1]; if (listChanges.moves.length > 0 && !changes_1.isLeaf(change.trace)) { var mapping = listChanges.moves.reduce(function (total, move) { total[move.oldIndex] = move.newIndex; return total; }, {}); var _tr_1 = { subs: __assign({}, change.trace.subs) }; Object.entries(mapping).forEach(function (_a) { var src = _a[0], dest = _a[1]; var subtrace = trace_1.trace(_this._value[src], change.value[dest]); if (subtrace) { _tr_1.subs[dest] = subtrace; } else { delete _tr_1.subs[dest]; } }); return [{ value: change.value, trace: _tr_1 }, listChanges]; } return [change, listChanges]; }), operators_1.tap(function (_a) { var change = _a[0]; _this._value = change.value || []; }), operators_1.multicast(function () { return _this._changesub; }), operators_1.refCount()); return _this; } KeyedState.prototype.next = function (t) { this.state.upstream.next({ value: t, trace: { from: this.value, to: t } }); }; KeyedState.prototype.error = function (err) { this.state.upstream.error(err); }; KeyedState.prototype.complete = function () { this._changesub.complete(); }; Object.defineProperty(KeyedState.prototype, "value", { get: function () { return this._value; }, set: function (t) { this.next(t); }, enumerable: false, configurable: true }); KeyedState.prototype.key = function (key) { var _a; var sub = new state_1.State((_a = this._watcher.keymap[key]) === null || _a === void 0 ? void 0 : _a.item, this.keyDownstream(key, function () { return sub.value; }), this.keyUpstream(key)); return sub; }; KeyedState.prototype.keyDownstream = function (key, current) { var _this = this; return this._changes.pipe(operators_1.map(function (_a) { var change = _a[0], _ = _a[1]; return ({ trace: change.trace, entry: _this._watcher.keymap[key], }); }), operators_1.filter(function (change) { /* istanbul ignore next */ if (changes_1.isLeaf(change.trace)) { return current() !== change.entry.item; } else { return (!change.entry && !!current()) || (change.entry && change.entry.index in change.trace.subs); } }), operators_1.map(function (change) { var _a; return ({ value: (_a = change.entry) === null || _a === void 0 ? void 0 : _a.item, trace: changes_1.isLeaf(change.trace) || !change.entry ? undefined : change.trace.subs[change.entry.index] }); })); }; KeyedState.prototype.keyUpstream = function (key) { var _this = this; return { next: function (change) { var _a; var entry = _this._watcher.keymap[key]; _this._value[entry.index] = change.value; _this.state.upstream.next({ value: _this._value, trace: { subs: (_a = {}, _a[entry.index] = change.trace, _a) } }); }, error: function (err) { return _this.state.upstream.error(err); }, complete: function () { }, }; }; KeyedState.prototype.index = function (key) { var _this = this; var _a; return this._changes.pipe(operators_1.map(function () { var _a; return (_a = _this._watcher.keymap[key]) === null || _a === void 0 ? void 0 : _a.index; }), operators_1.startWith((_a = this._watcher.keymap[key]) === null || _a === void 0 ? void 0 : _a.index), operators_1.distinctUntilChanged()); }; KeyedState.prototype.changes = function () { return this._changes.pipe(operators_1.map(function (_a) { var _ = _a[0], listChanges = _a[1]; return listChanges; })); }; return KeyedState; }(rxjs_1.Observable)); exports.KeyedState = KeyedState; function keyed(state, keyfunc) { return new KeyedState(state, keyfunc); } exports.keyed = keyed; //# sourceMappingURL=keyed.js.map