plugin-tls
Version:
plugin-tls 是插件系统相应的工具,可快速地让任何模块具备插件机制
2 lines (1 loc) • 12.6 kB
JavaScript
(function(s,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("deepmerge-ts"),require("@gby/destroyable")):typeof define=="function"&&define.amd?define(["exports","deepmerge-ts","@gby/destroyable"],d):(s=typeof globalThis<"u"?globalThis:s||self,d(s.PluginTls={},s.deepmergeTs,s.destroyable))})(this,(function(s,d,N){"use strict";function g(n,...t){for(const e of t){const i=Object.getOwnPropertyDescriptors(e);Object.defineProperties(n,i)}return n}function D(n,t){return t}function W(){return function(t,e){return e}}function j(n,t){return g(n,t),t}function A(n){return function(e){return g(n,e),e}}function l(n,t){return g(n,t),n}function S(n){return function(e){return g(n,e),n}}function C(n,t){return t}function h(n,t){g(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 k(n){return function(e){return h(n,e),e}}function T(n,t){return h(n,t),n}function q(n){return function(e){return h(n,e),n}}function m(n,t,e){for(const i of n)i.apply(t,e)}function z(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 B(n,t){return t}function _(n,t){h(n,t);const e=t._init;return typeof e=="function"&&(n._inits??(n._inits=[])).push(e),t}function $(n){return function(e){return _(n,e),e}}function K(n,t){return _(n,t),n}function R(n){return function(e){return _(n,e),n}}class f{host;pluginOptsMap=new Map;plugins=[];constructor(t){t&&(this.host=t)}use(t,...e){const{pluginOptsMap:i,plugins:r,host:o}=this;if(!r.includes(t))return r.push(t),i.set(t,e[0]),t.used(o,e[0])}created(t){const{plugins:e,pluginOptsMap:i}=this;for(const r of e){const o=i.get(r);r.created?.(t,o)}}destroyed(t){const{plugins:e,pluginOptsMap:i}=this;for(const r of e){const o=i.get(r);r.destroyed?.(t,o)}}instUse(t,e,...i){if(!this.pluginOptsMap.has(e))return this.use(e,...i),e.created?.(t,...i)}}class G extends f{inited(t){const{plugins:e,pluginOptsMap:i}=this;for(const r of e){const o=i.get(r);r.inited?.(t,o)}}}function J(n,t){const e=t??"defaultOptions",i=`_${e}`;return function(o){const u=l(o,{get[e](){return n}});T(u,{get[e](){return this[i]||(this[i]=structuredClone(u.defaultOptions))}})}}function L(n,t,e){const i=`_${t}`;return T(n,{get[t](){return this[i]||(this[i]=structuredClone(n[t]))}}),e}function b(n){const t=n.split(".");return t[0]===""&&t.shift(),t}function y(n,t){for(const e of t)if(n=n[e],n==null)return;return n}function Q(n,t){return y(n,b(t))}function x(n,t,e){const i=t.pop(),r=t.length;let o=0;for(;o<r;){const u=n[t[o]];if(u==null)break;n=u,o++}t=t.slice(o);for(const u of t)n=n[u]={};return n[i]=e,n}function X(n,t,e){return x(n,b(t),e)}const w={UndefinedAndNull:function(t){return t==null},Undefined:function(t){return t===void 0},Null:function(t){return t===null}};class V{freezeMember=!0;fullOption=new Proxy(this,{get:function(t,e,i){const r=t.plainFullOption[e];return t.freezeMember?Object.freeze(r):r},set:function(t,e,i,r){return t.setMember(e,i,!1),!0},has:function(t,e){return e in t.plainFullOption},getOwnPropertyDescriptor:function(t,e){return Object.getOwnPropertyDescriptor(t.plainFullOption,e)},ownKeys:function(t){return Reflect.ownKeys(t.plainFullOption)},deleteProperty:function(t,e){return t.setMember(e,void 0),!0},defineProperty:function(t,e,i){return Object.defineProperty(t.option,e,i),!0}});setOption(t,e){const i=e?d.deepmergeInto(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=w[t]||w.UndefinedAndNull}_undefinedValue="UndefinedAndNull";isUndefined=w.UndefinedAndNull;getMember(t){return this.option[t]}setMember(t,e,i){const r=this.option;if(e===void 0)delete r[t];else{const o=r[t];i&&o&&typeof o=="object"?d.deepmergeInto(o,e):r[t]=e}this.updateFullOption()}getValue(t){return y(this.option,t)}setValue(t,e,i){const r=this.option;let o;i&&(o=y(r,t))&&typeof o=="object"?d.deepmergeInto(o,e):x(this.option,t,e),this.updateFullOption()}}class c 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={};get plainFullOption(){let t=this._plainFullOption;if(t)return t;const{extends:e,option:i}=this;if(t=i,e){const r=e.plainFullOption;t=d.deepmerge(r,i)}return this._plainFullOption=t}_plainFullOption=null;updateFullOption(){this._plainFullOption=null}getMember(t){const e=this.option[t];if(!this.isUndefined(e))return e;const i=this.extends;if(i)return i.getMember(t)}getValue(t){const e=super.getValue(t);if(!this.isUndefined(e))return e;const i=this.extends;if(i)return i.getValue(t)}}function Y(n,t){const{name:e}=t,i=`_${e}`;return{get:function(){return this[i]||(this[i]=new c(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;getModeNames(){return Object.keys(this.modes)}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}getModeOption(t){return this.modes[t].option}getFullModeOption(t){return this.modes[t]?.plainFullOption}setModeOption(t,e,i,r){const{modes:o}=this,u=o[t]||(o[t]=new c);if(u.setOption(e,i),r){const a=o[r];a&&(u.extends=a)}return this.updateFullOption(),u}}class p 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;getFullModeOption(t){const{modes:e,extends:i}=this,r=e[t]?.plainFullOption;if(i){const o=i.getFullModeOption(t);return r?d.deepmerge(o,r):o}return r}getModeNames(){const{modes:t,extends:e}=this,i=Object.keys(t);if(!e)return i;const r=e.getModeNames();return[...new Set([...i,...r])]}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)}getModeOptionManager(t){const e=this.modes[t];return e||(this.extends?.getModeOptionManager(t)?this.setModeOption(t,{}):null)}getMember(t){const e=this.optionManager.getMember(t);if(!this.isUndefined(e))return e;const i=this.extends;if(i)return i.getMember(t)}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 Z(n,t){const{name:e}=t,i=`_${e}`;return{get:function(){return this[i]||(this[i]=new p(this.constructor[e].mode,void 0,this.constructor[e]))}}}class F extends N.WebDestroyable{static get pluginManager(){let t=this._pluginManager;return t||(t=this._pluginManager=new f(this)),t}static _pluginManager;get pluginManager(){return this.constructor.pluginManager}static use(t,...e){return this.pluginManager.use(t,...e)}use(t,...e){return this.pluginManager.instUse(this,t,...e)}destroyThis(){return this.pluginManager.destroyed(this),super.destroyThis()}}function H(n){const t=N.createDestroyableSubClass(n);return class extends t{static get pluginManager(){let i=this._pluginManager;return i||(i=this._pluginManager=new f(this)),i}static _pluginManager;get pluginManager(){return this.constructor.pluginManager}static use(i,...r){return this.pluginManager.use(i,...r)}use(i,...r){return this.pluginManager.instUse(this,i,...r)}destroyThis(){return this.pluginManager.destroyed(this),super.destroyThis()}}}function U(n){const t=l(n,{get pluginManager(){let e=this._pluginManager;return e||(e=this._pluginManager=new f(this)),e},use(e,i){return this.pluginManager.use(e,i)}});return l(t.prototype,{get pluginManager(){return this.constructor.pluginManager},use(e,i){return this.pluginManager.instUse(this,e,i)},destroy(){return this.pluginManager.destroyed(this)}}),n}function tt(n){const t=U(n);return new Proxy(n,{construct:function(e,i,r){const o=new e(...i);return t.pluginManager.created(this),o}})}function et(n){return class extends n{static get pluginManager(){let e=this._pluginManager;return e||(e=this._pluginManager=new f(this)),e}static _pluginManager;get pluginManager(){return this.constructor.pluginManager}static use(e,...i){return this.pluginManager.use(e,...i)}constructor(...e){super(...e),this.pluginManager.created(this)}use(e,...i){return this.pluginManager.instUse(this,e,...i)}destroy(){return this.pluginManager.destroyed(this)}}}class nt extends F{static defaultOptions=new c;get defaultOptions(){return this._defaultOptions||(this._defaultOptions=new c(void 0,this.constructor.defaultOptions))}_defaultOptions;constructor(t){super(),this.defaultOptions.option=t}}function it(n){const t=H(n);return class extends t{static defaultOptions=new c;get defaultOptions(){return this._defaultOptions||(this._defaultOptions=new c(void 0,this.constructor.defaultOptions))}_defaultOptions;constructor(i){super(),this.defaultOptions.option=i}}}function st(n,t){const e=l(n,{defaultOptions:new c(t)});return l(e.prototype,{get defaultOptions(){return this._defaultOptions||(this._defaultOptions=new c(void 0,this.constructor.defaultOptions))}}),n}const O="common";class rt extends F{static modeOptions=new p(O);get modeOptions(){return this._modeOptions||(this._modeOptions=new p(O,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 ot(n){const t=H(n);return class extends t{static modeOptions=new p(O);get modeOptions(){return this._modeOptions||(this._modeOptions=new p(O,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 ut(n,t,e){const i=l(n,{modeOptions:new p(t,e)});return l(i.prototype,{get modeOptions(){return this._modeOptions||(this._modeOptions=new p(t,void 0,this.constructor.modeOptions))},get mode(){return this.modeOptions.mode},set mode(r){this.modeOptions.mode=r},get options(){return this.modeOptions.fullOption}}),n}function v(n,t){const e=[{target:n,props:t}];let i;for(;i=e.shift();){const{target:r,props:o}=i;for(const[u,a]of Object.entries(o)){if(a!=null&&typeof a=="object"&&(!a.constructor||a.constructor===Object)){e.push({target:r[u],props:a});continue}r[u]=a}}return n}function I(n,t){const e={},i=[{target:n,props:t,targetProps:e}];let r;for(;r=i.shift();){const{target:o,props:u,targetProps:a}=r;for(const[P,M]of Object.entries(u)){if(M!=null&&typeof M=="object"&&(!M.constructor||M.constructor===Object)){i.push({target:o[P],props:M,targetProps:a[P]={}});continue}a[P]=o[P]}}return e}function at(n,t){const e=I(n,t);function i(){v(n,t)}function r(){v(n,e)}return{set:i,unset:r}}s.ModeOptions=E,s.PluginHost=F,s.PluginManager=f,s.PluginManagerWithInit=G,s.TierModeOptions=p,s.TierModeOptionsPluginHost=rt,s.TierOptions=c,s.TierOptionsPluginHost=nt,s.addDefaultOptions=J,s.addInstOptions=L,s.allFunctionApply=m,s.becomePluginHost=U,s.becomePluginHost_Extends=et,s.becomePluginHost_Proxy=tt,s.becomeTierModeOptionsHost=ut,s.becomeTierOptionsHost=st,s.commonModeName=O,s.createCallerOfPrivateMemberOfExtend=z,s.createDefineMixin=W,s.createExtendTarget=q,s.createExtendTargetWithInit=R,s.createMixinTarget=S,s.createPluginHost=H,s.createPropertySetter=at,s.createTargetExtend=k,s.createTargetExtendWithInit=$,s.createTargetMixin=A,s.createTierModeOptionsPluginHost=ot,s.createTierOptionsPluginHost=it,s.defineExtend=C,s.defineExtendWithInit=B,s.defineMixin=D,s.extendTarget=T,s.extendTargetWithInit=K,s.getPropertyValue=y,s.getPropertyValueByPath=Q,s.getTargetProperties=I,s.mergeProperties=v,s.mixin=g,s.mixinTarget=l,s.parsePropertyPath=b,s.setPropertyValue=x,s.setPropertyValueByPath=X,s.targetExtend=h,s.targetExtendWithInit=_,s.targetMixin=j,s.tierModeOptionsDecorator=Z,s.tierOptionsDecorator=Y,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));