@art-ws/di 2.0.17 → 2.0.19
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/es/injector.d.ts +6 -0
- package/dist/es/injector.js +28 -8
- package/package.json +1 -1
package/dist/es/injector.d.ts
CHANGED
@@ -23,5 +23,11 @@ export declare class Injector {
|
|
23
23
|
injector: Injector;
|
24
24
|
}) => void;
|
25
25
|
}>): Promise<T>;
|
26
|
+
dispose(): void;
|
27
|
+
getInstances(): unknown[];
|
26
28
|
}
|
27
29
|
export declare function inject<T>(token: Token<T>): T;
|
30
|
+
export declare function getRootInjector(): Injector;
|
31
|
+
export declare function setRootInjector(injector: Injector | undefined): void;
|
32
|
+
export declare function getOnScopeHandler(): OnScopeHandler | undefined;
|
33
|
+
export declare function setOnScopeHandler(handler: OnScopeHandler | undefined): void;
|
package/dist/es/injector.js
CHANGED
@@ -4,19 +4,16 @@ const asyncLocalStorage = new AsyncLocalStorage();
|
|
4
4
|
const rootState = {};
|
5
5
|
export class Injector {
|
6
6
|
static get root() {
|
7
|
-
|
8
|
-
throw new Error(`Root injector is not initialized`);
|
9
|
-
}
|
10
|
-
return rootState.injector;
|
7
|
+
return getRootInjector();
|
11
8
|
}
|
12
9
|
static set root(value) {
|
13
|
-
|
10
|
+
setRootInjector(value);
|
14
11
|
}
|
15
12
|
static get onScope() {
|
16
|
-
return
|
13
|
+
return getOnScopeHandler();
|
17
14
|
}
|
18
15
|
static set onScope(value) {
|
19
|
-
|
16
|
+
setOnScopeHandler(value);
|
20
17
|
}
|
21
18
|
parent = null;
|
22
19
|
#instances = new Map();
|
@@ -115,7 +112,7 @@ export class Injector {
|
|
115
112
|
async runScope(fn, options) {
|
116
113
|
const injector = new Injector();
|
117
114
|
injector.parent = this;
|
118
|
-
injector.addProviders(...(options?.providers || []));
|
115
|
+
injector.addProviders({ token: Injector, factory: () => injector }, ...(options?.providers || []));
|
119
116
|
Injector.onScope?.({ injector });
|
120
117
|
return asyncLocalStorage.run({ injector }, async () => {
|
121
118
|
try {
|
@@ -126,6 +123,14 @@ export class Injector {
|
|
126
123
|
}
|
127
124
|
});
|
128
125
|
}
|
126
|
+
dispose() {
|
127
|
+
this.parent = null;
|
128
|
+
this.#instances.clear();
|
129
|
+
this.#factories.clear();
|
130
|
+
}
|
131
|
+
getInstances() {
|
132
|
+
return Array.from(this.#instances.values());
|
133
|
+
}
|
129
134
|
}
|
130
135
|
export function inject(token) {
|
131
136
|
const asyncContext = asyncLocalStorage.getStore();
|
@@ -134,6 +139,21 @@ export function inject(token) {
|
|
134
139
|
throw new Error(`No injector found`);
|
135
140
|
return injector.get(token);
|
136
141
|
}
|
142
|
+
export function getRootInjector() {
|
143
|
+
if (!rootState.injector) {
|
144
|
+
throw new Error(`Root injector is not initialized`);
|
145
|
+
}
|
146
|
+
return rootState.injector;
|
147
|
+
}
|
148
|
+
export function setRootInjector(injector) {
|
149
|
+
rootState.injector = injector;
|
150
|
+
}
|
151
|
+
export function getOnScopeHandler() {
|
152
|
+
return rootState.onScope;
|
153
|
+
}
|
154
|
+
export function setOnScopeHandler(handler) {
|
155
|
+
rootState.onScope = handler;
|
156
|
+
}
|
137
157
|
function isClass(value) {
|
138
158
|
return typeof value === "function" && !!value.prototype;
|
139
159
|
}
|