UNPKG

takin

Version:

Front end engineering base toolchain and scaffold

83 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MethodsContainer = void 0; const errors_1 = require("../errors"); /** * 用于插件间共享函数方法 */ class MethodsContainer { constructor(parent, pluginName) { this._methods = new Map(); this._infos = new Map(); this.parent = parent; if (pluginName) this.pluginName = pluginName; } /** * 检查方法是否存在 * @param name - 共享方法名称 * @returns true or false */ has(name) { if (this.parent) return this.parent.has(name); return this._methods.has(name); } /** * 获取方法信息, 目前仅包含注册当前方法的插件名称 * @param name - 共享方法名称 * @returns 共享方法信息 */ getInfo(name) { if (this.parent) return this.parent.getInfo(name); return this._infos.get(name); } /** * 注册插件间共享方法 * @param name - 共享方法名称 * @param method - 共享方法函数体 */ register(name, method, info) { var _a; if (this.has(name)) { let errMessage = `方法 ${name} 已被`; const pluginName = (_a = this.getInfo(name)) === null || _a === void 0 ? void 0 : _a.pluginName; if (pluginName) errMessage = `插件 ${pluginName} 注册`; errMessage = errMessage + '注册'; throw new errors_1.RunnerMethodsError(errMessage); } // 记录方式是由哪个插件注册的 info = info || { pluginName: this.pluginName }; if (this.parent) { this.parent.register(name, method, info); } else { this._methods.set(name, method); this._infos.set(name, info); } } /** * 执行插件间共享方法 * @param name - 方法名称 * @param args - 方法参数 * @returns 方法执行结果 */ invoke(name, ...args) { var _a; if (this.has(name)) { if (this.parent) { return this.parent.invoke(name, ...args); } else { return (_a = this._methods.get(name)) === null || _a === void 0 ? void 0 : _a(...args); } } else { throw new errors_1.RunnerMethodsError(`方法 ${name} 不存在`); } } } exports.MethodsContainer = MethodsContainer; //# sourceMappingURL=methods.js.map