@resolid/di 0.2.0 → 0.2.2
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/README.md +1 -1
- package/dist/index.d.ts +3 -6
- package/dist/index.js +1 -1
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Resolid: DI Container Package
|
|
2
2
|
|
|
3
|
-
 
|
|
4
4
|
|
|
5
5
|
<b>[Documentation](https://www.resolid.tech/docs/di)</b> | [Framework Bundle](https://github.com/resolid/framework)
|
|
6
6
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/container/index.d.ts
|
|
2
2
|
type Scope = "singleton" | "transient";
|
|
3
3
|
type BindingConfig = Record<string, unknown>;
|
|
4
|
-
|
|
5
|
-
//#region src/container/index.d.ts
|
|
6
|
-
type Resolve = (name: symbol) => unknown;
|
|
4
|
+
type Resolve = <T>(name: symbol) => Promise<T>;
|
|
7
5
|
type LazyResolve = <T = unknown>(name: symbol) => Readonly<{
|
|
8
6
|
value: T;
|
|
9
7
|
}>;
|
|
@@ -32,8 +30,7 @@ type Container = {
|
|
|
32
30
|
toFunction: ToFunction;
|
|
33
31
|
toFactory: ToFactory;
|
|
34
32
|
};
|
|
35
|
-
resolve:
|
|
36
|
-
lazyResolve: LazyResolve;
|
|
33
|
+
resolve: Resolve;
|
|
37
34
|
} & Disposable;
|
|
38
35
|
declare const createContainer: () => Container;
|
|
39
36
|
//#endregion
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=()=>{let e=new t;return{bind:t=>e.bind(t),resolve:t=>e.resolve(t),
|
|
1
|
+
const e=()=>{let e=new t;return{bind:t=>e.bind(t),resolve:t=>e.resolve(t),dispose:()=>e.dispose()}};var t=class e{#bindings;#singletons;#constructing;#lazyResolveQueue=[];constructor(e,t,n){this.#bindings=e?.getBindings()??new Map,this.#singletons=e?.getSingletons()??new Map,this.#constructing=t?[...n??e?.getConstructing()??[],t]:[]}getBindings(){return this.#bindings}getSingletons(){return this.#singletons}getConstructing(){return this.#constructing}bind(e){return{toValue:t=>{this.#bindings.set(e,{factory:()=>t,scope:`singleton`})},toFunction:t=>{this.#bindings.set(e,{factory:()=>t,scope:`singleton`})},toFactory:(t,n)=>{this.#bindings.set(e,{factory:(e,r)=>t({resolver:{resolve:e,lazyResolve:r},config:n?.config}),scope:n?.scope??`singleton`,config:n?.config})}}}async#resolveBinding(t,n){let r=this.#bindings.get(t);if(!r)throw Error(`No binding found for name: ${t.description??t.toString()}`);let i=r.scope===`singleton`;if(i&&this.#singletons.has(t))return this.#singletons.get(t);let a=new e(this,t,n),o=await r.factory(e=>a.resolve(e),e=>a.lazyResolve(e));return i&&this.#singletons.set(t,o),await a.dequeueLazyResolves(),o}async dequeueLazyResolves(){for(let e of this.#lazyResolveQueue)try{e.resolve(await this.#resolveBinding(e.name,[]))}catch(t){throw Error(`Failed to resolve lazy resolver for name ${e.name.description??e.name.toString()}: ${t instanceof Error?t.message:String(t)}`)}}async resolve(e){if(this.#constructing.includes(e))throw Error(`Circular dependency detected: ${[...this.#constructing,e].map(e=>e.description??e.toString()).join(` -> `)}`);return this.#resolveBinding(e)}lazyResolve(e){let t;return new Promise(t=>{this.#lazyResolveQueue.push({name:e,resolve:e=>{t(e)}})}).then(e=>{t=e}),{get value(){if(!t)throw Error(`Lazy binding is not yet resolved. Do not use lazy-resolved bindings before the binding construction ends.`);return t}}}async dispose(){let e=[];for(let[t,n]of this.#singletons)if(typeof n.dispose==`function`)try{await n.dispose()}catch(n){e.push({name:t,error:n})}if(e.length>0){let t=e.map(({name:e,error:t})=>`${e.description??e.toString()}: ${t instanceof Error?t.message:String(t)}`).join(`; `);throw Error(`Failed to dispose ${e.length} binding(s): ${t}`)}}};export{e as createContainer};
|
package/package.json
CHANGED
|
@@ -4,8 +4,12 @@
|
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"private": false,
|
|
6
6
|
"sideEffects": false,
|
|
7
|
-
"version": "0.2.
|
|
7
|
+
"version": "0.2.2",
|
|
8
8
|
"description": "The Resolid DI Container package.",
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Huijie Wei",
|
|
11
|
+
"email": "hello@resolid.tech"
|
|
12
|
+
},
|
|
9
13
|
"publishConfig": {
|
|
10
14
|
"access": "public",
|
|
11
15
|
"provenance": true
|
|
@@ -20,6 +24,8 @@
|
|
|
20
24
|
"files": [
|
|
21
25
|
"dist"
|
|
22
26
|
],
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
23
29
|
"exports": {
|
|
24
30
|
".": {
|
|
25
31
|
"types": "./dist/index.d.ts",
|