UNPKG

@rxx/core

Version:

React MVI micro framework.

180 lines (179 loc) 6.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var state_handler_1 = require("../handler/state-handler"); var utils_1 = require("../utils"); var rxjs_1 = require("rxjs"); var MAX_HISTORY_LENGTH = 10; function intent(Base) { var EnhancedIntent = (function (_super) { tslib_1.__extends(EnhancedIntent, _super); function EnhancedIntent(dispatch, handlers) { var _this = _super.call(this, handlers) || this; _this.dispatch = dispatch; for (var key in handlers) { _this[key] = handlers[key]; } return _this; } return EnhancedIntent; }(Base)); return EnhancedIntent; } exports.intent = intent; var Intent = (function (_super) { tslib_1.__extends(Intent, _super); function Intent(parent) { var _this = _super.call(this) || this; _this.history = []; _this.children = []; _this.prepare(parent); return _this; } Intent.prototype.addChild = function (child) { this.children.push(child); }; Intent.prototype.removeChild = function (intent) { var index = this.children.indexOf(intent); if (index > -1) { this.children.splice(index, 1); } }; Intent.prototype.dispose = function () { if (this.parent) { this.parent.removeChild(this); } this.children = []; this.parent = null; }; Intent.prototype.prepare = function (parent) { this.parent = parent; if (this.parent) { this.parent.addChild(this); } }; Intent.prototype.subscribe = function (props) { return new rxjs_1.Subscription(); }; Intent.prototype.clone = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return this; }; Intent.prototype.push = function (key, args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var target, subjects, fire; var _this = this; return tslib_1.__generator(this, function (_a) { if (key === 'RETRY') { target = args ? this.findHistory(args) : this.history[this.history.length - 1]; if (target) { target.fire(); } return [2]; } subjects = this.findSubjects(key); if (!subjects.length) { return [2]; } fire = function () { subjects.forEach(function (subject) { return subject.next({ data: args, state: _this.state }); }); }; this.history.push({ fire: fire, key: key }); if (this.history.length > MAX_HISTORY_LENGTH) { this.history.shift(); } fire(); return [2, Promise.resolve()]; }); }); }; Intent.prototype.getStreamStore = function () { return this.store; }; Intent.prototype.callback = function (key, v) { var _this = this; return function (args) { return _this.push(key, utils_1.isDefined(v) ? v : args); }; }; Intent.prototype.findSubjects = function (key) { var subjects = this.store.get(key); if (this.parent) { subjects = subjects.concat(this.findParentSubjects(key, false)); } if (this.children.length) { subjects = subjects.concat(this.findChildrenSubjects(key, false)); } return subjects; }; Intent.prototype.findParentSubjects = function (key, searchSelfStore) { if (searchSelfStore === void 0) { searchSelfStore = true; } var subjects = searchSelfStore ? this.store.get(key) : []; if (this.parent) { return subjects.concat(this.parent.findParentSubjects(key)); } return subjects; }; Intent.prototype.findChildrenSubjects = function (key, searchSelfStore) { if (searchSelfStore === void 0) { searchSelfStore = true; } var subjects = searchSelfStore ? this.store.get(key) : []; if (this.children.length) { return this.children.reduce(function (subject, child) { return subjects.concat(child.findChildrenSubjects(key)); }, subjects); } return subjects; }; Intent.prototype.findHistory = function (retryKey) { var history = this.history.filter(function (_a) { var key = _a.key; return key === retryKey; })[0]; if (!history && this.parent) { history = this.findParentHistory(retryKey, false); } if (!history && this.children.length) { history = this.findChildrenHistory(retryKey, false); } return history; }; Intent.prototype.findParentHistory = function (retryKey, searchSelfStore) { if (searchSelfStore === void 0) { searchSelfStore = true; } var history = searchSelfStore ? this.history.filter(function (_a) { var key = _a.key; return key === retryKey; })[0] : null; if (!history && this.parent) { return this.parent.findParentHistory(retryKey); } return history; }; Intent.prototype.findChildrenHistory = function (retryKey, searchSelfStore) { if (searchSelfStore === void 0) { searchSelfStore = true; } var history = searchSelfStore ? this.history.filter(function (_a) { var key = _a.key; return key === retryKey; })[0] : null; if (!history && this.children.length) { var found_1 = null; this.children.some(function (child) { found_1 = child.findChildrenHistory(retryKey); if (found_1) { return true; } return false; }); return found_1; } return history; }; return Intent; }(state_handler_1.StateHandler)); exports.Intent = Intent;