@kaokei/di 5.0.3 → 5.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +103 -103
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var z=Object.defineProperty;var U=(n,t,e)=>t in n?z(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var o=(n,t,e)=>U(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},f={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},p={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},N={POST_CONSTRUCT:"Multiple @PostConstruct decorators are not allowed in a single class.",PRE_DESTROY:"Multiple @PreDestroy decorators are not allowed in a single class.",INVALID_TOKEN:"@Inject or @LazyInject requires a valid token, but received null or undefined.",LAZY_INJECT_INVALID_TOKEN:"@LazyInject requires a valid token, but received null or undefined."},D=Symbol("UNINITIALIZED");function _(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function Y(n){return n!==null&&typeof n=="object"}const y=new WeakMap;function O(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function P(n,t){y.set(n,t)}function V(n){const t=y.get(n);if(t)return t[u.POST_CONSTRUCT];if(O(n))return V(Object.getPrototypeOf(n))}function j(n){const t=y.get(n);if(t)return t[u.PRE_DESTROY];if(O(n))return j(Object.getPrototypeOf(n))}function w(n){const t=y.get(n),e=t&&_(t,u.INJECTED_PROPS)?t[u.INJECTED_PROPS]:void 0;if(!O(n))return e;const i=w(Object.getPrototypeOf(n));if(i||e)return Object.assign({},i,e)}class J{constructor(t){o(this,"name");this.name=t}}class R{constructor(t){o(this,"_callback");this._callback=t}resolve(){return this._callback()}}function b(n){if(!n)throw new Error(N.INVALID_TOKEN);return n instanceof R?n.resolve():n}class v extends Error{constructor(e,i){const s=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${s}`);o(this,"token");this.name=this.constructor.name,this.token=i}}class m extends v{constructor(t){super("");const e=[];let i=t;for(;i&&i.token;)e.push(i.token),i=i.parent;const s=e.reverse().map(r=>r.name||"<anonymous>").join(" --> ");this.message=`Circular dependency found: ${s}`}}class L extends v{constructor(t){super("Invalid binding: ",t)}}class k extends m{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const E=class E{constructor(t,e){o(this,"container");o(this,"context");o(this,"token");o(this,"type",p.INVALID);o(this,"status",f.DEFAULT);o(this,"classValue");o(this,"constantValue");o(this,"dynamicValue");o(this,"cache");o(this,"postConstructResult",D);o(this,"transient",!1);o(this,"onActivationHandler");o(this,"onDeactivationHandler");this.container=e,this.context={container:this.container},this.token=t}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t){const e=this.onActivationHandler?this.onActivationHandler(this.context,t):t;return this.container.activate(e,this.token)}deactivate(){this.onDeactivationHandler&&this.onDeactivationHandler(this.cache)}to(t){return this.type=p.INSTANCE,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=p.CONSTANT,this.constantValue=t,this}toDynamicValue(t){return this.type=p.DYNAMIC,this.dynamicValue=t,this}inTransientScope(){return this.transient=!0,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(f.INITING===this.status)throw new m(t);if(f.ACTIVATED===this.status)if(this.transient)this.status=f.DEFAULT;else return this.cache;const e=E._resolvers[this.type];if(e)return this[e](t);throw new L(this.token)}_getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(i=>e.includes(i.token)):typeof e=="function"?t.filter(e):[]}_postConstruct(t,e){if(p.INSTANCE===this.type){const{key:i,value:s}=V(this.classValue)||{};if(i)if(s){const r=e.filter(c=>p.INSTANCE===(c==null?void 0:c.type)),a=this._getAwaitBindings(r,s);for(const c of a)if(c&&c.postConstructResult===D)throw new k({token:c.token,parent:t});const l=a.map(c=>c.postConstructResult);this.postConstructResult=Promise.all(l).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(p.INSTANCE===this.type){const{key:t}=j(this.classValue)||{};t&&this._execute(t)}I._instanceContainerMap.delete(this.cache),this.container=null,this.context=null,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=D,this.onActivationHandler=void 0,this.onDeactivationHandler=void 0}_execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}_resolveInstanceValue(t){this.status=f.INITING;const e=this._createInstance();this.cache=this.activate(e),this.status=f.ACTIVATED,this._registerInstance();const{properties:i,bindings:s}=this._getInjectProperties(t);return this._injectProperties(i),this._postConstruct(t,s),this.cache}_createInstance(){const t=this.classValue;return new t}_registerInstance(){I._instanceContainerMap.set(this.cache,this.container)}_injectProperties(t){Object.assign(this.cache,t)}_resolveConstantValue(){return this.status=f.INITING,this.cache=this.activate(this.constantValue),this.status=f.ACTIVATED,this.cache}_resolveDynamicValue(){this.status=f.INITING;const t=this.dynamicValue(this.context);return this.cache=this.activate(t),this.status=f.ACTIVATED,this.cache}_getInjectProperties(t){const e=w(this.classValue)||{},i=Object.keys(e),s=Object.create(null),r=[];for(let a=0;a<i.length;a++){const l=i[a],c=e[l],h=Object.assign({},c);h.parent=t;const d=this.container.get(b(h.inject),h);d===void 0&&h.optional||(s[l]=d),r.push(h.binding)}return{properties:s,bindings:r}}};o(E,"_resolvers",{[p.INSTANCE]:"_resolveInstanceValue",[p.CONSTANT]:"_resolveConstantValue",[p.DYNAMIC]:"_resolveDynamicValue"});let C=E;class B extends v{constructor(t){super("No matching binding found for token: ",t)}}class H extends v{constructor(t){super("Cannot bind token multiple times: ",t)}}const T=class T{constructor(){o(this,"parent");o(this,"children");o(this,"_bindings",new Map);o(this,"_onActivationHandler");o(this,"_onDeactivationHandler")}static getContainerOf(t){return T._instanceContainerMap.get(t)}bind(t){if(this._bindings.has(t))throw new H(t);const e=this._buildBinding(t);return this._bindings.set(t,e),e}unbind(t){if(this._bindings.has(t)){const e=this._getBinding(t);this.deactivate(e),e.deactivate(),e.preDestroy(),this._bindings.delete(t)}}unbindAll(){const t=Array.from(this._bindings.keys());for(const e of t)this.unbind(e)}isCurrentBound(t){return this._bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new T;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e;if(this.children){const i=Array.from(this.children);for(const s of i)s.destroy()}this.unbindAll(),this._bindings.clear(),(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),this.parent=void 0,this.children=void 0,this._onActivationHandler=void 0,this._onDeactivationHandler=void 0}get(t,e={}){return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}getAsync(t,e={}){let i;try{i=this.get(t,e)}catch(r){return Promise.reject(r)}const s=e.binding;return(s==null?void 0:s.postConstructResult)instanceof Promise?s.postConstructResult.then(()=>i):Promise.resolve(i)}_resolveSkipSelf(t,e){return this.parent?(e.skipSelf=!1,this.parent.get(t,e)):this._checkBindingNotFoundError(t,e)}_resolveSelf(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this._checkBindingNotFoundError(t,e)}_resolveDefault(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this.parent?this.parent.get(t,e):this._checkBindingNotFoundError(t,e)}onActivation(t){this._onActivationHandler=t}onDeactivation(t){this._onDeactivationHandler=t}activate(t,e){return this._onActivationHandler?this._onActivationHandler({container:this},t,e):t}deactivate(t){this._onDeactivationHandler&&this._onDeactivationHandler(t.cache,t.token)}_buildBinding(t){return new C(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new B(t)}};o(T,"_instanceContainerMap",new WeakMap);let I=T;class M extends v{constructor(t,e){super(`@LazyInject(${t==null?void 0:t.name}) in class ${e.name} requires a registered container but none was found. Token: `,t)}}function A(n,t){return function(e){return function(i,s){const r=s.name,a=s.metadata;_(a,u.INJECTED_PROPS)||(a[u.INJECTED_PROPS]={});const l=a[u.INJECTED_PROPS];l[r]||(l[r]={}),l[r][n]=e===void 0?t:e}}}function F(n,t){return e=>(i,s)=>{const r=s.name,a=s.metadata;if(_(a,n))throw new Error(t);a[n]={key:r,value:e}}}const K=A(u.INJECT),G=A(u.SELF,!0),$=A(u.SKIP_SELF,!0),Z=A(u.OPTIONAL,!0),q=F(u.POST_CONSTRUCT,N.POST_CONSTRUCT),W=F(u.PRE_DESTROY,N.PRE_DESTROY);function Q(){return function(n,t){const e=t.metadata;P(n,e)}}function X(n,t,e,i){if(e==null)throw new Error(N.LAZY_INJECT_INVALID_TOKEN);const s=Symbol.for(t);Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!_(n,s)){const r=i||I.getContainerOf(n),a=n.constructor;if(!r)throw new M(b(e),a);n[s]=r.get(b(e),{parent:{token:a}})}return n[s]},set(r){n[s]=r}})}function x(n,t){return function(e,i){const s=i.name;i.addInitializer(function(){X(this,s,n,t)})}}function tt(n){return function(t){return x(t,n)}}function et(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const S=Symbol("decorate.metadata");function nt(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,r=typeof s[e]=="function",a=[];_(t,S)||(t[S]={});const l=t[S],c={kind:r?"method":"field",name:e,static:!1,private:!1,addInitializer(d){a.push(d)},metadata:l};let h=r?s[e]:void 0;for(let d=i.length-1;d>=0;d--){const g=i[d](h,c);r&&typeof g=="function"&&(h=g)}if(r&&h!==s[e]&&(s[e]=h),P(t,l),a.length>0){const d=Object.create(s);for(const g of a)g.call(d)}}exports.BaseError=v;exports.Binding=C;exports.BindingNotFoundError=B;exports.BindingNotValidError=L;exports.CircularDependencyError=m;exports.Container=I;exports.ContainerNotFoundError=M;exports.DuplicateBindingError=H;exports.ERRORS=N;exports.Inject=K;exports.Injectable=Q;exports.LazyInject=x;exports.LazyToken=R;exports.Optional=Z;exports.PostConstruct=q;exports.PostConstructError=k;exports.PreDestroy=W;exports.Self=G;exports.SkipSelf=$;exports.Token=J;exports.autobind=et;exports.createLazyInject=tt;exports.decorate=nt;exports.hasOwn=_;exports.isObject=Y;
|
|
1
|
+
"use strict";var z=Object.defineProperty;var U=(n,t,e)=>t in n?z(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var r=(n,t,e)=>U(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},f={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},p={INVALID:"Invalid",INSTANCE:"Instance",CONSTANT:"ConstantValue",DYNAMIC:"DynamicValue"},g={POST_CONSTRUCT:"Multiple @PostConstruct decorators are not allowed in a single class.",PRE_DESTROY:"Multiple @PreDestroy decorators are not allowed in a single class.",INVALID_TOKEN:"@Inject or @LazyInject requires a valid token, but received null or undefined.",LAZY_INJECT_INVALID_TOKEN:"@LazyInject requires a valid token, but received null or undefined."},S=Symbol("UNINITIALIZED");function E(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function Y(n){return n!==null&&typeof n=="object"}const y=new WeakMap;function b(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function m(n,t){y.set(n,t)}function V(n){const t=y.get(n);if(t)return t[l.POST_CONSTRUCT];if(b(n))return V(Object.getPrototypeOf(n))}function j(n){const t=y.get(n);if(t)return t[l.PRE_DESTROY];if(b(n))return j(Object.getPrototypeOf(n))}function w(n){const t=y.get(n),e=t&&E(t,l.INJECTED_PROPS)?t[l.INJECTED_PROPS]:void 0;if(!b(n))return e;const i=w(Object.getPrototypeOf(n));if(i||e)return Object.assign({},i,e)}class J{constructor(t){r(this,"name");this.name=t}}class R{constructor(t){r(this,"_callback");this._callback=t}resolve(){return this._callback()}}function O(n){if(!n)throw new Error(g.INVALID_TOKEN);return n instanceof R?n.resolve():n}class _ extends Error{constructor(e,i){const s=(i==null?void 0:i.name)||"<unknown token>";super(`${e}${s}`);r(this,"token");this.name=this.constructor.name,this.token=i}}class P extends _{constructor(t){super("");const e=[];let i=t;for(;i&&i.token;)e.push(i.token),i=i.parent;const s=e.reverse().map(o=>o.name||"<anonymous>").join(" --> ");this.message=`Circular dependency found: ${s}`}}class L extends _{constructor(t){super("Invalid binding: ",t)}}class k extends P{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}const C=class C{constructor(t,e){r(this,"container");r(this,"context");r(this,"token");r(this,"type",p.INVALID);r(this,"status",f.DEFAULT);r(this,"classValue");r(this,"constantValue");r(this,"dynamicValue");r(this,"cache");r(this,"postConstructResult",S);r(this,"transient",!1);r(this,"onActivationHandler");r(this,"onDeactivationHandler");this.container=e,this.context={container:this.container},this.token=t}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t){const e=this.onActivationHandler?this.onActivationHandler(this.context,t):t;return this.container.activate(e,this.token)}deactivate(){this.onDeactivationHandler&&this.onDeactivationHandler(this.cache)}to(t){return this.type=p.INSTANCE,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=p.CONSTANT,this.constantValue=t,this}toDynamicValue(t){return this.type=p.DYNAMIC,this.dynamicValue=t,this}inTransientScope(){return this.transient=!0,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(f.INITING===this.status)throw new P(t);if(f.ACTIVATED===this.status)if(this.transient)this.status=f.DEFAULT;else return this.cache;const e=C._resolvers[this.type];if(e)return this[e](t);throw new L(this.token)}_getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(i=>e.includes(i.token)):typeof e=="function"?t.filter(e):[]}_postConstruct(t,e){if(p.INSTANCE===this.type){const{key:i,value:s}=V(this.classValue)||{};if(i)if(s){const o=e.filter(u=>p.INSTANCE===(u==null?void 0:u.type)),a=this._getAwaitBindings(o,s);for(const u of a)if(u&&u.postConstructResult===S)throw new k({token:u.token,parent:t});const c=a.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(c).then(()=>this._execute(i))}else this.postConstructResult=this._execute(i);else this.postConstructResult=void 0}}preDestroy(){if(p.INSTANCE===this.type){const{key:t}=j(this.classValue)||{};t&&this._execute(t)}I._instanceContainerMap.delete(this.cache),this.container=null,this.context=null,this.classValue=void 0,this.constantValue=void 0,this.dynamicValue=void 0,this.cache=void 0,this.postConstructResult=S,this.onActivationHandler=void 0,this.onDeactivationHandler=void 0}_execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}_resolveInstanceValue(t){this.status=f.INITING;const e=this._createInstance();this.cache=this.activate(e),this.status=f.ACTIVATED,this._registerInstance();const{properties:i,bindings:s}=this._getInjectProperties(t);return this._injectProperties(i),this._postConstruct(t,s),this.cache}_createInstance(){const t=this.classValue;return new t}_registerInstance(){I._instanceContainerMap.set(this.cache,this.container)}_injectProperties(t){Object.assign(this.cache,t)}_resolveConstantValue(){return this.status=f.INITING,this.cache=this.activate(this.constantValue),this.status=f.ACTIVATED,this.cache}_resolveDynamicValue(){this.status=f.INITING;const t=this.dynamicValue(this.context);return this.cache=this.activate(t),this.status=f.ACTIVATED,this.cache}_getInjectProperties(t){const e=w(this.classValue)||{},i=Object.keys(e),s=Object.create(null),o=[];for(let a=0;a<i.length;a++){const c=i[a],u=e[c],h=Object.assign({},u);h.parent=t;const d=this.container.get(O(h.inject),h);d===void 0&&h.optional||(s[c]=d),o.push(h.binding)}return{properties:s,bindings:o}}};r(C,"_resolvers",{[p.INSTANCE]:"_resolveInstanceValue",[p.CONSTANT]:"_resolveConstantValue",[p.DYNAMIC]:"_resolveDynamicValue"});let T=C;class B extends _{constructor(t){super("No matching binding found for token: ",t)}}class H extends _{constructor(t){super("Cannot bind token multiple times: ",t)}}const v=class v{constructor(){r(this,"parent");r(this,"children");r(this,"_bindings",new Map);r(this,"_onActivationHandler");r(this,"_onDeactivationHandler")}static getContainerOf(t){return v._instanceContainerMap.get(t)}bind(t){if(this._bindings.has(t))throw new H(t);const e=this._buildBinding(t);return this._bindings.set(t,e),e}unbind(t){if(this._bindings.has(t)){const e=this._getBinding(t);this.deactivate(e),e.deactivate(),e.preDestroy(),this._bindings.delete(t)}}unbindAll(){const t=Array.from(this._bindings.keys());for(const e of t)this.unbind(e)}isCurrentBound(t){return this._bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new v;return t.parent=this,this.children||(this.children=new Set),this.children.add(t),t}destroy(){var t,e;if(this.children){const i=Array.from(this.children);for(const s of i)s.destroy()}this.unbindAll(),this._bindings.clear(),(e=(t=this.parent)==null?void 0:t.children)==null||e.delete(this),this.parent=void 0,this.children=void 0,this._onActivationHandler=void 0,this._onDeactivationHandler=void 0}get(t,e={}){return e.skipSelf?this._resolveSkipSelf(t,e):e.self?this._resolveSelf(t,e):this._resolveDefault(t,e)}getAsync(t,e={}){let i;try{i=this.get(t,e)}catch(o){return Promise.reject(o)}const s=e.binding;return(s==null?void 0:s.postConstructResult)instanceof Promise?s.postConstructResult.then(()=>i):Promise.resolve(i)}_resolveSkipSelf(t,e){return this.parent?(e.skipSelf=!1,this.parent.get(t,e)):this._checkBindingNotFoundError(t,e)}_resolveSelf(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this._checkBindingNotFoundError(t,e)}_resolveDefault(t,e){const i=this._getBinding(t);return i?(e.token=t,e.binding=i,i.get(e)):this.parent?this.parent.get(t,e):this._checkBindingNotFoundError(t,e)}onActivation(t){this._onActivationHandler=t}onDeactivation(t){this._onDeactivationHandler=t}activate(t,e){return this._onActivationHandler?this._onActivationHandler({container:this},t,e):t}deactivate(t){this._onDeactivationHandler&&this._onDeactivationHandler(t.cache,t.token)}_buildBinding(t){return new T(t,this)}_getBinding(t){return this._bindings.get(t)}_checkBindingNotFoundError(t,e){if(!e.optional)throw new B(t)}};r(v,"_instanceContainerMap",new WeakMap);let I=v;class M extends _{constructor(t,e){super(`@LazyInject(${t==null?void 0:t.name}) in class ${e.name} requires a registered container but none was found. Token: `,t)}}function D(n,t){return function(e){return function(i,s){const o=s.name,a=s.metadata;E(a,l.INJECTED_PROPS)||(a[l.INJECTED_PROPS]={});const c=a[l.INJECTED_PROPS];c[o]||(c[o]={}),c[o][n]=e===void 0?t:e}}}function F(n,t){return e=>(i,s)=>{const o=s.name,a=s.metadata;if(E(a,n))throw new Error(t);a[n]={key:o,value:e}}}const K=D(l.INJECT),G=D(l.SELF,!0),$=D(l.SKIP_SELF,!0),Z=D(l.OPTIONAL,!0),q=F(l.POST_CONSTRUCT,g.POST_CONSTRUCT),W=F(l.PRE_DESTROY,g.PRE_DESTROY);function Q(){return function(n,t){const e=t.metadata;m(n,e)}}function X(n,t,e,i){if(e==null)throw new Error(g.LAZY_INJECT_INVALID_TOKEN);let s,o=!1;Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get(){if(!o){const a=i||I.getContainerOf(n),c=n.constructor;if(!a)throw new M(O(e),c);s=a.get(O(e),{parent:{token:c}}),o=!0}return s},set(a){s=a,o=!0}})}function x(n,t){return function(e,i){const s=i.name;i.addInitializer(function(){X(this,s,n,t)})}}function tt(n){return function(t){return x(t,n)}}function et(n,t){const e=t.name;t.addInitializer(function(){this[e]=n.bind(this)})}const A=new WeakMap;function nt(n,t,e){const i=Array.isArray(n)?n:[n],s=t.prototype,o=typeof s[e]=="function",a=[];A.has(t)||A.set(t,{});const c=A.get(t),u={kind:o?"method":"field",name:e,static:!1,private:!1,addInitializer(d){a.push(d)},metadata:c};let h=o?s[e]:void 0;for(let d=i.length-1;d>=0;d--){const N=i[d](h,u);o&&typeof N=="function"&&(h=N)}if(o&&h!==s[e]&&(s[e]=h),m(t,c),a.length>0){const d=Object.create(s);for(const N of a)N.call(d)}}exports.BaseError=_;exports.Binding=T;exports.BindingNotFoundError=B;exports.BindingNotValidError=L;exports.CircularDependencyError=P;exports.Container=I;exports.ContainerNotFoundError=M;exports.DuplicateBindingError=H;exports.ERRORS=g;exports.Inject=K;exports.Injectable=Q;exports.LazyInject=x;exports.LazyToken=R;exports.Optional=Z;exports.PostConstruct=q;exports.PostConstructError=k;exports.PreDestroy=W;exports.Self=G;exports.SkipSelf=$;exports.Token=J;exports.autobind=et;exports.createLazyInject=tt;exports.decorate=nt;exports.hasOwn=E;exports.isObject=Y;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var L = Object.defineProperty;
|
|
2
2
|
var k = (n, t, e) => t in n ? L(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
-
var
|
|
4
|
-
const
|
|
3
|
+
var r = (n, t, e) => k(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
const l = {
|
|
5
5
|
// 记录实例属性装饰器对应的数据的键
|
|
6
6
|
INJECTED_PROPS: "injected:props",
|
|
7
7
|
// Inject 装饰器的键
|
|
@@ -33,37 +33,37 @@ const u = {
|
|
|
33
33
|
INVALID_TOKEN: "@Inject or @LazyInject requires a valid token, but received null or undefined.",
|
|
34
34
|
// 用于 decorator.ts 的 defineLazyProperty —— 无效 token
|
|
35
35
|
LAZY_INJECT_INVALID_TOKEN: "@LazyInject requires a valid token, but received null or undefined."
|
|
36
|
-
},
|
|
37
|
-
function
|
|
36
|
+
}, y = Symbol("UNINITIALIZED");
|
|
37
|
+
function m(n, t) {
|
|
38
38
|
return Object.prototype.hasOwnProperty.call(n, t);
|
|
39
39
|
}
|
|
40
40
|
function K(n) {
|
|
41
41
|
return n !== null && typeof n == "object";
|
|
42
42
|
}
|
|
43
|
-
const
|
|
44
|
-
function
|
|
43
|
+
const C = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
function O(n) {
|
|
45
45
|
return typeof n == "function" && Object.getPrototypeOf(n) !== Function.prototype;
|
|
46
46
|
}
|
|
47
|
-
function
|
|
48
|
-
|
|
47
|
+
function b(n, t) {
|
|
48
|
+
C.set(n, t);
|
|
49
49
|
}
|
|
50
50
|
function P(n) {
|
|
51
|
-
const t =
|
|
51
|
+
const t = C.get(n);
|
|
52
52
|
if (t)
|
|
53
|
-
return t[
|
|
54
|
-
if (
|
|
53
|
+
return t[l.POST_CONSTRUCT];
|
|
54
|
+
if (O(n))
|
|
55
55
|
return P(Object.getPrototypeOf(n));
|
|
56
56
|
}
|
|
57
57
|
function V(n) {
|
|
58
|
-
const t =
|
|
58
|
+
const t = C.get(n);
|
|
59
59
|
if (t)
|
|
60
|
-
return t[
|
|
61
|
-
if (
|
|
60
|
+
return t[l.PRE_DESTROY];
|
|
61
|
+
if (O(n))
|
|
62
62
|
return V(Object.getPrototypeOf(n));
|
|
63
63
|
}
|
|
64
64
|
function w(n) {
|
|
65
|
-
const t =
|
|
66
|
-
if (!
|
|
65
|
+
const t = C.get(n), e = t && m(t, l.INJECTED_PROPS) ? t[l.INJECTED_PROPS] : void 0;
|
|
66
|
+
if (!O(n))
|
|
67
67
|
return e;
|
|
68
68
|
const i = w(Object.getPrototypeOf(n));
|
|
69
69
|
if (i || e)
|
|
@@ -72,44 +72,44 @@ function w(n) {
|
|
|
72
72
|
class G {
|
|
73
73
|
constructor(t) {
|
|
74
74
|
// 仅类型层面存在,无运行时开销
|
|
75
|
-
|
|
75
|
+
r(this, "name");
|
|
76
76
|
this.name = t;
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
class B {
|
|
80
80
|
constructor(t) {
|
|
81
|
-
|
|
81
|
+
r(this, "_callback");
|
|
82
82
|
this._callback = t;
|
|
83
83
|
}
|
|
84
84
|
resolve() {
|
|
85
85
|
return this._callback();
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
-
function
|
|
88
|
+
function D(n) {
|
|
89
89
|
if (!n)
|
|
90
90
|
throw new Error(g.INVALID_TOKEN);
|
|
91
91
|
return n instanceof B ? n.resolve() : n;
|
|
92
92
|
}
|
|
93
|
-
class
|
|
93
|
+
class v extends Error {
|
|
94
94
|
constructor(e, i) {
|
|
95
95
|
const s = (i == null ? void 0 : i.name) || "<unknown token>";
|
|
96
96
|
super(`${e}${s}`);
|
|
97
|
-
|
|
97
|
+
r(this, "token");
|
|
98
98
|
this.name = this.constructor.name, this.token = i;
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
class R extends
|
|
101
|
+
class R extends v {
|
|
102
102
|
constructor(t) {
|
|
103
103
|
super("");
|
|
104
104
|
const e = [];
|
|
105
105
|
let i = t;
|
|
106
106
|
for (; i && i.token; )
|
|
107
107
|
e.push(i.token), i = i.parent;
|
|
108
|
-
const s = e.reverse().map((
|
|
108
|
+
const s = e.reverse().map((o) => o.name || "<anonymous>").join(" --> ");
|
|
109
109
|
this.message = `Circular dependency found: ${s}`;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
-
class H extends
|
|
112
|
+
class H extends v {
|
|
113
113
|
constructor(t) {
|
|
114
114
|
super("Invalid binding: ", t);
|
|
115
115
|
}
|
|
@@ -119,22 +119,22 @@ class M extends R {
|
|
|
119
119
|
super(t), this.name = "CircularDependencyError inside @PostConstruct";
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
const
|
|
122
|
+
const T = class T {
|
|
123
123
|
constructor(t, e) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
r(this, "container");
|
|
125
|
+
r(this, "context");
|
|
126
|
+
r(this, "token");
|
|
127
|
+
r(this, "type", p.INVALID);
|
|
128
|
+
r(this, "status", f.DEFAULT);
|
|
129
|
+
r(this, "classValue");
|
|
130
|
+
r(this, "constantValue");
|
|
131
|
+
r(this, "dynamicValue");
|
|
132
|
+
r(this, "cache");
|
|
133
|
+
r(this, "postConstructResult", y);
|
|
134
134
|
// 是否为瞬态作用域,默认 false(单例)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
r(this, "transient", !1);
|
|
136
|
+
r(this, "onActivationHandler");
|
|
137
|
+
r(this, "onDeactivationHandler");
|
|
138
138
|
this.container = e, this.context = { container: this.container }, this.token = t;
|
|
139
139
|
}
|
|
140
140
|
onActivation(t) {
|
|
@@ -178,7 +178,7 @@ const C = class C {
|
|
|
178
178
|
this.status = f.DEFAULT;
|
|
179
179
|
else
|
|
180
180
|
return this.cache;
|
|
181
|
-
const e =
|
|
181
|
+
const e = T._resolvers[this.type];
|
|
182
182
|
if (e)
|
|
183
183
|
return this[e](t);
|
|
184
184
|
throw new H(this.token);
|
|
@@ -204,17 +204,17 @@ const C = class C {
|
|
|
204
204
|
const { key: i, value: s } = P(this.classValue) || {};
|
|
205
205
|
if (i)
|
|
206
206
|
if (s) {
|
|
207
|
-
const
|
|
208
|
-
(
|
|
209
|
-
), a = this._getAwaitBindings(
|
|
210
|
-
for (const
|
|
211
|
-
if (
|
|
207
|
+
const o = e.filter(
|
|
208
|
+
(u) => p.INSTANCE === (u == null ? void 0 : u.type)
|
|
209
|
+
), a = this._getAwaitBindings(o, s);
|
|
210
|
+
for (const u of a)
|
|
211
|
+
if (u && u.postConstructResult === y)
|
|
212
212
|
throw new M({
|
|
213
|
-
token:
|
|
213
|
+
token: u.token,
|
|
214
214
|
parent: t
|
|
215
215
|
});
|
|
216
|
-
const
|
|
217
|
-
this.postConstructResult = Promise.all(
|
|
216
|
+
const c = a.map((u) => u.postConstructResult);
|
|
217
|
+
this.postConstructResult = Promise.all(c).then(
|
|
218
218
|
() => this._execute(i)
|
|
219
219
|
);
|
|
220
220
|
} else
|
|
@@ -228,7 +228,7 @@ const C = class C {
|
|
|
228
228
|
const { key: t } = V(this.classValue) || {};
|
|
229
229
|
t && this._execute(t);
|
|
230
230
|
}
|
|
231
|
-
I._instanceContainerMap.delete(this.cache), this.container = null, this.context = null, this.classValue = void 0, this.constantValue = void 0, this.dynamicValue = void 0, this.cache = void 0, this.postConstructResult =
|
|
231
|
+
I._instanceContainerMap.delete(this.cache), this.container = null, this.context = null, this.classValue = void 0, this.constantValue = void 0, this.dynamicValue = void 0, this.cache = void 0, this.postConstructResult = y, this.onActivationHandler = void 0, this.onDeactivationHandler = void 0;
|
|
232
232
|
}
|
|
233
233
|
_execute(t) {
|
|
234
234
|
const e = this.cache[t];
|
|
@@ -263,43 +263,43 @@ const C = class C {
|
|
|
263
263
|
return this.cache = this.activate(t), this.status = f.ACTIVATED, this.cache;
|
|
264
264
|
}
|
|
265
265
|
_getInjectProperties(t) {
|
|
266
|
-
const e = w(this.classValue) || {}, i = Object.keys(e), s = /* @__PURE__ */ Object.create(null),
|
|
266
|
+
const e = w(this.classValue) || {}, i = Object.keys(e), s = /* @__PURE__ */ Object.create(null), o = [];
|
|
267
267
|
for (let a = 0; a < i.length; a++) {
|
|
268
|
-
const
|
|
268
|
+
const c = i[a], u = e[c], h = Object.assign({}, u);
|
|
269
269
|
h.parent = t;
|
|
270
270
|
const d = this.container.get(
|
|
271
|
-
|
|
271
|
+
D(h.inject),
|
|
272
272
|
h
|
|
273
273
|
);
|
|
274
|
-
d === void 0 && h.optional || (s[
|
|
274
|
+
d === void 0 && h.optional || (s[c] = d), o.push(h.binding);
|
|
275
275
|
}
|
|
276
|
-
return { properties: s, bindings:
|
|
276
|
+
return { properties: s, bindings: o };
|
|
277
277
|
}
|
|
278
278
|
};
|
|
279
279
|
// 类型到解析方法的静态映射表,用于替代 get 方法中的 if-else 链
|
|
280
|
-
|
|
280
|
+
r(T, "_resolvers", {
|
|
281
281
|
[p.INSTANCE]: "_resolveInstanceValue",
|
|
282
282
|
[p.CONSTANT]: "_resolveConstantValue",
|
|
283
283
|
[p.DYNAMIC]: "_resolveDynamicValue"
|
|
284
284
|
});
|
|
285
|
-
let
|
|
286
|
-
class x extends
|
|
285
|
+
let S = T;
|
|
286
|
+
class x extends v {
|
|
287
287
|
constructor(t) {
|
|
288
288
|
super("No matching binding found for token: ", t);
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
|
-
class F extends
|
|
291
|
+
class F extends v {
|
|
292
292
|
constructor(t) {
|
|
293
293
|
super("Cannot bind token multiple times: ", t);
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
296
|
const _ = class _ {
|
|
297
297
|
constructor() {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
298
|
+
r(this, "parent");
|
|
299
|
+
r(this, "children");
|
|
300
|
+
r(this, "_bindings", /* @__PURE__ */ new Map());
|
|
301
|
+
r(this, "_onActivationHandler");
|
|
302
|
+
r(this, "_onDeactivationHandler");
|
|
303
303
|
}
|
|
304
304
|
// 查询实例所属的容器
|
|
305
305
|
static getContainerOf(t) {
|
|
@@ -348,8 +348,8 @@ const _ = class _ {
|
|
|
348
348
|
let i;
|
|
349
349
|
try {
|
|
350
350
|
i = this.get(t, e);
|
|
351
|
-
} catch (
|
|
352
|
-
return Promise.reject(
|
|
351
|
+
} catch (o) {
|
|
352
|
+
return Promise.reject(o);
|
|
353
353
|
}
|
|
354
354
|
const s = e.binding;
|
|
355
355
|
return (s == null ? void 0 : s.postConstructResult) instanceof Promise ? s.postConstructResult.then(() => i) : Promise.resolve(i);
|
|
@@ -381,7 +381,7 @@ const _ = class _ {
|
|
|
381
381
|
this._onDeactivationHandler && this._onDeactivationHandler(t.cache, t.token);
|
|
382
382
|
}
|
|
383
383
|
_buildBinding(t) {
|
|
384
|
-
return new
|
|
384
|
+
return new S(t, this);
|
|
385
385
|
}
|
|
386
386
|
_getBinding(t) {
|
|
387
387
|
return this._bindings.get(t);
|
|
@@ -397,9 +397,9 @@ const _ = class _ {
|
|
|
397
397
|
// 同一对象可能通过 toConstantValue 被绑定到多个容器,WeakMap 只能保留最后一次映射,
|
|
398
398
|
// 会导致 @LazyInject 从错误的容器解析依赖。
|
|
399
399
|
// 由于 Instance 类型每次都通过 new ClassName() 创建新实例,不存在同一实例被多个容器注册的覆盖风险
|
|
400
|
-
|
|
400
|
+
r(_, "_instanceContainerMap", /* @__PURE__ */ new WeakMap());
|
|
401
401
|
let I = _;
|
|
402
|
-
class z extends
|
|
402
|
+
class z extends v {
|
|
403
403
|
constructor(t, e) {
|
|
404
404
|
super(
|
|
405
405
|
`@LazyInject(${t == null ? void 0 : t.name}) in class ${e.name} requires a registered container but none was found. Token: `,
|
|
@@ -407,57 +407,57 @@ class z extends T {
|
|
|
407
407
|
);
|
|
408
408
|
}
|
|
409
409
|
}
|
|
410
|
-
function
|
|
410
|
+
function E(n, t) {
|
|
411
411
|
return function(e) {
|
|
412
412
|
return function(i, s) {
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
const
|
|
416
|
-
|
|
413
|
+
const o = s.name, a = s.metadata;
|
|
414
|
+
m(a, l.INJECTED_PROPS) || (a[l.INJECTED_PROPS] = {});
|
|
415
|
+
const c = a[l.INJECTED_PROPS];
|
|
416
|
+
c[o] || (c[o] = {}), c[o][n] = e === void 0 ? t : e;
|
|
417
417
|
};
|
|
418
418
|
};
|
|
419
419
|
}
|
|
420
420
|
function j(n, t) {
|
|
421
421
|
return (e) => (i, s) => {
|
|
422
|
-
const
|
|
423
|
-
if (
|
|
422
|
+
const o = s.name, a = s.metadata;
|
|
423
|
+
if (m(a, n))
|
|
424
424
|
throw new Error(t);
|
|
425
|
-
a[n] = { key:
|
|
425
|
+
a[n] = { key: o, value: e };
|
|
426
426
|
};
|
|
427
427
|
}
|
|
428
|
-
const $ =
|
|
429
|
-
|
|
428
|
+
const $ = E(l.INJECT), Z = E(l.SELF, !0), q = E(l.SKIP_SELF, !0), W = E(l.OPTIONAL, !0), Q = j(
|
|
429
|
+
l.POST_CONSTRUCT,
|
|
430
430
|
g.POST_CONSTRUCT
|
|
431
431
|
), X = j(
|
|
432
|
-
|
|
432
|
+
l.PRE_DESTROY,
|
|
433
433
|
g.PRE_DESTROY
|
|
434
434
|
);
|
|
435
435
|
function tt() {
|
|
436
436
|
return function(n, t) {
|
|
437
437
|
const e = t.metadata;
|
|
438
|
-
|
|
438
|
+
b(n, e);
|
|
439
439
|
};
|
|
440
440
|
}
|
|
441
441
|
function U(n, t, e, i) {
|
|
442
442
|
if (e == null)
|
|
443
443
|
throw new Error(g.LAZY_INJECT_INVALID_TOKEN);
|
|
444
|
-
|
|
444
|
+
let s, o = !1;
|
|
445
445
|
Object.defineProperty(n, t, {
|
|
446
446
|
configurable: !0,
|
|
447
447
|
enumerable: !0,
|
|
448
448
|
get() {
|
|
449
|
-
if (!
|
|
450
|
-
const
|
|
451
|
-
if (!
|
|
452
|
-
throw new z(
|
|
453
|
-
|
|
454
|
-
parent: { token:
|
|
455
|
-
});
|
|
449
|
+
if (!o) {
|
|
450
|
+
const a = i || I.getContainerOf(n), c = n.constructor;
|
|
451
|
+
if (!a)
|
|
452
|
+
throw new z(D(e), c);
|
|
453
|
+
s = a.get(D(e), {
|
|
454
|
+
parent: { token: c }
|
|
455
|
+
}), o = !0;
|
|
456
456
|
}
|
|
457
|
-
return
|
|
457
|
+
return s;
|
|
458
458
|
},
|
|
459
|
-
set(
|
|
460
|
-
|
|
459
|
+
set(a) {
|
|
460
|
+
s = a, o = !0;
|
|
461
461
|
}
|
|
462
462
|
});
|
|
463
463
|
}
|
|
@@ -480,34 +480,34 @@ function nt(n, t) {
|
|
|
480
480
|
this[e] = n.bind(this);
|
|
481
481
|
});
|
|
482
482
|
}
|
|
483
|
-
const
|
|
483
|
+
const A = /* @__PURE__ */ new WeakMap();
|
|
484
484
|
function it(n, t, e) {
|
|
485
|
-
const i = Array.isArray(n) ? n : [n], s = t.prototype,
|
|
486
|
-
|
|
487
|
-
const
|
|
488
|
-
kind:
|
|
485
|
+
const i = Array.isArray(n) ? n : [n], s = t.prototype, o = typeof s[e] == "function", a = [];
|
|
486
|
+
A.has(t) || A.set(t, {});
|
|
487
|
+
const c = A.get(t), u = {
|
|
488
|
+
kind: o ? "method" : "field",
|
|
489
489
|
name: e,
|
|
490
490
|
static: !1,
|
|
491
491
|
private: !1,
|
|
492
492
|
addInitializer(d) {
|
|
493
493
|
a.push(d);
|
|
494
494
|
},
|
|
495
|
-
metadata:
|
|
495
|
+
metadata: c
|
|
496
496
|
};
|
|
497
|
-
let h =
|
|
497
|
+
let h = o ? s[e] : void 0;
|
|
498
498
|
for (let d = i.length - 1; d >= 0; d--) {
|
|
499
|
-
const N = i[d](h,
|
|
500
|
-
|
|
499
|
+
const N = i[d](h, u);
|
|
500
|
+
o && typeof N == "function" && (h = N);
|
|
501
501
|
}
|
|
502
|
-
if (
|
|
502
|
+
if (o && h !== s[e] && (s[e] = h), b(t, c), a.length > 0) {
|
|
503
503
|
const d = Object.create(s);
|
|
504
504
|
for (const N of a)
|
|
505
505
|
N.call(d);
|
|
506
506
|
}
|
|
507
507
|
}
|
|
508
508
|
export {
|
|
509
|
-
|
|
510
|
-
|
|
509
|
+
v as BaseError,
|
|
510
|
+
S as Binding,
|
|
511
511
|
x as BindingNotFoundError,
|
|
512
512
|
H as BindingNotValidError,
|
|
513
513
|
R as CircularDependencyError,
|
|
@@ -529,6 +529,6 @@ export {
|
|
|
529
529
|
nt as autobind,
|
|
530
530
|
et as createLazyInject,
|
|
531
531
|
it as decorate,
|
|
532
|
-
|
|
532
|
+
m as hasOwn,
|
|
533
533
|
K as isObject
|
|
534
534
|
};
|