@resolid/di 0.1.1 → 0.2.0
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.d.ts +3 -5
- package/dist/index.js +1 -1
- package/package.json +9 -5
package/dist/index.d.ts
CHANGED
|
@@ -2,11 +2,6 @@
|
|
|
2
2
|
type Scope = "singleton" | "transient";
|
|
3
3
|
type BindingConfig = Record<string, unknown>;
|
|
4
4
|
//#endregion
|
|
5
|
-
//#region src/utils/index.d.ts
|
|
6
|
-
type Disposable = {
|
|
7
|
-
dispose: () => Promise<void> | void;
|
|
8
|
-
};
|
|
9
|
-
//#endregion
|
|
10
5
|
//#region src/container/index.d.ts
|
|
11
6
|
type Resolve = (name: symbol) => unknown;
|
|
12
7
|
type LazyResolve = <T = unknown>(name: symbol) => Readonly<{
|
|
@@ -28,6 +23,9 @@ type ToFactory = (fn: ({
|
|
|
28
23
|
scope?: Scope;
|
|
29
24
|
config?: BindingConfig;
|
|
30
25
|
}) => void;
|
|
26
|
+
type Disposable = {
|
|
27
|
+
dispose: () => Promise<void> | void;
|
|
28
|
+
};
|
|
31
29
|
type Container = {
|
|
32
30
|
bind: (name: symbol) => {
|
|
33
31
|
toValue: ToValue;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=
|
|
1
|
+
const e=()=>{let e=new t;return{bind:t=>e.bind(t),resolve:t=>e.resolve(t),lazyResolve:t=>e.lazyResolve(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.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.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(String).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.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,12 +4,19 @@
|
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"private": false,
|
|
6
6
|
"sideEffects": false,
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.2.0",
|
|
8
8
|
"description": "The Resolid DI Container package.",
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public",
|
|
11
11
|
"provenance": true
|
|
12
12
|
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"container",
|
|
15
|
+
"ioc",
|
|
16
|
+
"inversion of control",
|
|
17
|
+
"dependency injection",
|
|
18
|
+
"di"
|
|
19
|
+
],
|
|
13
20
|
"files": [
|
|
14
21
|
"dist"
|
|
15
22
|
],
|
|
@@ -21,9 +28,7 @@
|
|
|
21
28
|
},
|
|
22
29
|
"dependencies": {},
|
|
23
30
|
"devDependencies": {},
|
|
24
|
-
"peerDependencies": {
|
|
25
|
-
"typescript": "^5.9.3"
|
|
26
|
-
},
|
|
31
|
+
"peerDependencies": {},
|
|
27
32
|
"engines": {
|
|
28
33
|
"node": "^20.19.0 || ^22.13.0 || >=24"
|
|
29
34
|
},
|
|
@@ -37,7 +42,6 @@
|
|
|
37
42
|
"lint": "eslint .",
|
|
38
43
|
"build": "tsdown",
|
|
39
44
|
"test": "vitest run",
|
|
40
|
-
"bench": "vitest bench",
|
|
41
45
|
"typecheck": "tsc --noEmit"
|
|
42
46
|
}
|
|
43
47
|
}
|