@resolid/di 0.1.0 → 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 -130
- package/package.json +10 -6
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,130 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const isDisposable = (obj) => !!obj && typeof obj.dispose === "function";
|
|
3
|
-
const formatChain = (chain, current) => {
|
|
4
|
-
return (current ? [...chain, current] : chain).map(String).join(" -> ");
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
//#endregion
|
|
8
|
-
//#region src/container/index.ts
|
|
9
|
-
const createContainer = () => {
|
|
10
|
-
const result = _createContainer();
|
|
11
|
-
return {
|
|
12
|
-
bind: result.bind,
|
|
13
|
-
resolve: result.resolve,
|
|
14
|
-
lazyResolve: result.lazyResolve,
|
|
15
|
-
dispose: result.dispose
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
const _createContainer = (parent, item, constructing) => {
|
|
19
|
-
const bindings = parent?.bindings ?? /* @__PURE__ */ new Map();
|
|
20
|
-
const singletons = parent?.singletons ?? /* @__PURE__ */ new Map();
|
|
21
|
-
const currentConstructing = item ? [...constructing ?? parent?.constructing ?? [], item] : [];
|
|
22
|
-
const lazyResolveQueue = [];
|
|
23
|
-
const container = {};
|
|
24
|
-
const enqueueLazyResolve = (name) => {
|
|
25
|
-
return new Promise((resolve$1) => {
|
|
26
|
-
const lazy = (value) => {
|
|
27
|
-
resolve$1(value);
|
|
28
|
-
};
|
|
29
|
-
lazyResolveQueue.push({
|
|
30
|
-
name,
|
|
31
|
-
resolve: lazy
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
const dequeueLazyResolves = async () => {
|
|
36
|
-
for (const lazyResolve$1 of lazyResolveQueue) try {
|
|
37
|
-
lazyResolve$1.resolve(await resolveBinding(lazyResolve$1.name, []));
|
|
38
|
-
} catch (e) {
|
|
39
|
-
throw new Error(`Failed to resolve lazy resolver for name ${lazyResolve$1.name.toString()}: ${e instanceof Error ? e.message : String(e)}`);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
const bind = (name) => {
|
|
43
|
-
const toValue = (value) => {
|
|
44
|
-
bindings.set(name, {
|
|
45
|
-
factory: () => value,
|
|
46
|
-
scope: "singleton"
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
const toFunction = (fn) => {
|
|
50
|
-
bindings.set(name, {
|
|
51
|
-
factory: () => fn,
|
|
52
|
-
scope: "singleton"
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
const toFactory = (fn, options) => {
|
|
56
|
-
bindings.set(name, {
|
|
57
|
-
factory: (resolve$1, lazyResolve$1) => {
|
|
58
|
-
return fn({
|
|
59
|
-
resolver: {
|
|
60
|
-
resolve: resolve$1,
|
|
61
|
-
lazyResolve: lazyResolve$1
|
|
62
|
-
},
|
|
63
|
-
config: options?.config
|
|
64
|
-
});
|
|
65
|
-
},
|
|
66
|
-
scope: options?.scope ?? "singleton",
|
|
67
|
-
config: options?.config
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
return {
|
|
71
|
-
toValue,
|
|
72
|
-
toFunction,
|
|
73
|
-
toFactory
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
const resolveBinding = async (name, constructing$1) => {
|
|
77
|
-
const binding = bindings.get(name);
|
|
78
|
-
if (!binding) throw new Error(`No binding found for name: ${name.toString()}`);
|
|
79
|
-
const isSingleton = binding.scope === "singleton";
|
|
80
|
-
if (isSingleton && singletons.has(name)) return singletons.get(name);
|
|
81
|
-
const child = _createContainer(container, name, constructing$1);
|
|
82
|
-
const result = await binding.factory(child.resolve, child.lazyResolve);
|
|
83
|
-
if (isSingleton) singletons.set(name, result);
|
|
84
|
-
await child.dequeueLazyResolves();
|
|
85
|
-
return result;
|
|
86
|
-
};
|
|
87
|
-
const resolve = async (name) => {
|
|
88
|
-
if (currentConstructing.includes(name)) throw new Error(`Circular dependency detected: ${formatChain(currentConstructing, name)}`);
|
|
89
|
-
return resolveBinding(name);
|
|
90
|
-
};
|
|
91
|
-
const lazyResolve = (name) => {
|
|
92
|
-
let value;
|
|
93
|
-
enqueueLazyResolve(name).then((resolved) => {
|
|
94
|
-
value = resolved;
|
|
95
|
-
});
|
|
96
|
-
return { get value() {
|
|
97
|
-
if (!value) throw new Error("Lazy binding is not yet resolved. Do not use lazy-resolved bindings before the binding construction ends.");
|
|
98
|
-
return value;
|
|
99
|
-
} };
|
|
100
|
-
};
|
|
101
|
-
const dispose = async () => {
|
|
102
|
-
const disposeErrors = [];
|
|
103
|
-
for (const [name, instance] of singletons) if (isDisposable(instance)) try {
|
|
104
|
-
await instance.dispose();
|
|
105
|
-
} catch (error) {
|
|
106
|
-
disposeErrors.push({
|
|
107
|
-
name,
|
|
108
|
-
error
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
if (disposeErrors.length > 0) {
|
|
112
|
-
const errorMessages = disposeErrors.map(({ name, error }) => `${name.toString()}: ${error instanceof Error ? error.message : String(error)}`).join("; ");
|
|
113
|
-
throw new Error(`Failed to dispose ${disposeErrors.length} binding(s): ${errorMessages}`);
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
Object.assign(container, {
|
|
117
|
-
bindings,
|
|
118
|
-
singletons,
|
|
119
|
-
constructing: currentConstructing,
|
|
120
|
-
dequeueLazyResolves,
|
|
121
|
-
bind,
|
|
122
|
-
resolve,
|
|
123
|
-
lazyResolve,
|
|
124
|
-
dispose
|
|
125
|
-
});
|
|
126
|
-
return container;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
//#endregion
|
|
130
|
-
export { createContainer };
|
|
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,11 +28,9 @@
|
|
|
21
28
|
},
|
|
22
29
|
"dependencies": {},
|
|
23
30
|
"devDependencies": {},
|
|
24
|
-
"peerDependencies": {
|
|
25
|
-
"typescript": "^5.9.3"
|
|
26
|
-
},
|
|
31
|
+
"peerDependencies": {},
|
|
27
32
|
"engines": {
|
|
28
|
-
"node": "^22.13.0 || >=24"
|
|
33
|
+
"node": "^20.19.0 || ^22.13.0 || >=24"
|
|
29
34
|
},
|
|
30
35
|
"homepage": "https://www.resolid.tech",
|
|
31
36
|
"repository": {
|
|
@@ -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
|
}
|