@art-ws/di 2.0.15 → 2.0.16

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.
@@ -1,9 +1,15 @@
1
1
  import type { DIModuleDef, Func, Provider, Token } from "./types.js";
2
+ type ScopeContext = {
3
+ injector: Injector;
4
+ };
2
5
  export type InjectorInstance = InstanceType<typeof Injector>;
6
+ type OnScope = (context: ScopeContext) => void;
3
7
  export declare class Injector {
4
8
  #private;
5
- static root: Injector;
6
- static onScope: (injector: Injector) => void;
9
+ static get root(): Injector;
10
+ static set root(value: Injector);
11
+ static get onScope(): OnScope | undefined;
12
+ static set onScope(value: OnScope | undefined);
7
13
  parent: Injector | null;
8
14
  addFactory<T>(token: Token<T>, factory?: Func<T>): void;
9
15
  addValue<T>(token: Token<T>, value: T): void;
@@ -20,3 +26,4 @@ export declare class Injector {
20
26
  }>): Promise<T>;
21
27
  }
22
28
  export declare function inject<T>(token: Token<T>): T;
29
+ export {};
@@ -1,9 +1,23 @@
1
1
  import { AsyncLocalStorage } from "async_hooks";
2
2
  import { InjectionToken } from "./types.js";
3
3
  const asyncLocalStorage = new AsyncLocalStorage();
4
+ const rootState = {};
4
5
  export class Injector {
5
- static root;
6
- static onScope;
6
+ static get root() {
7
+ if (!rootState.injector) {
8
+ throw new Error(`Root injector is not initialized`);
9
+ }
10
+ return rootState.injector;
11
+ }
12
+ static set root(value) {
13
+ rootState.injector = value;
14
+ }
15
+ static get onScope() {
16
+ return rootState.onScope;
17
+ }
18
+ static set onScope(value) {
19
+ rootState.onScope = value;
20
+ }
7
21
  parent = null;
8
22
  #instances = new Map();
9
23
  #factories = new Map();
@@ -102,7 +116,7 @@ export class Injector {
102
116
  const injector = new Injector();
103
117
  injector.parent = this;
104
118
  injector.addProviders(...(options?.providers || []));
105
- Injector.onScope?.(injector);
119
+ Injector.onScope?.({ injector });
106
120
  return asyncLocalStorage.run({ injector }, async () => {
107
121
  try {
108
122
  return await fn();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/di",
3
- "version": "2.0.15",
3
+ "version": "2.0.16",
4
4
  "description": "Dependency injection for TypeScript",
5
5
  "license": "UNLICENSED",
6
6
  "author": "art-ws Team",