md5-fight-plus
Version:
115 lines (114 loc) • 4.15 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncBailHook = exports.hooksRecord = exports.ids = void 0;
const BailEvent_1 = require("../events/BailEvent");
const lodash_1 = __importDefault(require("lodash"));
exports.ids = -1;
exports.hooksRecord = [];
class SyncBailHook {
constructor(options = {}) {
this.bailedResult = undefined;
this.cbs = [];
this.options = options;
this.error = undefined;
this.lastCallback = (params) => params;
if (this.options.afterAction) {
this.registerAfterActionHook("register options afterAction", this.options.afterAction);
}
if (this.options.beforeAction) {
this.registerBeforeActionHook("register options beforeAction", this.options.beforeAction);
}
}
tap(options, callback) {
const optionsFormatted = formatTapOptions(options);
this.cbs.push({
options: optionsFormatted,
func: callback,
id: exports.ids++,
});
this.cbs.sort((cb1, cb2) => cb1.options.stage - cb2.options.stage);
return exports.ids - 1;
}
call(params) {
this.bailedResult = params;
try {
for (let i = 0; i < this.cbs.length; i++) {
const currentTap = this.cbs[i];
if (this.beforeActionHook) {
this.bailedResult = this.beforeActionHook.call(this.bailedResult);
//如果返回值是BailEvent,则会直接熔断。
if (this.bailedResult instanceof BailEvent_1.BailEvent)
break;
}
this.bailedResult = currentTap.func(this.bailedResult);
if (lodash_1.default.isNumber(currentTap.options.lives) &&
--currentTap.options.lives <= 0) {
//减少生命计数,若计数值<0则移除taps
this.removeTap(currentTap.id);
i--;
}
exports.hooksRecord.push(currentTap.options.name);
//如果返回值是BailEvent,则会直接熔断。
if (this.bailedResult instanceof BailEvent_1.BailEvent)
break;
//拦截器
if (this.afterActionHook) {
this.bailedResult = this.afterActionHook.call(this.bailedResult);
//如果返回值是BailEvent,则会直接熔断。
if (this.bailedResult instanceof BailEvent_1.BailEvent)
break;
}
}
}
catch (err) {
this.error = err;
}
finally {
if (this.error)
throw this.error;
return this.lastCallback(this.bailedResult);
}
}
registerLastCallback(cb) {
this.lastCallback = cb;
}
registerAfterActionHook(info = "register afterActionFn", cb) {
if (!this.afterActionHook) {
this.afterActionHook = new SyncBailHook();
}
this.afterActionHook.tap(info, (params) => cb(params));
}
registerBeforeActionHook(info = "register afterActionFn", cb) {
if (!this.beforeActionHook) {
this.beforeActionHook = new SyncBailHook();
}
this.beforeActionHook.tap(info, (params) => cb(params));
}
removeTap(id) {
for (let i = 0; i < this.cbs.length; i++) {
const cb = this.cbs[i];
if (cb.id === id) {
return this.cbs.splice(i, 1);
}
}
return false;
}
}
exports.SyncBailHook = SyncBailHook;
function formatTapOptions(options) {
const rawOptions = {
name: "",
stage: 50,
};
if (lodash_1.default.isString(options)) {
rawOptions.name = options;
return rawOptions;
}
for (const key in options) {
rawOptions[key] = options[key];
}
return rawOptions;
}