UNPKG

@rxx/core

Version:

React MVI micro framework.

108 lines (107 loc) 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var MAX_HISTORY_LENGTH = 10; var SubjectTree = (function () { function SubjectTree(devTools, parent) { this.devTools = devTools; this.parent = parent; this.children = []; this.subject = new rxjs_1.Subject(); this.state = { view: {} }; this.preservation = []; this.shouldImmediateDispatch = true; this.lastDispatched = null; this.histories = []; this.devtoolsEnabled = true; this.parent && this.parent.addChild(this); } SubjectTree.prototype.setState = function (state) { this.state = state; }; SubjectTree.prototype.addChild = function (subjectTree) { this.children.push(subjectTree); }; SubjectTree.prototype.notify = function (payload) { this.shouldImmediateDispatch = false; if (payload.type === 'RETRY') { var target = payload.payload ? this.findHistory(payload.payload) : this.histories[this.histories.length - 1]; if (target) { this.doNotify(target); } return; } this.doNotify(this.createSubjectPayload(payload)); }; SubjectTree.prototype.prepare = function (parent) { this.parent = parent; }; Object.defineProperty(SubjectTree.prototype, "observable", { get: function () { return this.subject.pipe(operators_1.share()); }, enumerable: true, configurable: true }); SubjectTree.prototype.getLastDispatched = function () { return this.lastDispatched; }; SubjectTree.prototype.suspendDevtools = function () { this.devtoolsEnabled = false; }; SubjectTree.prototype.resumeDevtools = function () { this.devtoolsEnabled = true; }; SubjectTree.prototype.doNotify = function (payload) { var _this = this; if (this.devtoolsEnabled) { var args = { type: payload.type, payload: payload.payload }; this.devTools.send(args, tslib_1.__assign({}, this.state.view, { __payload__: args })); } this.histories.push(payload); if (this.histories.length >= MAX_HISTORY_LENGTH) { this.histories.shift(); } this.lastDispatched = payload; this.subject.next(payload); this.children.forEach(function (child) { return child.notify({ type: payload.type, payload: payload.payload }); }); if (this.preservation.length) { var preservations = this.preservation.slice(); this.preservation.length = 0; preservations.forEach(function (preserved) { return _this.notify(preserved); }); } this.shouldImmediateDispatch = true; }; SubjectTree.prototype.findHistory = function (retryKey) { var history = this.histories.filter(function (_a) { var type = _a.type; return type === retryKey; })[0] || null; if (!history && this.children.length) { this.children.some(function (child) { history = child.findHistory(retryKey); return !!history; }); } return history; }; SubjectTree.prototype.createSubjectPayload = function (payload) { var _this = this; return tslib_1.__assign({}, payload, { states: this.state.view, dispatch: function (type, payload) { if (_this.shouldImmediateDispatch) { _this.notify({ type: type, payload: payload }); } else { _this.preservation.push({ type: type, payload: payload }); } } }); }; return SubjectTree; }()); exports.SubjectTree = SubjectTree;