-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.ts
216 lines (193 loc) · 6.78 KB
/
Module.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { DependencyKey, Target } from './DependencyKey'
import { Container } from './Container'
import { Merge } from './ProvideGraph'
import { unresolved, CanRequest } from './CanRequest'
import DefaultGraph = Container.DefaultGraph
import ProvideGraph = Container.Graph
import FlatGraph = ProvideGraph.Flat
import EmptyGraph = ProvideGraph.Empty
function requestForModule<P extends ProvideGraph, K extends DependencyKey>(
mod: Module.ApplyTo<P>,
deps: K,
): ModuleActual<K, P> {
return Container.create().apply(mod).requestUnchecked(deps)
}
function requestAsyncForModule<P extends ProvideGraph, K extends DependencyKey>(
mod: Module.ApplyTo<P>,
deps: K,
): Promise<ModuleActual<K, P>> {
return Container.create().apply(mod).requestAsyncUnchecked(deps)
}
type ModuleActual<K extends DependencyKey, P extends ProvideGraph> = Target<K, Merge<DefaultGraph, P>>
/**
* An object used to provide definitions to a {@link Container}
*/
export abstract class BaseModule<P extends ProvideGraph = any> implements Module.ApplyTo<P> {
/** @ignore */
readonly [unresolved]!: void
abstract applyTo(ct: Container.Builder<any>): Container<P>
/**
* {@inheritDoc Container.requestUnchecked}
* @see {@link Container.requestUnchecked}
*/
requestUnchecked<K extends DependencyKey>(key: K): ModuleActual<K, P> {
return requestForModule(this, key)
}
/**
* {@inheritDoc Container.requestAsyncUnchecked}
* @see {@link Container.requestAsyncUnchecked}
*/
requestAsyncUnchecked<K extends DependencyKey>(key: K): Promise<ModuleActual<K, P>> {
return requestAsyncForModule(this, key)
}
/**
* {@inheritDoc Container.request}
* @see {@link Container.request}
*/
request<K extends DependencyKey, Th extends CanRequest<Merge<DefaultGraph, P>, K>>(
this: this & Th,
deps: K,
): ModuleActual<K, P> {
return requestForModule(this, deps)
}
/**
* {@inheritDoc Container.requestAsync}
* @see {@link Container.requestAsync}
*/
requestAsync<K extends DependencyKey, Th extends CanRequest<Merge<DefaultGraph, P>, K, never>>(
this: this & Th,
deps: K,
): Promise<ModuleActual<K, P>> {
return requestAsyncForModule(this, deps)
}
/**
* {@inheritDoc Container.inject}
* @see {@link Container.inject}
*/
inject<K extends DependencyKey, R, Th extends CanRequest<Merge<DefaultGraph, P>, K>>(
this: this & Th,
deps: K,
f: (deps: ModuleActual<K, P>) => R
): R {
return f(requestForModule(this, deps))
}
/**
* {@inheritDoc Container.injectAsync}
* @see {@link Container.injectAsync}
*/
injectAsync<K extends DependencyKey, R, Th extends CanRequest<Merge<DefaultGraph, P>, K, never>>(
this: this & Th,
deps: K,
f: (deps: ModuleActual<K, P>) => R | Promise<R>
): Promise<R> {
return requestAsyncForModule(this, deps).then(f)
}
/**
* {@inheritDoc Container.build}
* @see {@link Container.build}
*/
build<
K extends DependencyKey.Of<(...args: Args) => any>,
Th extends CanRequest<Merge<DefaultGraph, P>, K>,
Args extends ModuleActual<K, P> extends (...args: infer A) => Out ? A : never,
Out = ModuleActual<K, P> extends (...args: Args) => infer O ? O : unknown,
>(this: this & Th, fac: K, ...args: Args): Out {
return requestForModule(this, fac)(...args)
}
/**
* {@inheritDoc Container.buildAsync}
* @see {@link Container.buildAsync}
*/
buildAsync<
K extends DependencyKey.Of<(...args: Args) => any>,
Th extends CanRequest<Merge<DefaultGraph, P>, K, never>,
Args extends ModuleActual<K, P> extends (...args: infer A) => Out ? A : never,
Out = ModuleActual<K, P> extends (...args: Args) => infer O ? O : unknown,
>(this: this & Th, fac: K, ...args: Args): Promise<Out> {
return requestAsyncForModule(this, fac).then(f => f(...args))
}
/** Returns a {@link Container} with `this` applied to it. */
container(this: Module.ApplyTo<P>) {
return Container.create().apply(this)
}
}
class ListModule<P extends ProvideGraph> extends BaseModule<P> {
private readonly _items: Module.Item[]
constructor(items: Module.Item[]) {
super()
this._items = items
}
override applyTo(ct: Container.Builder<any>) {
for (const item of this._items) {
if (item instanceof Array) {
for (const x of item) { ct.apply(x) }
} else if (typeof item == 'function') {
item(ct)
} else {
item.applyTo(ct)
}
}
return ct as Container<P>
}
}
/**
* A pre-defined set of resource providers and scopes that can be applied to any {@link Container}.
* @see The {@link Module} function
*
* @group Injection
* @category Module
*/
export interface Module<P extends ProvideGraph = any> extends BaseModule<P> { }
/**
* Defines a {@link Module:type | Module} given a list of:
* * Modules, which will be applied in the given order when applying the return module
* * Functions that take a {@link Container.Builder} and provide resources and scopes
* * Arbitrarily-nested lists of the above
*
* @example Providing resources and scopes:
*
* ```ts
* const UserModule = Module(ct => ct
* .addScope(UserScope)
* .provideInstance(NameKey, 'Alice')
* .provideInstance(IdKey, 123)
* .provide(User, Inject.construct(User, NameKey, IdKey))
* )
* ```
*
* @example Combining Modules:
*
* ```ts
* const AppModule = Module(UserModule, DataModule, FooModule)
* ```
*
* @group Injection
* @category Module
*/
export function Module<M extends Module.Item[]>(...m: M): Module<Module.Provides<M>> {
return new ListModule(m)
}
/**
* @group Injection
* @category Module
*/
export namespace Module {
/** Implementation of a module that performs operations on a given {@link Container}. */
export interface FunctionItem<P extends FlatGraph = any> {
(ct: Container.Builder): Container.Builder<P>
}
/** An argument for the {@link Module} function */
export type Item = FunctionItem | ApplyTo | readonly Item[]
/** {@link Container.Graph} produced by a module of type {@link M} */
export type Provides<M> =
M extends ApplyTo<infer P> ? P :
M extends FunctionItem<infer P> ? P :
M extends readonly [infer A] ? Provides<A> :
M extends readonly [infer A, ...infer B] ? Merge<Provides<A>, Provides<B>> :
M extends [] ? EmptyGraph :
never
/** Supertype of {@link Module:type | Module} that can apply itself to a {@link Container}. */
export interface ApplyTo<P extends ProvideGraph = any> {
applyTo(ct: Container.Builder<any>): Container<P>
}
}