@rxx/worker
Version:
React MVI micro framework.
104 lines (103 loc) • 3.68 kB
JavaScript
;
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() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.history = [];
_this.children = [];
_this.subscribers = [];
return _this;
}
Intent.prototype.subscribeEvent = function (subscriber) {
this.subscribers.push(subscriber);
};
Intent.prototype.unsubscribeEvent = function (subscriber) {
this.subscribers = this.subscribers.filter(function (s) { return s !== subscriber; });
};
Intent.prototype.unsubscribeAll = function () {
this.subscribers = [];
};
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.subscribers.forEach(function (s) { return s(key, args); });
};
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) {
return this.store.get(key);
};
Intent.prototype.findHistory = function (retryKey) {
return this.history.filter(function (_a) {
var key = _a.key;
return key === retryKey;
})[0];
};
return Intent;
}(state_handler_1.StateHandler));
exports.Intent = Intent;