@enshou/di 0.1.3 → 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 +21 -4
- package/dist/index.js +32 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -9,15 +9,32 @@ declare function createToken<T>(description: string): Token<T>;
|
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/container.d.ts
|
|
11
11
|
type Scope = "singleton" | "transient";
|
|
12
|
+
type ProviderToken<T> = Token<T> | string | Class<T>;
|
|
13
|
+
interface ClassProvider<T> {
|
|
14
|
+
provide: ProviderToken<T>;
|
|
15
|
+
useClass: Class<T>;
|
|
16
|
+
scope?: Scope;
|
|
17
|
+
}
|
|
18
|
+
interface ValueProvider<T> {
|
|
19
|
+
provide: ProviderToken<T>;
|
|
20
|
+
useValue: T;
|
|
21
|
+
}
|
|
22
|
+
interface FactoryProvider<T> {
|
|
23
|
+
provide: ProviderToken<T>;
|
|
24
|
+
useFactory: (container: Container) => T;
|
|
25
|
+
scope?: Scope;
|
|
26
|
+
}
|
|
27
|
+
type Provider<T> = ClassProvider<T> | ValueProvider<T> | FactoryProvider<T>;
|
|
12
28
|
declare class Container {
|
|
13
29
|
private readonly providers;
|
|
14
30
|
private readonly singletonCache;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
31
|
+
register<T>(provider: Provider<T>): void;
|
|
32
|
+
registerValue(token: ProviderToken<unknown>, value: unknown): void;
|
|
33
|
+
registerClass(token: ProviderToken<unknown>, value: Class<any>, scope?: Scope): void;
|
|
34
|
+
resolve<T>(token: ProviderToken<T>): T;
|
|
18
35
|
}
|
|
19
36
|
//#endregion
|
|
20
37
|
//#region src/inject.d.ts
|
|
21
38
|
declare function Inject(tokens: Array<Token<any> | string | Class<any>>): (target: any, _context?: ClassDecoratorContext) => void;
|
|
22
39
|
//#endregion
|
|
23
|
-
export { Container, Inject, type Scope, type Token, createToken };
|
|
40
|
+
export { type ClassProvider, Container, type FactoryProvider, Inject, type Provider, type ProviderToken, type Scope, type Token, type ValueProvider, createToken };
|
package/dist/index.js
CHANGED
|
@@ -10,12 +10,35 @@ function Inject(tokens) {
|
|
|
10
10
|
var Container = class {
|
|
11
11
|
providers = /* @__PURE__ */ new Map();
|
|
12
12
|
singletonCache = /* @__PURE__ */ new Map();
|
|
13
|
+
register(provider) {
|
|
14
|
+
this.singletonCache.delete(provider.provide);
|
|
15
|
+
if ("useValue" in provider) {
|
|
16
|
+
this.singletonCache.set(provider.provide, provider.useValue);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if ("useFactory" in provider) {
|
|
20
|
+
this.providers.set(provider.provide, {
|
|
21
|
+
kind: "factory",
|
|
22
|
+
useFactory: provider.useFactory,
|
|
23
|
+
scope: provider.scope ?? "singleton"
|
|
24
|
+
});
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.providers.set(provider.provide, {
|
|
28
|
+
kind: "class",
|
|
29
|
+
useClass: provider.useClass,
|
|
30
|
+
scope: provider.scope ?? "singleton"
|
|
31
|
+
});
|
|
32
|
+
}
|
|
13
33
|
registerValue(token, value) {
|
|
14
|
-
this.
|
|
34
|
+
this.register({
|
|
35
|
+
provide: token,
|
|
36
|
+
useValue: value
|
|
37
|
+
});
|
|
15
38
|
}
|
|
16
39
|
registerClass(token, value, scope = "singleton") {
|
|
17
|
-
this.
|
|
18
|
-
|
|
40
|
+
this.register({
|
|
41
|
+
provide: token,
|
|
19
42
|
useClass: value,
|
|
20
43
|
scope
|
|
21
44
|
});
|
|
@@ -24,8 +47,12 @@ var Container = class {
|
|
|
24
47
|
if (this.singletonCache.has(token)) return this.singletonCache.get(token);
|
|
25
48
|
const provider = this.providers.get(token);
|
|
26
49
|
if (!provider) throw Error(`No provider for ${String(token)}`);
|
|
27
|
-
|
|
28
|
-
|
|
50
|
+
let value;
|
|
51
|
+
if (provider.kind === "factory") value = provider.useFactory(this);
|
|
52
|
+
else {
|
|
53
|
+
const deps = (provider.useClass[INJECTS_KEY] ?? []).map(this.resolve.bind(this));
|
|
54
|
+
value = new provider.useClass(...deps);
|
|
55
|
+
}
|
|
29
56
|
if (provider.scope === "singleton") this.singletonCache.set(token, value);
|
|
30
57
|
return value;
|
|
31
58
|
}
|