@fougere/container 0.9.2-alpha.0 → 0.9.3-alpha.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.
@@ -12,10 +12,6 @@ export interface RegisterOptions {
12
12
  */
13
13
  deps?: string[];
14
14
  }
15
- /** Anything holding a resource can say so, and disposing the container says it back. */
16
- export interface Disposable {
17
- dispose(): void | Promise<void>;
18
- }
19
15
  /** DI container interface — the only thing application code sees. */
20
16
  export interface Container {
21
17
  /** The container builds it: `register('UserService', UserService, { deps: ['UserRepository'] })`. */
@@ -33,4 +29,4 @@ export interface Container {
33
29
  /** Children first, then what this scope built, in reverse — refusals travel as one `AggregateError`. */
34
30
  dispose(): Promise<void>;
35
31
  }
36
- //# sourceMappingURL=container.d.ts.map
32
+ //# sourceMappingURL=Container.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Container.d.ts","sourceRoot":"","sources":["../../src/container/Container.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAEjE,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,QAAQ,CAAC,EAAE,WAAW,GAAG,WAAW,CAAC;IAErC;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,qEAAqE;AACrE,MAAM,WAAW,SAAS;IACxB,qGAAqG;IACrG,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC;IAEjF,+GAA+G;IAC/G,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAE/C,yEAAyE;IACzE,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC;IAE5B,kEAAkE;IAClE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAE3B,yEAAyE;IACzE,WAAW,CAAC,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI,CAAC;IAEvD,yEAAyE;IACzE,WAAW,IAAI,SAAS,CAAC;IAEzB,wGAAwG;IACxG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Container.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Container.js","sourceRoot":"","sources":["../../src/container/Container.ts"],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
1
+ /** Anything holding a resource can say so, and disposing the container says it back. */
2
+ export interface Disposable {
3
+ dispose(): void | Promise<void>;
4
+ }
5
+ export declare class Disposables {
6
+ static is(value: unknown): value is Disposable;
7
+ }
8
+ //# sourceMappingURL=Disposable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Disposable.d.ts","sourceRoot":"","sources":["../../src/container/Disposable.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,MAAM,WAAW,UAAU;IACzB,OAAO,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC;AAED,qBAAa,WAAW;IACtB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAM7C;CACF"}
@@ -0,0 +1,8 @@
1
+ export class Disposables {
2
+ static is(value) {
3
+ return (typeof value === 'object' &&
4
+ value !== null &&
5
+ typeof value.dispose === 'function');
6
+ }
7
+ }
8
+ //# sourceMappingURL=Disposable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Disposable.js","sourceRoot":"","sources":["../../src/container/Disposable.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,WAAW;IACtB,MAAM,CAAC,EAAE,CAAC,KAAc;QACtB,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;YACzB,KAAK,KAAK,IAAI;YACd,OAAQ,KAAoB,CAAC,OAAO,KAAK,UAAU,CACpD,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ import type { Container } from './Container.js';
2
+ export declare function createContainer(): Container;
3
+ //# sourceMappingURL=ScopeContainer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ScopeContainer.d.ts","sourceRoot":"","sources":["../../src/container/ScopeContainer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAgC,MAAM,gBAAgB,CAAC;AAwK9E,wBAAgB,eAAe,IAAI,SAAS,CAE3C"}
@@ -0,0 +1,147 @@
1
+ import { Disposables } from './Disposable.js';
2
+ /** A scope reaches its parent and its children through members only a scope can read. */
3
+ class ScopeContainer {
4
+ parent;
5
+ registry = new Map();
6
+ // In construction order: a thing built later may hold one built earlier, so disposal
7
+ // walks this backwards.
8
+ built = [];
9
+ // Closed by this container, and before `built` — a child may hold what this scope built,
10
+ // never the other way round.
11
+ children = [];
12
+ fallback;
13
+ // The names being built right now, shared by the whole tree: a miss here is answered by the
14
+ // parent and the descent continues there, so only one stack can name the path whole —
15
+ // `child.resolve('A')` reaching a parent's `B` that asks for `A` back reports `A → B → A`.
16
+ resolving;
17
+ constructor(parent) {
18
+ this.parent = parent;
19
+ this.resolving = parent?.resolving ?? [];
20
+ }
21
+ register(name, ctor, options) {
22
+ const lifetime = options?.lifetime ?? 'transient';
23
+ const deps = options?.deps ?? [];
24
+ this.registry.set(name, {
25
+ factory: (c) => new ctor(...deps.map((d) => c.resolve(d))),
26
+ lifetime,
27
+ });
28
+ }
29
+ registerValue(name, value) {
30
+ // A value the container did not build is not the container's to dispose.
31
+ this.registry.set(name, {
32
+ factory: () => value,
33
+ lifetime: 'singleton',
34
+ instance: value,
35
+ });
36
+ }
37
+ resolve(name) {
38
+ const entry = this.registry.get(name);
39
+ // Not found locally — the parent holds it, and holds its instance too.
40
+ if (!entry && this.parent?.entryOf(name)) {
41
+ return this.parent.resolve(name);
42
+ }
43
+ // Nobody holds it. A frond declared in `remotes` registers nothing here, so its
44
+ // façade is fabricated by the fallback rather than found.
45
+ if (!entry) {
46
+ const made = this.fallbackOf()?.(name);
47
+ if (made !== undefined) {
48
+ this.registry.set(name, {
49
+ factory: () => made,
50
+ lifetime: 'singleton',
51
+ instance: made,
52
+ });
53
+ return made;
54
+ }
55
+ throw new Error(`[container] '${name}' is not registered${this.through(name)}`);
56
+ }
57
+ if (entry.instance !== undefined)
58
+ return entry.instance;
59
+ if (this.resolving.includes(name)) {
60
+ throw new Error(`[container] dependency cycle: ${[...this.resolving, name].join(' → ')}`);
61
+ }
62
+ // Popped in a finally, because a constructor that throws leaves the name on the stack
63
+ // otherwise and the next resolution of it reports a cycle that is not there.
64
+ this.resolving.push(name);
65
+ try {
66
+ const value = entry.factory(this);
67
+ // The container disposes what it KEEPS: a transient is handed over and forgotten,
68
+ // and its caller is the one who knows when it is done.
69
+ if (entry.lifetime === 'singleton') {
70
+ entry.instance = value;
71
+ this.remember(value);
72
+ }
73
+ return value;
74
+ }
75
+ finally {
76
+ this.resolving.pop();
77
+ }
78
+ }
79
+ has(name) {
80
+ return this.registry.has(name) || (this.parent?.has(name) ?? false);
81
+ }
82
+ createScope() {
83
+ const child = new ScopeContainer(this);
84
+ this.children.push(child);
85
+ return child;
86
+ }
87
+ async dispose() {
88
+ // Everything is told before anything throws, then the failures travel together.
89
+ // Dropped from the parent first: a scope that closes itself leaves a reference the
90
+ // parent would hold for the life of the process.
91
+ this.parent?.forget(this);
92
+ const failures = [];
93
+ // A copy: closing a child splices it out of `children`, so walking the array
94
+ // itself stepped over every second sibling.
95
+ for (const child of [...this.children].reverse()) {
96
+ try {
97
+ await child.dispose();
98
+ }
99
+ catch (error) {
100
+ failures.push(error);
101
+ }
102
+ }
103
+ this.children.length = 0;
104
+ for (const value of this.built.reverse()) {
105
+ try {
106
+ await value.dispose();
107
+ }
108
+ catch (error) {
109
+ failures.push(error);
110
+ }
111
+ }
112
+ this.built.length = 0;
113
+ this.registry.clear();
114
+ if (failures.length > 0) {
115
+ throw new AggregateError(failures, '[container] one or more disposals failed');
116
+ }
117
+ }
118
+ setFallback(resolve) {
119
+ this.fallback = resolve;
120
+ }
121
+ entryOf(name) {
122
+ return this.registry.get(name) ?? this.parent?.entryOf(name);
123
+ }
124
+ /** Set on the root, honoured from any scope — a scope inherits it by asking upward. */
125
+ fallbackOf() {
126
+ return this.fallback ?? this.parent?.fallbackOf();
127
+ }
128
+ forget(child) {
129
+ const at = this.children.indexOf(child);
130
+ if (at !== -1)
131
+ this.children.splice(at, 1);
132
+ }
133
+ through(name) {
134
+ return this.resolving.length > 0
135
+ ? ` (resolving: ${[...this.resolving, name].join(' → ')})`
136
+ : '';
137
+ }
138
+ remember(value) {
139
+ if (Disposables.is(value))
140
+ this.built.push(value);
141
+ return value;
142
+ }
143
+ }
144
+ export function createContainer() {
145
+ return new ScopeContainer();
146
+ }
147
+ //# sourceMappingURL=ScopeContainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ScopeContainer.js","sourceRoot":"","sources":["../../src/container/ScopeContainer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAmB,MAAM,iBAAiB,CAAC;AAQ/D,yFAAyF;AACzF,MAAM,cAAc;IAcW,MAAM;IAblB,QAAQ,GAAG,IAAI,GAAG,EAAiB,CAAC;IACrD,qFAAqF;IACrF,wBAAwB;IACP,KAAK,GAAc,EAAE,CAAC;IACvC,yFAAyF;IACzF,6BAA6B;IACZ,QAAQ,GAAqB,EAAE,CAAC;IACzC,QAAQ,CAA0C;IAC1D,4FAA4F;IAC5F,sFAAsF;IACtF,2FAA2F;IAC1E,SAAS,CAAW;IAErC,YAA6B,MAAuB;sBAAvB,MAAM;QACjC,IAAI,CAAC,SAAS,GAAG,MAAM,EAAE,SAAS,IAAI,EAAE,CAAC;IAC3C,CA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AAC;IAEO,OAAO,CAAC,IAAY;QAC1B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/D,CAAC;IAED,uFAAuF;IAC/E,UAAU;QAChB,OAAO,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,CAAC;IACpD,CAAC;IAEO,MAAM,CAAC,KAAqB;QAClC,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACxC,IAAI,EAAE,KAAK,CAAC,CAAC;YAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC7C,CAAC;IAEO,OAAO,CAAC,IAAY;QAC1B,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC;YAC9B,CAAC,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG;YAC1D,CAAC,CAAC,EAAE,CAAC;IACT,CAAC;IAEO,QAAQ,CAAI,KAAQ;QAC1B,IAAI,WAAW,CAAC,EAAE,CAAC,KAAK,CAAC;YAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAElD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAED,MAAM,UAAU,eAAe;IAC7B,OAAO,IAAI,cAAc,EAAE,CAAC;AAC9B,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { type Container, type Constructor, } from './container.js';
2
- export { createContainer } from './create.js';
1
+ export { type Container, type Constructor } from './container/Container.js';
2
+ export { createContainer } from './container/ScopeContainer.js';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EACd,KAAK,WAAW,GACjB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5E,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { createContainer } from './create.js';
1
+ export { createContainer } from './container/ScopeContainer.js';
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fougere/container",
3
- "version": "0.9.2-alpha.0",
3
+ "version": "0.9.3-alpha.0",
4
4
  "description": "Fougere's DI container: resolution by type, wired from the AST scan. Zero dependencies.",
5
5
  "keywords": [
6
6
  "fougere",
@@ -1,4 +1,3 @@
1
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
1
  export type Constructor<T = unknown> = new (...args: any[]) => T;
3
2
 
4
3
  export interface RegisterOptions {
@@ -16,11 +15,6 @@ export interface RegisterOptions {
16
15
  deps?: string[];
17
16
  }
18
17
 
19
- /** Anything holding a resource can say so, and disposing the container says it back. */
20
- export interface Disposable {
21
- dispose(): void | Promise<void>;
22
- }
23
-
24
18
  /** DI container interface — the only thing application code sees. */
25
19
  export interface Container {
26
20
  /** The container builds it: `register('UserService', UserService, { deps: ['UserRepository'] })`. */
@@ -0,0 +1,14 @@
1
+ /** Anything holding a resource can say so, and disposing the container says it back. */
2
+ export interface Disposable {
3
+ dispose(): void | Promise<void>;
4
+ }
5
+
6
+ export class Disposables {
7
+ static is(value: unknown): value is Disposable {
8
+ return (
9
+ typeof value === 'object' &&
10
+ value !== null &&
11
+ typeof (value as Disposable).dispose === 'function'
12
+ );
13
+ }
14
+ }
@@ -0,0 +1,171 @@
1
+ import type { Container, Constructor, RegisterOptions } from './Container.js';
2
+ import { Disposables, type Disposable } from './Disposable.js';
3
+
4
+ interface Entry {
5
+ factory: (container: Container) => unknown;
6
+ lifetime: 'singleton' | 'transient';
7
+ instance?: unknown;
8
+ }
9
+
10
+ /** A scope reaches its parent and its children through members only a scope can read. */
11
+ class ScopeContainer implements Container {
12
+ private readonly registry = new Map<string, Entry>();
13
+ // In construction order: a thing built later may hold one built earlier, so disposal
14
+ // walks this backwards.
15
+ private readonly built: unknown[] = [];
16
+ // Closed by this container, and before `built` — a child may hold what this scope built,
17
+ // never the other way round.
18
+ private readonly children: ScopeContainer[] = [];
19
+ private fallback: ((name: string) => unknown) | undefined;
20
+ // The names being built right now, shared by the whole tree: a miss here is answered by the
21
+ // parent and the descent continues there, so only one stack can name the path whole —
22
+ // `child.resolve('A')` reaching a parent's `B` that asks for `A` back reports `A → B → A`.
23
+ private readonly resolving: string[];
24
+
25
+ constructor(private readonly parent?: ScopeContainer) {
26
+ this.resolving = parent?.resolving ?? [];
27
+ }
28
+
29
+ register<T>(name: string, ctor: Constructor<T>, options?: RegisterOptions): void {
30
+ const lifetime = options?.lifetime ?? 'transient';
31
+ const deps = options?.deps ?? [];
32
+ this.registry.set(name, {
33
+ factory: (c) => new ctor(...deps.map((d) => c.resolve(d))),
34
+ lifetime,
35
+ });
36
+ }
37
+
38
+ registerValue<T>(name: string, value: T): void {
39
+ // A value the container did not build is not the container's to dispose.
40
+ this.registry.set(name, {
41
+ factory: () => value,
42
+ lifetime: 'singleton',
43
+ instance: value,
44
+ });
45
+ }
46
+
47
+ resolve<T>(name: string): T {
48
+ const entry = this.registry.get(name);
49
+
50
+ // Not found locally — the parent holds it, and holds its instance too.
51
+ if (!entry && this.parent?.entryOf(name)) {
52
+ return this.parent.resolve<T>(name);
53
+ }
54
+
55
+ // Nobody holds it. A frond declared in `remotes` registers nothing here, so its
56
+ // façade is fabricated by the fallback rather than found.
57
+ if (!entry) {
58
+ const made = this.fallbackOf()?.(name);
59
+ if (made !== undefined) {
60
+ this.registry.set(name, {
61
+ factory: () => made,
62
+ lifetime: 'singleton',
63
+ instance: made,
64
+ });
65
+
66
+ return made as T;
67
+ }
68
+ throw new Error(`[container] '${name}' is not registered${this.through(name)}`);
69
+ }
70
+
71
+ if (entry.instance !== undefined) return entry.instance as T;
72
+
73
+ if (this.resolving.includes(name)) {
74
+ throw new Error(
75
+ `[container] dependency cycle: ${[...this.resolving, name].join(' → ')}`,
76
+ );
77
+ }
78
+
79
+ // Popped in a finally, because a constructor that throws leaves the name on the stack
80
+ // otherwise and the next resolution of it reports a cycle that is not there.
81
+ this.resolving.push(name);
82
+ try {
83
+ const value = entry.factory(this) as T;
84
+ // The container disposes what it KEEPS: a transient is handed over and forgotten,
85
+ // and its caller is the one who knows when it is done.
86
+ if (entry.lifetime === 'singleton') {
87
+ entry.instance = value;
88
+ this.remember(value);
89
+ }
90
+
91
+ return value;
92
+ } finally {
93
+ this.resolving.pop();
94
+ }
95
+ }
96
+
97
+ has(name: string): boolean {
98
+ return this.registry.has(name) || (this.parent?.has(name) ?? false);
99
+ }
100
+
101
+ createScope(): Container {
102
+ const child = new ScopeContainer(this);
103
+ this.children.push(child);
104
+
105
+ return child;
106
+ }
107
+
108
+ async dispose(): Promise<void> {
109
+ // Everything is told before anything throws, then the failures travel together.
110
+ // Dropped from the parent first: a scope that closes itself leaves a reference the
111
+ // parent would hold for the life of the process.
112
+ this.parent?.forget(this);
113
+ const failures: unknown[] = [];
114
+ // A copy: closing a child splices it out of `children`, so walking the array
115
+ // itself stepped over every second sibling.
116
+ for (const child of [...this.children].reverse()) {
117
+ try {
118
+ await child.dispose();
119
+ } catch (error) {
120
+ failures.push(error);
121
+ }
122
+ }
123
+ this.children.length = 0;
124
+ for (const value of this.built.reverse()) {
125
+ try {
126
+ await (value as Disposable).dispose();
127
+ } catch (error) {
128
+ failures.push(error);
129
+ }
130
+ }
131
+ this.built.length = 0;
132
+ this.registry.clear();
133
+ if (failures.length > 0) {
134
+ throw new AggregateError(failures, '[container] one or more disposals failed');
135
+ }
136
+ }
137
+
138
+ setFallback(resolve: (name: string) => unknown): void {
139
+ this.fallback = resolve;
140
+ }
141
+
142
+ private entryOf(name: string): Entry | undefined {
143
+ return this.registry.get(name) ?? this.parent?.entryOf(name);
144
+ }
145
+
146
+ /** Set on the root, honoured from any scope — a scope inherits it by asking upward. */
147
+ private fallbackOf(): ((name: string) => unknown) | undefined {
148
+ return this.fallback ?? this.parent?.fallbackOf();
149
+ }
150
+
151
+ private forget(child: ScopeContainer): void {
152
+ const at = this.children.indexOf(child);
153
+ if (at !== -1) this.children.splice(at, 1);
154
+ }
155
+
156
+ private through(name: string): string {
157
+ return this.resolving.length > 0
158
+ ? ` (resolving: ${[...this.resolving, name].join(' → ')})`
159
+ : '';
160
+ }
161
+
162
+ private remember<T>(value: T): T {
163
+ if (Disposables.is(value)) this.built.push(value);
164
+
165
+ return value;
166
+ }
167
+ }
168
+
169
+ export function createContainer(): Container {
170
+ return new ScopeContainer();
171
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,3 @@
1
- export {
2
- type Container,
3
- type Constructor,
4
- } from './container.js';
1
+ export { type Container, type Constructor } from './container/Container.js';
5
2
 
6
- export { createContainer } from './create.js';
3
+ export { createContainer } from './container/ScopeContainer.js';
@@ -1 +0,0 @@
1
- {"version":3,"file":"container.d.ts","sourceRoot":"","sources":["../src/container.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAEjE,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,QAAQ,CAAC,EAAE,WAAW,GAAG,WAAW,CAAC;IAErC;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,wFAAwF;AACxF,MAAM,WAAW,UAAU;IACzB,OAAO,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC;AAED,qEAAqE;AACrE,MAAM,WAAW,SAAS;IACxB,qGAAqG;IACrG,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC;IAEjF,+GAA+G;IAC/G,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAE/C,yEAAyE;IACzE,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC;IAE5B,kEAAkE;IAClE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAE3B,yEAAyE;IACzE,WAAW,CAAC,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI,CAAC;IAEvD,yEAAyE;IACzE,WAAW,IAAI,SAAS,CAAC;IAEzB,wGAAwG;IACxG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
package/dist/container.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=container.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"container.js","sourceRoot":"","sources":["../src/container.ts"],"names":[],"mappings":""}
package/dist/create.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { Container } from './container.js';
2
- export declare function createContainer(): Container;
3
- //# sourceMappingURL=create.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAA4C,MAAM,gBAAgB,CAAC;AAqK1F,wBAAgB,eAAe,IAAI,SAAS,CAE3C"}
package/dist/create.js DELETED
@@ -1,136 +0,0 @@
1
- const isDisposable = (value) => typeof value === 'object' && value !== null &&
2
- typeof value.dispose === 'function';
3
- function createScope(parent) {
4
- const registry = new Map();
5
- // In construction order: a thing built later may hold one built earlier, so disposal
6
- // walks this backwards.
7
- const built = [];
8
- // Closed by this container, and before `built` — a child may hold what this scope built,
9
- // never the other way round.
10
- const children = [];
11
- let fallback;
12
- // The names being built right now, shared by the whole tree: a miss here is answered by the
13
- // parent and the descent continues there, so only one stack can name the path whole —
14
- // `child.resolve('A')` reaching a parent's `B` that asks for `A` back reports `A → B → A`.
15
- const resolving = parent?._resolving() ?? [];
16
- const through = (name) => resolving.length > 0 ? ` (resolving: ${[...resolving, name].join(' → ')})` : '';
17
- const remember = (value) => {
18
- if (isDisposable(value))
19
- built.push(value);
20
- return value;
21
- };
22
- const container = {
23
- register(name, ctor, options) {
24
- const lifetime = options?.lifetime ?? 'transient';
25
- const deps = options?.deps ?? [];
26
- registry.set(name, {
27
- factory: (c) => new ctor(...deps.map((d) => c.resolve(d))),
28
- lifetime,
29
- });
30
- },
31
- registerValue(name, value) {
32
- // A value the container did not build is not the container's to dispose.
33
- registry.set(name, { factory: () => value, lifetime: 'singleton', instance: value });
34
- },
35
- resolve(name) {
36
- let entry = registry.get(name);
37
- // Not found locally — the parent holds it, and holds its instance too.
38
- if (!entry && parent && parent._getEntry(name)) {
39
- return parent.resolve(name);
40
- }
41
- // Nobody holds it. A frond declared in `remotes` registers nothing here, so its
42
- // façade is fabricated by the fallback rather than found.
43
- if (!entry) {
44
- const made = container._getFallback()?.(name);
45
- if (made !== undefined) {
46
- registry.set(name, { factory: () => made, lifetime: 'singleton', instance: made });
47
- return made;
48
- }
49
- throw new Error(`[container] '${name}' is not registered${through(name)}`);
50
- }
51
- if (entry.instance !== undefined)
52
- return entry.instance;
53
- if (resolving.includes(name)) {
54
- throw new Error(`[container] dependency cycle: ${[...resolving, name].join(' → ')}`);
55
- }
56
- // Popped in a finally, because a constructor that throws leaves the name on the stack
57
- // otherwise and the next resolution of it reports a cycle that is not there.
58
- resolving.push(name);
59
- try {
60
- const value = entry.factory(container);
61
- // The container disposes what it KEEPS: a transient is handed over and forgotten,
62
- // and its caller is the one who knows when it is done.
63
- if (entry.lifetime === 'singleton') {
64
- entry.instance = value;
65
- remember(value);
66
- }
67
- return value;
68
- }
69
- finally {
70
- resolving.pop();
71
- }
72
- },
73
- has(name) {
74
- return registry.has(name) || (parent?.has(name) ?? false);
75
- },
76
- createScope() {
77
- const child = createScope(container);
78
- children.push(child);
79
- return child;
80
- },
81
- async dispose() {
82
- // Everything is told before anything throws, then the failures travel together.
83
- // Dropped from the parent first: a scope that closes itself leaves a reference the
84
- // parent would hold for the life of the process.
85
- parent?._forget(container);
86
- const failures = [];
87
- // A copy: closing a child splices it out of `children`, so walking the array
88
- // itself stepped over every second sibling.
89
- for (const child of [...children].reverse()) {
90
- try {
91
- await child.dispose();
92
- }
93
- catch (error) {
94
- failures.push(error);
95
- }
96
- }
97
- children.length = 0;
98
- for (const value of built.reverse()) {
99
- try {
100
- await value.dispose();
101
- }
102
- catch (error) {
103
- failures.push(error);
104
- }
105
- }
106
- built.length = 0;
107
- registry.clear();
108
- if (failures.length > 0) {
109
- throw new AggregateError(failures, '[container] one or more disposals failed');
110
- }
111
- },
112
- setFallback(resolve) {
113
- fallback = resolve;
114
- },
115
- _getEntry(name) {
116
- return registry.get(name) ?? parent?._getEntry(name);
117
- },
118
- _resolving() {
119
- return resolving;
120
- },
121
- _forget(child) {
122
- const at = children.indexOf(child);
123
- if (at !== -1)
124
- children.splice(at, 1);
125
- },
126
- /** Set on the root, honoured from any scope — a scope inherits it by asking upward. */
127
- _getFallback() {
128
- return fallback ?? parent?._getFallback();
129
- },
130
- };
131
- return container;
132
- }
133
- export function createContainer() {
134
- return createScope();
135
- }
136
- //# sourceMappingURL=create.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAeA,MAAM,YAAY,GAAG,CAAC,KAAc,EAAuB,EAAE,CAC3D,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;IAC3C,OAAQ,KAAoB,CAAC,OAAO,KAAK,UAAU,CAAC;AAEtD,SAAS,WAAW,CAAC,MAAuB;IAC1C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAiB,CAAC;IAC1C,qFAAqF;IACrF,wBAAwB;IACxB,MAAM,KAAK,GAAc,EAAE,CAAC;IAC5B,yFAAyF;IACzF,6BAA6B;IAC7B,MAAM,QAAQ,GAAqB,EAAE,CAAC;IACtC,IAAI,QAAiD,CAAC;IACtD,4FAA4F;IAC5F,sFAAsF;IACtF,2FAA2F;IAC3F,MAAM,SAAS,GAAa,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;IAEvD,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE,CAC/B,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;IAElF,MAAM,QAAQ,GAAG,CAAI,KAAQ,EAAK,EAAE;QAClC,IAAI,YAAY,CAAC,KAAK,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3C,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IAEF,MAAM,SAAS,GAAmB;QAChC,QAAQ,CAAI,IAAY,EAAE,IAAoB,EAAE,OAAyB;YACvE,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,IAAI,WAAW,CAAC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AC,EAAE,EAAE,CAAC,CAAC,CAAC;QACxC,CAAC;QAED,uFAAuF;QACvF,YAAY;YACV,OAAO,QAAQ,IAAI,MAAM,EAAE,YAAY,EAAE,CAAC;QAC5C,CAAC;KACF,CAAC;IAEF,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,eAAe;IAC7B,OAAO,WAAW,EAAE,CAAC;AACvB,CAAC"}
package/src/create.ts DELETED
@@ -1,168 +0,0 @@
1
- import type { Container, RegisterOptions, Constructor, Disposable } from './container.js';
2
-
3
- interface Entry {
4
- factory: (container: Container) => unknown;
5
- lifetime: 'singleton' | 'transient';
6
- instance?: unknown;
7
- }
8
-
9
- interface ScopeContainer extends Container {
10
- _getEntry(name: string): Entry | undefined;
11
- _getFallback(): ((name: string) => unknown) | undefined;
12
- _forget(child: ScopeContainer): void;
13
- _resolving(): string[];
14
- }
15
-
16
- const isDisposable = (value: unknown): value is Disposable =>
17
- typeof value === 'object' && value !== null &&
18
- typeof (value as Disposable).dispose === 'function';
19
-
20
- function createScope(parent?: ScopeContainer): ScopeContainer {
21
- const registry = new Map<string, Entry>();
22
- // In construction order: a thing built later may hold one built earlier, so disposal
23
- // walks this backwards.
24
- const built: unknown[] = [];
25
- // Closed by this container, and before `built` — a child may hold what this scope built,
26
- // never the other way round.
27
- const children: ScopeContainer[] = [];
28
- let fallback: ((name: string) => unknown) | undefined;
29
- // The names being built right now, shared by the whole tree: a miss here is answered by the
30
- // parent and the descent continues there, so only one stack can name the path whole —
31
- // `child.resolve('A')` reaching a parent's `B` that asks for `A` back reports `A → B → A`.
32
- const resolving: string[] = parent?._resolving() ?? [];
33
-
34
- const through = (name: string) =>
35
- resolving.length > 0 ? ` (resolving: ${[...resolving, name].join(' → ')})` : '';
36
-
37
- const remember = <T>(value: T): T => {
38
- if (isDisposable(value)) built.push(value);
39
- return value;
40
- };
41
-
42
- const container: ScopeContainer = {
43
- register<T>(name: string, ctor: Constructor<T>, options?: RegisterOptions) {
44
- const lifetime = options?.lifetime ?? 'transient';
45
- const deps = options?.deps ?? [];
46
- registry.set(name, {
47
- factory: (c) => new ctor(...deps.map((d) => c.resolve(d))),
48
- lifetime,
49
- });
50
- },
51
-
52
- registerValue<T>(name: string, value: T) {
53
- // A value the container did not build is not the container's to dispose.
54
- registry.set(name, { factory: () => value, lifetime: 'singleton', instance: value });
55
- },
56
-
57
- resolve<T>(name: string): T {
58
- let entry = registry.get(name);
59
-
60
- // Not found locally — the parent holds it, and holds its instance too.
61
- if (!entry && parent && parent._getEntry(name)) {
62
- return parent.resolve<T>(name);
63
- }
64
-
65
- // Nobody holds it. A frond declared in `remotes` registers nothing here, so its
66
- // façade is fabricated by the fallback rather than found.
67
- if (!entry) {
68
- const made = container._getFallback()?.(name);
69
- if (made !== undefined) {
70
- registry.set(name, { factory: () => made, lifetime: 'singleton', instance: made });
71
- return made as T;
72
- }
73
- throw new Error(`[container] '${name}' is not registered${through(name)}`);
74
- }
75
-
76
- if (entry.instance !== undefined) return entry.instance as T;
77
-
78
- if (resolving.includes(name)) {
79
- throw new Error(`[container] dependency cycle: ${[...resolving, name].join(' → ')}`);
80
- }
81
-
82
- // Popped in a finally, because a constructor that throws leaves the name on the stack
83
- // otherwise and the next resolution of it reports a cycle that is not there.
84
- resolving.push(name);
85
- try {
86
- const value = entry.factory(container) as T;
87
- // The container disposes what it KEEPS: a transient is handed over and forgotten,
88
- // and its caller is the one who knows when it is done.
89
- if (entry.lifetime === 'singleton') {
90
- entry.instance = value;
91
- remember(value);
92
- }
93
-
94
- return value;
95
- } finally {
96
- resolving.pop();
97
- }
98
- },
99
-
100
- has(name: string): boolean {
101
- return registry.has(name) || (parent?.has(name) ?? false);
102
- },
103
-
104
- createScope(): Container {
105
- const child = createScope(container);
106
- children.push(child);
107
- return child;
108
- },
109
-
110
- async dispose(): Promise<void> {
111
- // Everything is told before anything throws, then the failures travel together.
112
- // Dropped from the parent first: a scope that closes itself leaves a reference the
113
- // parent would hold for the life of the process.
114
- parent?._forget(container);
115
- const failures: unknown[] = [];
116
- // A copy: closing a child splices it out of `children`, so walking the array
117
- // itself stepped over every second sibling.
118
- for (const child of [...children].reverse()) {
119
- try {
120
- await child.dispose();
121
- } catch (error) {
122
- failures.push(error);
123
- }
124
- }
125
- children.length = 0;
126
- for (const value of built.reverse()) {
127
- try {
128
- await (value as Disposable).dispose();
129
- } catch (error) {
130
- failures.push(error);
131
- }
132
- }
133
- built.length = 0;
134
- registry.clear();
135
- if (failures.length > 0) {
136
- throw new AggregateError(failures, '[container] one or more disposals failed');
137
- }
138
- },
139
-
140
- setFallback(resolve: (name: string) => unknown) {
141
- fallback = resolve;
142
- },
143
-
144
- _getEntry(name: string): Entry | undefined {
145
- return registry.get(name) ?? parent?._getEntry(name);
146
- },
147
-
148
- _resolving() {
149
- return resolving;
150
- },
151
-
152
- _forget(child: ScopeContainer) {
153
- const at = children.indexOf(child);
154
- if (at !== -1) children.splice(at, 1);
155
- },
156
-
157
- /** Set on the root, honoured from any scope — a scope inherits it by asking upward. */
158
- _getFallback() {
159
- return fallback ?? parent?._getFallback();
160
- },
161
- };
162
-
163
- return container;
164
- }
165
-
166
- export function createContainer(): Container {
167
- return createScope();
168
- }