-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.ts
156 lines (115 loc) · 4.29 KB
/
box.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
export type External<T> = {type: 'External'};
export type Value<T> = {type: 'Value'; value: T};
export type Factory<T> = {type: 'Factory'; factory: () => T};
export type Class<T> = {type: 'Class'; constructor: new () => T};
export type Provider<T> = Value<T> | Factory<T> | Class<T>;
export type Declaration<T> = External<T> | Provider<T>;
export type DeclarationValue<T extends Declaration<unknown>> = T extends Declaration<infer V> ? V : never;
export type Declarations = Record<string | number | symbol, Declaration<unknown>>;
// https://www.totaltypescript.com/concepts/the-prettify-helper
type Pretty<T> = {[K in keyof T]: T[K]} & {}; // eslint-disable-line @typescript-eslint/ban-types
type Instance<T extends Declarations> = Pretty<{
[K in keyof T]: DeclarationValue<T[K]>;
}>;
type PickByType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K];
};
type OmitByType<T, U> = {
[K in keyof T as T[K] extends U ? never : K]: T[K];
};
type ReplaceExternal<T extends Declarations> = {
[K in keyof T]: Provider<DeclarationValue<T[K]>>;
};
type Init<T extends Declarations> = Pretty<
& ReplaceExternal<PickByType<T, External<unknown>>>
& Partial<ReplaceExternal<OmitByType<T, External<unknown>>>>
>;
type ExtendDeclarations<T extends Declarations> = {
[K in keyof T]?: Declaration<DeclarationValue<T[K]>>
};
type Extend<T, U> = Pretty<Omit<T, keyof U> & U>;
type InjectionContext = {
readonly instance: Record<string | number | symbol, unknown>;
readonly visited: Record<string | number | symbol, true>;
readonly providers: Record<string | number | symbol, Provider<unknown>>;
};
export class Box<T extends Declarations> {
static external<V>(): External<V> {
return {type: 'External'};
}
static value<V>(value: V): Value<V> {
return {type: 'Value', value};
}
static factory<V>(factory: () => V): Factory<V> {
return {type: 'Factory', factory};
}
static class<V>(constructor: new () => V): Class<V> {
return {type: 'Class', constructor};
}
private context?: InjectionContext;
private injector: Box<T> | undefined = undefined;
constructor(private readonly declarations: T, private readonly parent?: Box<any>) {}
extend<U extends ExtendDeclarations<T>>(declarations: U): Box<Extend<T, U>> {
return new Box({...this.declarations, ...declarations} as Extend<T, U>, this);
}
init(providers: Init<T>): Instance<T> {
this.parent?.setInjector(this);
this.context = {
instance: {},
visited: {},
providers,
};
for (const key of Object.keys(this.declarations)) {
this.inject(key);
}
const {instance} = this.context;
delete this.context;
this.parent?.setInjector(undefined);
return instance as Instance<T>;
}
inject<K extends keyof T>(key: K): Instance<T>[K] {
if (this.injector) {
return this.injector.inject(key);
}
if (!this.context) {
throw new Error('Called inject() outside injection context');
}
if (key in this.context.instance) {
return this.context.instance[key] as Instance<T>[K];
}
if (this.context.visited[key]) {
throw new Error(`Circular dependency detected in "${key.toString()}" (see stack trace)`);
}
this.context.visited[key] = true;
const provider = this.context.providers[key] ?? this.declarations[key];
if (provider) {
if (provider.type === 'Class') {
this.context.instance[key] = new provider.constructor();
return this.context.instance[key] as Instance<T>[K];
}
if (provider.type === 'Factory') {
this.context.instance[key] = provider.factory();
return this.context.instance[key] as Instance<T>[K];
}
if (provider.type === 'Value') {
this.context.instance[key] = provider.value;
return this.context.instance[key] as Instance<T>[K];
}
}
// This error is only possible if you bypass type checking
throw new Error('No provider for ' + key.toString());
}
injectAll(): Instance<T> {
if (this.injector) {
return this.injector.injectAll();
}
if (!this.context) {
throw new Error('Called injectAll() outside injection context');
}
return this.context.instance as Instance<T>;
}
private setInjector(injector: Box<T> | undefined): void {
this.injector = injector;
this.parent?.setInjector(injector);
}
}