@resolid/di 0.3.2 → 0.4.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/README.md CHANGED
@@ -37,9 +37,9 @@ bun add @resolid/di
37
37
  ```js
38
38
  import { createContainer } from "@resolid/di";
39
39
 
40
- const VALUE = Symbol("VALUE");
41
- const FUNCTION = Symbol("FUNCTION");
42
- const FACTORY = Symbol("FACTORY");
40
+ const VALUE = "VALUE";
41
+ const FUNCTION = "FUNCTION";
42
+ const FACTORY = "FACTORY";
43
43
 
44
44
  const container = createContainer([
45
45
  { name: VALUE, value: "Hello World" },
@@ -87,8 +87,8 @@ console.log(factoryResult); // Output: "Hello World from factory with config"
87
87
  You can create bindings that are resolved lazily, useful for circular dependencies or heavy computations:
88
88
 
89
89
  ```js
90
- const LAZY_A = Symbol("LAZY_A");
91
- const LAZY_B = Symbol("LAZY_B");
90
+ const LAZY_A = "LAZY_A";
91
+ const LAZY_B = "LAZY_B";
92
92
 
93
93
  const container = createContainer([
94
94
  {
@@ -117,8 +117,8 @@ await container.resolve(LAZY_B);
117
117
  ### Circular Dependency Detection
118
118
 
119
119
  ```js
120
- const A = Symbol("A");
121
- const B = Symbol("B");
120
+ const A = "A";
121
+ const B = "B";
122
122
 
123
123
  const container = createContainer([
124
124
  {
@@ -144,7 +144,7 @@ class Resource {
144
144
  }
145
145
  }
146
146
 
147
- const RESOURCE = Symbol("RESOURCE");
147
+ const RESOURCE = "RESOURCE";
148
148
 
149
149
  const container = createContainer([{ name: RESOURCE, value: new Resource() }]);
150
150
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  //#region src/index.d.ts
2
- type Resolve = <T>(name: symbol) => T | Promise<T>;
3
- type LazyResolve = <T = unknown>(name: symbol) => Readonly<{
2
+ type Resolve = <T>(name: string) => Promise<T>;
3
+ type LazyResolve = <T = unknown>(name: string) => Readonly<{
4
4
  value: T;
5
5
  }>;
6
6
  type Scope = "singleton" | "transient";
@@ -9,26 +9,26 @@ type Resolver = {
9
9
  lazyResolve: LazyResolve;
10
10
  };
11
11
  type FactoryConfig = Record<string, unknown>;
12
- type BindingDefinition<T = unknown, Config extends FactoryConfig = FactoryConfig> = {
13
- name: symbol;
14
- value: T;
12
+ type BindingDefinition<T$1 = unknown, Config extends FactoryConfig = FactoryConfig> = {
13
+ name: string;
14
+ value: T$1;
15
15
  callable?: never;
16
16
  factory?: never;
17
17
  scope?: never;
18
18
  config?: never;
19
19
  } | {
20
- name: symbol;
21
- callable: (...args: unknown[]) => T;
20
+ name: string;
21
+ callable: (...args: unknown[]) => T$1;
22
22
  value?: never;
23
23
  factory?: never;
24
24
  scope?: never;
25
25
  config?: never;
26
26
  } | {
27
- name: symbol;
27
+ name: string;
28
28
  factory: (options: {
29
29
  resolver: Resolver;
30
30
  config?: Config;
31
- }) => T | Promise<T>;
31
+ }) => T$1 | Promise<T$1>;
32
32
  scope?: Scope;
33
33
  config?: Config;
34
34
  value?: never;
package/dist/index.js CHANGED
@@ -1 +1 @@
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?.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}register(e){for(let t of e)t.callable?this.#bindings.set(t.name,{factory:()=>t.callable,scope:`singleton`}):t.factory?this.#bindings.set(t.name,{factory:(e,n)=>t.factory({resolver:{resolve:e,lazyResolve:n},config:t.config}),scope:t.scope??`singleton`,config:t.config}):this.#bindings.set(t.name,{factory:()=>t.value,scope:`singleton`})}async#resolveBinding(t,n){let r=this.#bindings.get(t);if(!r)throw Error(`No binding found for ${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 binding ${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. Avoid accessing it before container construction finishes.`);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(this.#singletons.clear(),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):\n${t}`)}}};export{e as createContainer};
1
+ import{env as e}from"node:process";const t=e=>{let t=n(`__RESOLID_CONTAINER`,()=>{let t=new r;return t.register(e),t});return{resolve:e=>t.resolve(e),dispose:()=>t.dispose()}},n=(t,n)=>{if(e.NODE_ENV==`development`){let e=globalThis;if(t in e)return e[t];let r=n();return e[t]=r,r}return n()};var r=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}register(e){for(let t of e)t.callable?this.#bindings.set(t.name,{factory:()=>t.callable,scope:`singleton`}):t.factory?this.#bindings.set(t.name,{factory:(e,n)=>t.factory({resolver:{resolve:e,lazyResolve:n},config:t.config}),scope:t.scope??`singleton`,config:t.config}):this.#bindings.set(t.name,{factory:()=>t.value,scope:`singleton`})}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=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 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;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. Avoid accessing it before container construction finishes.`);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(this.#singletons.clear(),e.length>0){let t=e.map(({name:e,error:t})=>`${e}: ${t instanceof Error?t.message:String(t)}`).join(`; `);throw Error(`Failed to dispose ${e.length} binding(s):\n${t}`)}}};export{t as createContainer};
package/package.json CHANGED
@@ -1,19 +1,8 @@
1
1
  {
2
2
  "name": "@resolid/di",
3
- "type": "module",
4
- "license": "MIT",
3
+ "version": "0.4.0",
5
4
  "private": false,
6
- "sideEffects": false,
7
- "version": "0.3.2",
8
5
  "description": "The Resolid DI Container package.",
9
- "author": {
10
- "name": "Huijie Wei",
11
- "email": "hello@resolid.tech"
12
- },
13
- "publishConfig": {
14
- "access": "public",
15
- "provenance": true
16
- },
17
6
  "keywords": [
18
7
  "resolid",
19
8
  "container",
@@ -22,32 +11,48 @@
22
11
  "di",
23
12
  "dependency injection"
24
13
  ],
25
- "files": [
26
- "dist"
27
- ],
28
- "main": "./dist/index.js",
29
- "types": "./dist/index.d.ts",
14
+ "homepage": "https://www.resolid.tech",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/resolid/framework.git",
18
+ "directory": "packages/di"
19
+ },
20
+ "license": "MIT",
21
+ "author": {
22
+ "name": "Huijie Wei",
23
+ "email": "hello@resolid.tech"
24
+ },
25
+ "sideEffects": false,
26
+ "type": "module",
30
27
  "exports": {
31
28
  ".": {
32
29
  "types": "./dist/index.d.ts",
33
30
  "import": "./dist/index.js"
34
31
  }
35
32
  },
33
+ "main": "./dist/index.js",
34
+ "types": "./dist/index.d.ts",
35
+ "files": [
36
+ "dist"
37
+ ],
36
38
  "dependencies": {},
37
- "devDependencies": {},
39
+ "devDependencies": {
40
+ "@vitest/coverage-v8": "beta",
41
+ "tsdown": "^0.15.7",
42
+ "typescript": "^5.9.3",
43
+ "vitest": "beta"
44
+ },
38
45
  "peerDependencies": {},
39
46
  "engines": {
40
47
  "node": "^20.19.0 || ^22.13.0 || >=24"
41
48
  },
42
- "homepage": "https://www.resolid.tech",
43
- "repository": {
44
- "type": "git",
45
- "url": "git+https://github.com/resolid/framework.git",
46
- "directory": "packages/di"
49
+ "publishConfig": {
50
+ "access": "public",
51
+ "provenance": true
47
52
  },
48
53
  "scripts": {
49
- "lint": "eslint .",
50
54
  "build": "tsdown",
55
+ "lint": "eslint .",
51
56
  "test": "vitest run",
52
57
  "typecheck": "tsc --noEmit"
53
58
  }