rxdeep
Version:
RxJS deep state management
96 lines • 4.07 kB
JavaScript
;
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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.state = exports.State = void 0;
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var changes_1 = require("./types/changes");
var post_trace_1 = require("./util/post-trace");
var State = /** @class */ (function (_super) {
__extends(State, _super);
function State(initial, downstream, upstream) {
if (downstream === void 0) { downstream = new rxjs_1.Subject(); }
var _this = _super.call(this, function (observer) {
observer.next(_this.value);
return _this.downstream.pipe(operators_1.map(function (change) { return change.value; })).subscribe(observer);
}) || this;
_this._value = initial;
_this._changesub = new rxjs_1.Subject();
_this.downstream = downstream.pipe(post_trace_1.postTrace(), operators_1.tap(function (change) {
if (change.value !== _this._value) {
_this._value = change.value;
}
}), operators_1.multicast(function () { return _this._changesub; }), operators_1.refCount());
_this.upstream = upstream || downstream;
return _this;
}
State.prototype.next = function (t) { this.upstream.next({ value: t, trace: { from: this.value, to: t } }); };
State.prototype.error = function (err) { this.upstream.error(err); };
State.prototype.complete = function () { this.upstream.complete(); this._changesub.complete(); };
Object.defineProperty(State.prototype, "value", {
get: function () { return this._value; },
set: function (t) { this.next(t); },
enumerable: false,
configurable: true
});
State.prototype.sub = function (key) {
var _sub = new State(this.value ? this.value[key] : undefined, this.subDownstream(key, function () { return _sub.value; }), this.subUpstream(key));
return _sub;
};
State.prototype.subDownstream = function (key, current) {
return this.downstream.pipe(operators_1.map(function (change) { return ({
value: change.value ? change.value[key] : undefined,
trace: change.trace,
}); }), operators_1.filter(function (change) {
if (changes_1.isLeaf(change.trace)) {
return current() !== change.value;
}
else {
return key in change.trace.subs;
}
}), operators_1.map(function (change) { return ({
value: change.value,
trace: changes_1.isLeaf(change.trace) ? undefined : change.trace.subs[key]
}); }));
};
State.prototype.subUpstream = function (key) {
var _this = this;
return {
next: function (change) {
var _a;
if (_this.value) {
_this.value[key] = change.value;
}
_this.upstream.next({
value: _this.value,
trace: {
subs: (_a = {},
_a[key] = change.trace,
_a)
}
});
},
error: function (err) { return _this.upstream.error(err); },
complete: function () { },
};
};
return State;
}(rxjs_1.Observable));
exports.State = State;
function state(initial) {
return new State(initial);
}
exports.state = state;
//# sourceMappingURL=state.js.map