surrogate
Version:
Object method hooks made easy
85 lines • 3.07 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseNext = void 0;
const rules_1 = require("./rules");
const provider_1 = require("../../provider");
const which_1 = require("../../which");
const asarray_1 = require("@jfrazx/asarray");
class BaseNext {
constructor(controller, proxy, container, hookFor) {
this.controller = controller;
this.proxy = proxy;
this.container = container;
this.hookFor = hookFor;
controller.addNext(this);
}
skip(times = 1) {
return this.skipWith(times);
}
next(_a = {}) {
var { using = [] } = _a, options = __rest(_a, ["using"]);
this.controller.timeTracker.setHookEnd();
this.replace(options);
const rules = [
new rules_1.ErrorRule(Object.assign(Object.assign({}, options), { using })),
new rules_1.BailRule(Object.assign(Object.assign({}, options), { using })),
new rules_1.ProgressRule(Object.assign(Object.assign({}, options), { using })),
];
const rule = rules.find((rule) => rule.shouldRun());
rule.run(this);
}
shouldRun(using) {
var _a;
const runParameters = new provider_1.RunConditionProvider(this, using, (_a = this.prevNode) === null || _a === void 0 ? void 0 : _a.didError);
const { options } = this.container;
const context = this.useContext;
return (0, asarray_1.asArray)(options.runConditions).every((condition) => {
const result = condition.call(context, runParameters);
runParameters.reset();
return result;
});
}
get instance() {
return this.context.target;
}
get hookType() {
return this.hookFor === which_1.HookType.PRE ? which_1.HookType.PRE : which_1.HookType.POST;
}
get context() {
return this.controller.context;
}
addNext(next) {
if (this.nextNode) {
return this.nextNode.addNext(next);
}
this.nextNode = next;
next.prevNode = this;
}
get useContext() {
return this.context.determineContext(this.container.options);
}
shouldReplace(options) {
return 'replace' in options;
}
replace(options) {
if (this.shouldReplace(options)) {
this.controller.updateLatestArgs(options.replace);
}
}
bail(bailOptions = {}) {
this.next(Object.assign(Object.assign({}, bailOptions), { bail: true }));
}
}
exports.BaseNext = BaseNext;
//# sourceMappingURL=baseNext.js.map