UNPKG

@lzptec/di

Version:

A Lightweight dependency manager

6 lines (3 loc) 3.27 kB
import { AsyncContext, ContextToken } from '@lzptec/ctx'; var I=(s,t,e)=>{if(!t.has(s))throw TypeError("Cannot "+e)};var r=(s,t,e)=>(I(s,t,"read from private field"),e?e.call(s):t.get(s)),u=(s,t,e)=>{if(t.has(s))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(s):t.set(s,e);},b=(s,t,e,o)=>(I(s,t,"write to private field"),o?o.call(s,e):t.set(s,e),e);var f=(s,t,e)=>(I(s,t,"access private method"),e);var w,g=class{constructor(t){u(this,w,void 0);b(this,w,t);}get description(){return r(this,w)}get type(){return null}};w=new WeakMap;var k=(n=>(n[n.SINGLETON=0]="SINGLETON",n[n.CONTEXT=1]="CONTEXT",n[n.INSTANCE=2]="INSTANCE",n[n.CONTAINER=3]="CONTAINER",n))(k||{});function C(s){let t;return ()=>(t||(t=s()),t)}var A=["apply","construct","defineProperty","deleteProperty","getOwnPropertyDescriptor","getPrototypeOf","has","isExtensible","ownKeys","preventExtensions","setPrototypeOf","decorate","defineMetadata","getMetadata","hasMetadata","getOwnMetadata","hasOwnMetadata","metadata"],d,h,m,p,x,F,O,S,E,M,N,P,y=class y{constructor(t){u(this,x);u(this,O);u(this,E);u(this,N);u(this,d,new WeakMap);u(this,h,new AsyncContext);u(this,m,void 0);b(this,m,t);}register(t,e,o=0){if(t instanceof g&&typeof e!="function")throw new Error("ContainerKey requires a factory.");if(r(y,p).has(t)||r(this,d).has(t)){let n=f(this,N,P).call(this,t);throw new Error(`${n} is already in use`)}if(t instanceof g){if(typeof e!="function")throw new Error("Factory is required!");return f(this,O,S).call(this,t,e,o)}return f(this,x,F).call(this,t,e,o)}get(t){return f(this,E,M).call(this,()=>t,!1)}lazy(t){return f(this,E,M).call(this,t,!0)}context(t){return r(this,h).run(t)}};d=new WeakMap,h=new WeakMap,m=new WeakMap,p=new WeakMap,x=new WeakSet,F=function(t,e,o){let n;switch(o){case 2:n=()=>typeof e=="function"?e(this):new t;break;case 1:let l=new ContextToken(()=>{});n=()=>(r(this,h).has(l)||r(this,h).set(l,C(()=>typeof e=="function"?e(this):new t)),r(this,h).get(l)());break;case 0:n=C(()=>typeof e=="function"?e(this):new t),r(y,p).set(t,n);return;default:n=C(()=>typeof e=="function"?e(this):new t);break}r(this,d).set(t,n);},O=new WeakSet,S=function(t,e,o){let n;switch(o){case 2:n=()=>e(this);break;case 1:let l=new ContextToken(()=>{});n=()=>(r(this,h).has(l)||r(this,h).set(l,C(()=>e(this))),r(this,h).get(l)());break;case 0:n=C(()=>e(this)),r(y,p).set(t,n);return;default:n=C(()=>e(this));break}r(this,d).set(t,n);},E=new WeakSet,M=function(t,e){let o=()=>{let i=t(),T=r(y,p).get(i),c=this;for(;!T&&c;)T=r(c,d).get(i),c=r(c,m);if(!T){let a=f(this,N,P).call(this,i);throw console.error("Key not found"),console.error(i),new Error(`${a} is not found, did you call register?`)}try{return T()}catch(a){throw a instanceof RangeError&&console.error("Possible circular dependency",i),console.error(a),a}};if(!e)return o();let n={get:(i,T)=>{let c=o(),a=c[T];return typeof a=="function"?a.bind(c):a},set:(i,T,c)=>{let a=o();return a[T]=c,!0}};return A.forEach(i=>{n[i]=(...T)=>{let c=o();return Reflect[i].apply(null,[c,...T.slice(1)])};}),new Proxy({},n)},N=new WeakSet,P=function(t){return t instanceof g?t.description:"name"in t?t.name:t.toString()},u(y,p,new WeakMap);var K=y; export { K as Container, g as ContainerKey, k as Scopes };