@resolid/di 0.5.0 → 0.5.1
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.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=
|
|
1
|
+
const e=e=>{let n=new t;return n.register(e),{resolve:e=>n.resolve(e),dispose:()=>n.dispose()}};var t=class e{_bindings;_singletons;_constructing;_lazyResolveQueue=[];constructor(e,t,n){this._bindings=e?._bindings??new Map,this._singletons=e?._singletons??new Map,this._constructing=t?[...n??e?._constructing??[],t]:[]}register(e){for(let t of e){let e=t.callable?()=>t.callable:t.factory?(e,n)=>t.factory({resolver:{resolve:e,lazyResolve:n},config:t.config}):()=>t.value;this._bindings.set(t.name,{factory:e,scope:t.scope??`singleton`,config:t.config})}}async _resolveBinding(t,n){let r=this._bindings.get(t);if(!r)throw Error(`No binding found for ${t}.`);let i=r.scope===`singleton`;if(i&&this._singletons.has(t))return this._singletons.get(t);let a=new e(this,t,n),o=r.factory(e=>a.resolve(e),e=>a.lazyResolve(e)),s=o instanceof Promise?await o:o;return i&&this._singletons.set(t,s),await a.dequeueLazyResolves(),s}async dequeueLazyResolves(){for(let e of this._lazyResolveQueue)try{e.resolve(await this._resolveBinding(e.name,[]))}catch(t){throw Error(`Failed to resolve lazy binding ${e.name}: ${t instanceof Error?t.message:String(t)}`)}}async resolve(e){if(this._constructing.includes(e))throw Error(`Circular dependency detected: ${[...this._constructing,e].join(` -> `)}`);return this._resolveBinding(e)}lazyResolve(e){let t={resolved:!1};return this._lazyResolveQueue.push({name:e,resolve:e=>{t.value=e,t.resolved=!0}}),{get value(){if(!t.resolved)throw Error(`Lazy binding is not yet resolved. Avoid accessing it before container construction finishes.`);return t.value}}}async dispose(){let e=0,t=``;for(let[n,r]of this._singletons)if(typeof r.dispose==`function`)try{await r.dispose()}catch(r){e++,t+=`${n}: ${r instanceof Error?r.message:r}; `}if(this._singletons.clear(),e>0)throw Error(`Failed to dispose ${e} binding(s):\n${t.slice(0,-2)}`)}};export{e as createContainer};
|