/
space.ts
225 lines (193 loc) · 6.47 KB
/
space.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
217
218
219
220
221
222
223
224
225
import ServiceMaker, { ENUM_PHASE_BOOTSTRAP } from './maker';
import ProvisionMap from './provisionMap';
import ServiceScope from './scope';
import { isServiceProvider, isInterfaceable } from './utils';
import type {
ServiceBinding,
ServiceProvision,
ServiceCache,
ServiceProvider,
Interfaceable,
PolymorphicProviderBinding,
PolymorphicValueBinding,
} from './types';
const hasOwnProperty = (obj, prop) =>
Object.prototype.hasOwnProperty.call(obj, prop);
const printInterface = (obj) => obj?.$$name || obj?.name || String(obj);
const isPolymorphic = <T>(
target: ServiceBinding<T>
): target is PolymorphicProviderBinding<T> | PolymorphicValueBinding<T> =>
!!target.provide.$$polymorphic;
const resolveBindings = (
provisions: ServiceProvision<unknown>[]
): ProvisionMap<ServiceBinding<unknown>> => {
const provisionMapping = new ProvisionMap<ServiceBinding<unknown>>();
for (const provision of provisions) {
let binding: ServiceBinding<unknown>;
if (isServiceProvider(provision)) {
binding = {
provide: provision,
withProvider: provision,
};
} else {
if (!isInterfaceable(provision.provide)) {
throw new TypeError(
isInterfaceable(provision)
? `${printInterface(
provision
)} is an interface and cannot be provided directly`
: `invalid service interface ${printInterface(provision.provide)}`
);
}
if (
!(
hasOwnProperty(provision, 'withValue') ||
('withProvider' in provision &&
isServiceProvider(provision.withProvider))
)
) {
throw new TypeError(
'withProvider' in provision
? `invalid provider ${printInterface(provision.withProvider)}`
: `either withProvider or withValue must be provided within binding`
);
}
binding = provision;
}
if (isPolymorphic(binding)) {
const { provide: target, platform } = binding;
const replaced = provisionMapping.setPolymorphic(
target,
binding,
platform
);
if (replaced) {
throw new Error(
`${target.$$name} is already bound to ${printInterface(
'withProvider' in replaced
? replaced.withProvider
: replaced.withValue
)} on '${platform}' platform`
);
}
} else if (binding.provide.$$multi) {
provisionMapping.setMulti(binding.provide, binding);
} else {
const { provide: target } = binding;
const replaced = provisionMapping.setSingular(target, binding);
if (replaced) {
throw new Error(
`${target.$$name} is already bound to ${printInterface(
'withProvider' in replaced
? replaced.withProvider
: replaced.withValue
)}`
);
}
}
}
return provisionMapping;
};
export default class ServiceSpace {
maker: ServiceMaker;
provisionsMapping: ProvisionMap<ServiceBinding<unknown>>;
_singletonCache: null | ServiceCache;
constructor(
base: null | ServiceSpace,
provisions: ServiceProvision<unknown>[]
) {
const baseMapping = base
? new ProvisionMap(base.provisionsMapping)
: new ProvisionMap<ServiceBinding<unknown>>();
const provisionMapping = resolveBindings(provisions);
this._singletonCache = null;
this.provisionsMapping = baseMapping.merge(provisionMapping);
this.maker = new ServiceMaker(this.provisionsMapping);
}
bootstrap(
bootstrapTimeProvisions: Map<Interfaceable<unknown>, unknown> = new Map()
): ServiceScope {
const singletonCache = new Map();
const scopedCache = new Map();
const bootstrapScope = new ServiceScope(
this.maker,
singletonCache,
scopedCache
);
const provisionMap = new Map(bootstrapTimeProvisions);
provisionMap.set(ServiceScope, bootstrapScope);
for (const [, binding] of this.provisionsMapping) {
if ('withProvider' in binding) {
const { withProvider: provider } = binding;
this._verifyDependencies(provider, provisionMap, []);
if (provider.$$lifetime === 'singleton') {
this.maker.makeProvider(
provider,
ENUM_PHASE_BOOTSTRAP,
singletonCache,
scopedCache,
provisionMap
);
}
}
}
this._singletonCache = singletonCache;
return bootstrapScope;
}
createScope(): ServiceScope {
const singletonCache = this._singletonCache;
if (!singletonCache) {
throw new Error('service space has not bootstraped');
}
const scopeInjector = new ServiceScope(this.maker, singletonCache);
return scopeInjector;
}
private _verifyDependencies(
provider: ServiceProvider<unknown, unknown[]>,
bootstrapProvisions: null | Map<Interfaceable<unknown>, unknown>,
refLock: ServiceProvider<unknown, unknown[]>[]
) {
const subRefLock = [...refLock, provider];
for (const { require: target, optional } of provider.$$deps) {
let bindings: null | ServiceBinding<unknown>[] = null;
if (target.$$polymorphic) {
const branches = this.provisionsMapping.getPolymorphic(target);
bindings = [];
for (const [, binding] of branches) {
bindings.push(binding);
}
} else if (target.$$multi) {
bindings = this.provisionsMapping.getMulti(target);
} else if (target !== ServiceScope) {
const binding = this.provisionsMapping.getSingular(target);
const isProvidedOnBootstrap =
provider.$$lifetime === 'singleton' &&
!!bootstrapProvisions?.has(target);
if (!binding && !optional && !isProvidedOnBootstrap) {
throw new TypeError(`${target.$$name} is not bound`);
}
bindings = binding ? [binding] : null;
}
if (bindings) {
for (const binding of bindings) {
if ('withProvider' in binding) {
const { withProvider: argProvider } = binding;
if (subRefLock.indexOf(argProvider) !== -1) {
const argProviderName = argProvider.$$name;
throw new Error(
`${argProviderName} is circular dependent (${subRefLock
.map(({ $$name: name }) => name)
.join(' > ')} > ${argProviderName})`
);
}
this._verifyDependencies(
argProvider,
bootstrapProvisions,
subRefLock
);
}
}
}
}
}
}