UNPKG

plugin-tls

Version:

plugin-tls 是插件系统相应的工具,可快速地让任何模块具备插件机制

1,039 lines (1,038 loc) 22.8 kB
import { deepmergeInto as M, deepmerge as w } from "deepmerge-ts"; import { WebDestroyable as V, createDestroyableSubClass as H } from "@gby/destroyable"; function l(n, ...t) { for (const e of t) { const i = Object.getOwnPropertyDescriptors(e); Object.defineProperties(n, i); } return n; } function A(n, t) { return t; } function k() { return function(t, e) { return e; }; } function z(n, t) { return l(n, t), t; } function C(n) { return function(e) { return l(n, e), e; }; } function a(n, t) { return l(n, t), n; } function $(n) { return function(e) { return l(n, e), n; }; } function S(n, t) { return t; } function O(n, t) { l(n.prototype, t); const e = t._constructor; typeof e == "function" && (n._constructors ?? (n._constructors = [])).push(e); const i = t._destroy; return typeof i == "function" && (n._destroys ?? (n._destroys = [])).push(i), t; } function q(n) { return function(e) { return O(n, e), e; }; } function T(n, t) { return O(n, t), n; } function B(n) { return function(e) { return O(n, e), n; }; } function m(n, t, e) { for (const i of n) i.apply(t, e); } function K(n) { return { constructed: function(t, e) { m(n._constructors ?? [], t, e); }, initialized: function(t, e) { m(n._inits ?? [], t, e); }, destroyed: function(t, e) { m(n._destroys ?? [], t, e); } }; } function R(n, t) { return t; } function y(n, t) { O(n, t); const e = t._init; return typeof e == "function" && (n._inits ?? (n._inits = [])).push(e), t; } function G(n) { return function(e) { return y(n, e), e; }; } function J(n, t) { return y(n, t), n; } function L(n) { return function(e) { return y(n, e), n; }; } class g { /** * 插件的宿主 */ host; /** * 插件与插件选项的映射 */ pluginOptsMap = /* @__PURE__ */ new Map(); /** * 注册的插件 */ plugins = []; /** * @param host - 插件的宿主 */ constructor(t) { t && (this.host = t); } /** * 使用插件、注册插件 * @param plugin - 插件 * @param options - 传递给插件的选项 */ use(t, ...e) { const { pluginOptsMap: i, plugins: s, host: o } = this; if (!s.includes(t)) return s.push(t), i.set(t, e[0]), t.used(o, e[0]); } /** * 插件宿主创建实例时调用 * * @remarks * 会调用每个插件的 {@link IPlugin.created} 方法 * @param hostInst - 插件宿主的新实例 */ created(t) { const { plugins: e, pluginOptsMap: i } = this; for (const s of e) { const o = i.get(s); s.created?.(t, o); } } /** * 插件宿主被销毁时调用 * * @remarks * 会调用每个插件的 {@link IPlugin.destroyed} 方法 * @param hostInst - 被销毁的插件实例 */ destroyed(t) { const { plugins: e, pluginOptsMap: i } = this; for (const s of e) { const o = i.get(s); s.destroyed?.(t, o); } } /** * 插件宿主实例的注册插件的方法 * * @remarks * 会调用每个插件的 {@link IPlugin.use | IPlugin.created()} 方法 * @param hostInst - 插件宿主的新实例 */ instUse(t, e, ...i) { if (!this.pluginOptsMap.has(e)) return this.use(e, ...i), e.created?.(t, ...i); } } class Q extends g { /** * 插件宿主创建实例时调用 * * @remarks * 会调用每个插件的 {@link IPlugin.created | IPlugin.created()} 方法 * @param hostInst - 插件宿主的新实例 */ inited(t) { const { plugins: e, pluginOptsMap: i } = this; for (const s of e) { const o = i.get(s); s.inited?.(t, o); } } } function X(n, t) { const e = t ?? "defaultOptions", i = `_${e}`; return function(o) { const r = a(o, { /** * 全局级别的默认选项 */ get [e]() { return n; } }); T(r, { /** * 实例级的默认选项 */ get [e]() { return this[i] || (this[i] = structuredClone(r.defaultOptions)); } }); }; } function Y(n, t, e) { const i = `_${t}`; return T(n, { /** * 实例级的默认选项 */ get [t]() { return this[i] || (this[i] = structuredClone(n[t])); } }), e; } function F(n) { const t = n.split("."); return t[0] === "" && t.shift(), t; } function x(n, t) { for (const e of t) if (n = n[e], n == null) return; return n; } function Z(n, t) { return x(n, F(t)); } function b(n, t, e) { const i = t.pop(), s = t.length; let o = 0; for (; o < s; ) { const r = n[t[o]]; if (r == null) break; n = r, o++; } t = t.slice(o); for (const r of t) n = n[r] = {}; return n[i] = e, n; } function tt(n, t, e) { return b(n, F(t), e); } const _ = { UndefinedAndNull: function(t) { return t == null; }, Undefined: function(t) { return t === void 0; }, Null: function(t) { return t === null; } }; class v { /** * 是否要冻结 fullOption 的成员 * @remarks * 只会冻结 fullOption 的直接成员的值,不会冻结 fullOption 孙子成员的值 */ freezeMember = !0; /** * 可更新顶级成员值的完整的选项 * @remarks * 合并后的选项; * plainFullOption 的代理对象,自动监听了顶层成员的屐;所以可以更改顶级成员的值,不可以更改非顶级成员的值; */ fullOption = new Proxy(this, { // @ts-ignore get: function(t, e, i) { const s = t.plainFullOption[e]; return t.freezeMember ? Object.freeze(s) : s; }, // @ts-ignore set: function(t, e, i, s) { return t.setMember(e, i, !1), !0; }, // @ts-ignore has: function(t, e) { return e in t.plainFullOption; }, // @ts-ignore getOwnPropertyDescriptor: function(t, e) { return Object.getOwnPropertyDescriptor(t.plainFullOption, e); }, // @ts-ignore ownKeys: function(t) { return Reflect.ownKeys(t.plainFullOption); }, // @ts-ignore deleteProperty: function(t, e) { return t.setMember(e, void 0), !0; }, // @ts-ignore defineProperty: function(t, e, i) { return Object.defineProperty(t.option, e, i), !0; } }); /** * 设置选项 * @remarks * 会将被设置的选项浅合并进当前选项中 * @param option * @param deep - 是否要进行深度合并 */ setOption(t, e) { const i = e ? M(this.option, t) : Object.assign(this.option, t); return this.updateFullOption(), i; } /** * 未定义值的类型 */ get undefinedValue() { return this._undefinedValue; } set undefinedValue(t) { this._undefinedValue = t, this.isUndefined = _[t] || _.UndefinedAndNull; } _undefinedValue = "UndefinedAndNull"; /** * 未定义值的判断函数 */ isUndefined = _.UndefinedAndNull; /** * 获取选项成员 * @param key * @returns */ getMember(t) { return this.option[t]; } /** * 设置选项成员 * @param key * @param value * @param deep - 是否要进行深度合并 */ setMember(t, e, i) { const s = this.option; if (e === void 0) delete s[t]; else { const o = s[t]; i && o && typeof o == "object" ? M(o, e) : s[t] = e; } this.updateFullOption(); } /** * 获取属性路径对应的值 * @param propPath * @returns */ getValue(t) { return x(this.option, t); } /** * 设置属性路径对应的值 * @param propPath * @param value * @param deep - 是否要进行深度合并 * @returns */ setValue(t, e, i) { const s = this.option; let o; i && (o = x(s, t)) && typeof o == "object" ? M(o, e) : b(this.option, t, e), this.updateFullOption(); } } class p extends v { constructor(t, e) { super(), t && (this.option = t), e && (this.extends = e); } /** * 继承的选项 */ get extends() { return this._extends; } set extends(t) { this._extends = t, this.updateFullOption(); } _extends = null; /** * 选项 */ option = {}; /** * 获取完整的选项 * @remarks * 会与继承来的选项进行合并 */ get plainFullOption() { let t = this._plainFullOption; if (t) return t; const { extends: e, option: i } = this; if (t = i, e) { const s = e.plainFullOption; t = w(s, i); } return this._plainFullOption = t; } _plainFullOption = null; /** * 更新 */ updateFullOption() { this._plainFullOption = null; } /** * 获取选项成员 * @param key * @returns */ getMember(t) { const e = this.option[t]; if (!this.isUndefined(e)) return e; const i = this.extends; if (i) return i.getMember(t); } /** * 获取属性路径对应的值 * @param propPath * @returns */ getValue(t) { const e = super.getValue(t); if (!this.isUndefined(e)) return e; const i = this.extends; if (i) return i.getValue(t); } } function et(n, t) { const { name: e } = t, i = `_${e}`; return { get: function() { return this[i] || (this[i] = new p(void 0, this.constructor[e])); } }; } class E extends v { constructor(t, e) { super(), e && this.setModeOption(t, e), this.mode = t; } /** * 模式的配置 */ modes = {}; /** * 当前模式 */ get mode() { return this._mode || (this.mode = this.modeNames[0]); } set mode(t) { this._mode = t, this.hasMode(t) || this.setModeOption(t, {}), this.updateFullOption(); } _mode; /** * 所有的模式名字 */ get modeNames() { return this._modeNames || (this._modeNames = this.getModeNames()); } _modeNames = null; /** * 获取所有的模式名字 * @returns */ getModeNames() { return Object.keys(this.modes); } /** * 判断是否有某个模式 * @param mode * @returns */ hasMode(t) { return !!this.modes[t]; } /** * 当前模式的选项 */ get optionManager() { return this.modes[this.mode]; } /** * 当前模式对应的选项 */ get option() { return this.optionManager.option; } get plainFullOption() { return this._plainFullOption ?? (this._plainFullOption = this.getFullModeOption(this.mode)); } _plainFullOption = null; /** * 更新 */ updateFullOption() { this._plainFullOption = null, this._modeNames = null; } /** * 返回已合并继承选项的选项 * @remarks * 与继承选项只是浅合并 * @param mode * @returns */ getModeOption(t) { return this.modes[t].option; } /** * 返回已合并继承选项的完整选项 * @remarks * 与继承选项合并 * @param mode * @returns */ getFullModeOption(t) { return this.modes[t]?.plainFullOption; } /** * 给模式设置选项 * @param mode * @param option * @param deep - 是否要进行深度合并 * @param extend - 继承哪个模式 */ setModeOption(t, e, i, s) { const { modes: o } = this, r = o[t] || (o[t] = new p()); if (r.setOption(e, i), s) { const u = o[s]; u && (r.extends = u); } return this.updateFullOption(), r; } } class c extends E { constructor(t, e, i) { super(t, e), i && (this.extends = i); } /** * 继承的选项 */ get extends() { return this._extends; } set extends(t) { this._extends = t, this.updateFullOption(); } _extends = null; /** * 返回已合并继承选项的最终选项 * @remarks * 与继承选项只是浅合并 * @param mode * @returns */ getFullModeOption(t) { const { modes: e, extends: i } = this, s = e[t]?.plainFullOption; if (i) { const o = i.getFullModeOption(t); return s ? w(o, s) : o; } return s; } /** * 获取所有的模式名字 * @returns */ getModeNames() { const { modes: t, extends: e } = this, i = Object.keys(t); if (!e) return i; const s = e.getModeNames(); return [.../* @__PURE__ */ new Set([...i, ...s])]; } /** * 在整个层级上判断是否有某个模式 * @param mode * @returns */ hasModeOnTier(t) { if (this.hasMode(t)) return !0; const e = this.extends; return e ? e.hasModeOnTier(t) : !1; } /** * 当前模式的选项 */ get optionManager() { return this.getModeOptionManager(this.mode); } /** * 获取模式对应的选项管理器 * @param mode * @returns */ getModeOptionManager(t) { const e = this.modes[t]; return e || (this.extends?.getModeOptionManager(t) ? this.setModeOption(t, {}) : null); } /** * 获取选项成员 * @param key * @returns */ getMember(t) { const e = this.optionManager.getMember(t); if (!this.isUndefined(e)) return e; const i = this.extends; if (i) return i.getMember(t); } /** * 获取属性路径对应的值 * @param propPath * @returns */ getValue(t) { const e = this.optionManager.getValue(t); if (!this.isUndefined(e)) return e; const i = this.extends; if (i) return i.getValue(t); } } function nt(n, t) { const { name: e } = t, i = `_${e}`; return { get: function() { return this[i] || (this[i] = new c(this.constructor[e].mode, void 0, this.constructor[e])); } }; } class N extends V { /** * 插件管理器 */ static get pluginManager() { let t = this._pluginManager; return t || (t = this._pluginManager = new g(this)), t; } static _pluginManager; /** * 插件管理器 {@link PluginHost.pluginManager} */ get pluginManager() { return this.constructor.pluginManager; } /** * {@inheritDoc PluginManager.use} */ static use(t, ...e) { return this.pluginManager.use(t, ...e); } /** * 需要在子类中调用 * * 之所以没写在该类中的函数中,是因为 `this.pluginManager.created(this)` 通常会在构建函数执行的最后时才调用; * 如果像下面写在该类的构建函数中,然后子类通过 `super()` 来调用,这样就会导致 `this.pluginManager.created(this)` 不是在最后的执行; */ // constructor() { // this.pluginManager.created(this); // } /** * 实例的注册插件的方法 * @param plugin * @param options * @returns */ use(t, ...e) { return this.pluginManager.instUse(this, t, ...e); } /** * 销毁实例 * @returns */ destroyThis() { return this.pluginManager.destroyed(this), super.destroyThis(); } } function U(n) { const t = H(n); return class extends t { /** * PluginHost 的插件管理器 */ static get pluginManager() { let i = this._pluginManager; return i || (i = this._pluginManager = new g(this)), i; } static _pluginManager; /** * 插件管理器 {@link CEarth.pluginManager} */ get pluginManager() { return this.constructor.pluginManager; } /** * {@inheritDoc PluginManager.use} */ static use(i, ...s) { return this.pluginManager.use(i, ...s); } // constructor() { // this.pluginManager.created(this as HostInst<PHOST>); // } /** /** * 实例的注册插件的方法 * @param plugin * @param options * @returns */ use(i, ...s) { return this.pluginManager.instUse(this, i, ...s); } /** * 销毁实例 * @returns */ destroyThis() { return this.pluginManager.destroyed(this), super.destroyThis(); } }; } function D(n) { const t = a(n, { /** * PluginHost 的插件管理器 */ get pluginManager() { let e = this._pluginManager; return e || (e = this._pluginManager = new g(this)), e; }, // _pluginManager: PluginManager<HOST,Plugin>; /** * {@inheritDoc PluginManager.use} */ use(e, i) { return this.pluginManager.use(e, i); } }); return a(t.prototype, { /** * 插件管理器 */ get pluginManager() { return this.constructor.pluginManager; }, /** * 实例的注册插件的方法 * @param plugin * @param options * @returns */ use(e, i) { return this.pluginManager.instUse(this, e, i); }, /** * 销毁实例 * @returns */ destroy() { return this.pluginManager.destroyed(this); } }), n; } function st(n) { const t = D(n); return new Proxy(n, { construct: function(e, i, s) { const o = new e(...i); return t.pluginManager.created(this), o; } }); } function rt(n) { return class extends n { /** * 插件管理器 */ static get pluginManager() { let e = this._pluginManager; return e || (e = this._pluginManager = new g(this)), e; } static _pluginManager; /** * 插件管理器 {@link PluginHost.pluginManager} */ get pluginManager() { return this.constructor.pluginManager; } /** * {@inheritDoc PluginManager.use} */ static use(e, ...i) { return this.pluginManager.use(e, ...i); } constructor(...e) { super(...e), this.pluginManager.created(this); } /** * 实例的注册插件的方法 * @param plugin * @param options * @returns */ use(e, ...i) { return this.pluginManager.instUse(this, e, ...i); } /** * 销毁实例 * @returns */ destroy() { return this.pluginManager.destroyed(this); } }; } class ut extends N { /** * 全局级别的默认选项 */ static defaultOptions = new p(); /** * 实例级的默认选项 */ get defaultOptions() { return this._defaultOptions || (this._defaultOptions = new p(void 0, this.constructor.defaultOptions)); } _defaultOptions; constructor(t) { super(), this.defaultOptions.option = t; } } function at(n) { const t = U(n); return class extends t { /** * 全局级别的默认选项 */ static defaultOptions = new p(); /** * 实例级的默认选项 */ get defaultOptions() { return this._defaultOptions || (this._defaultOptions = new p(void 0, this.constructor.defaultOptions)); } _defaultOptions; constructor(i) { super(), this.defaultOptions.option = i; } }; } function ct(n, t) { const e = a(n, { /** * 全局级别的默认选项 */ defaultOptions: new p(t) }); return a(e.prototype, { /** * 实例级的默认选项 */ get defaultOptions() { return this._defaultOptions || (this._defaultOptions = new p(void 0, this.constructor.defaultOptions)); } }), n; } const h = "common"; class dt extends N { /** * 全局级别的默认选项 */ static modeOptions = new c(h); /** * 实例级的默认选项 */ get modeOptions() { return this._modeOptions || (this._modeOptions = new c(h, void 0, this.constructor.modeOptions)); } _modeOptions; get mode() { return this.modeOptions.mode; } set mode(t) { this.modeOptions.mode = t; } get options() { return this.modeOptions.plainFullOption; } constructor(t) { super(), this.modeOptions.setOption(t); } } function gt(n) { const t = U(n); return class extends t { /** * 全局级别的默认选项 */ static modeOptions = new c(h); /** * 实例级的默认选项 */ get modeOptions() { return this._modeOptions || (this._modeOptions = new c(h, void 0, this.constructor.modeOptions)); } _modeOptions; get mode() { return this.modeOptions.mode; } set mode(i) { this.modeOptions.mode = i; } get options() { return this.modeOptions.plainFullOption; } constructor(i) { super(), this.modeOptions.setOption(i); } }; } function ft(n, t, e) { const i = a(n, { /** * 全局级别的默认选项 */ modeOptions: new c(t, e) }); return a(i.prototype, { /** * 实例级的默认选项 */ get modeOptions() { return this._modeOptions || (this._modeOptions = new c(t, void 0, this.constructor.modeOptions)); }, get mode() { return this.modeOptions.mode; }, set mode(s) { this.modeOptions.mode = s; }, get options() { return this.modeOptions.fullOption; } }), n; } function P(n, t) { const e = [{ target: n, props: t }]; let i; for (; i = e.shift(); ) { const { target: s, props: o } = i; for (const [r, u] of Object.entries(o)) { if (u != null && typeof u == "object" && (!u.constructor || u.constructor === Object)) { e.push({ target: s[r], props: u }); continue; } s[r] = u; } } return n; } function j(n, t) { const e = {}, i = [{ target: n, props: t, targetProps: e }]; let s; for (; s = i.shift(); ) { const { target: o, props: r, targetProps: u } = s; for (const [f, d] of Object.entries(r)) { if (d != null && typeof d == "object" && (!d.constructor || d.constructor === Object)) { i.push({ target: o[f], props: d, targetProps: u[f] = {} }); continue; } u[f] = o[f]; } } return e; } function ht(n, t) { const e = j(n, t); function i() { P(n, t); } function s() { P(n, e); } return { set: i, unset: s }; } export { E as ModeOptions, N as PluginHost, g as PluginManager, Q as PluginManagerWithInit, c as TierModeOptions, dt as TierModeOptionsPluginHost, p as TierOptions, ut as TierOptionsPluginHost, X as addDefaultOptions, Y as addInstOptions, m as allFunctionApply, D as becomePluginHost, rt as becomePluginHost_Extends, st as becomePluginHost_Proxy, ft as becomeTierModeOptionsHost, ct as becomeTierOptionsHost, h as commonModeName, K as createCallerOfPrivateMemberOfExtend, k as createDefineMixin, B as createExtendTarget, L as createExtendTargetWithInit, $ as createMixinTarget, U as createPluginHost, ht as createPropertySetter, q as createTargetExtend, G as createTargetExtendWithInit, C as createTargetMixin, gt as createTierModeOptionsPluginHost, at as createTierOptionsPluginHost, S as defineExtend, R as defineExtendWithInit, A as defineMixin, T as extendTarget, J as extendTargetWithInit, x as getPropertyValue, Z as getPropertyValueByPath, j as getTargetProperties, P as mergeProperties, l as mixin, a as mixinTarget, F as parsePropertyPath, b as setPropertyValue, tt as setPropertyValueByPath, O as targetExtend, y as targetExtendWithInit, z as targetMixin, nt as tierModeOptionsDecorator, et as tierOptionsDecorator };