-
Notifications
You must be signed in to change notification settings - Fork 901
/
provider.ts
378 lines (338 loc) · 12.3 KB
/
provider.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* @license
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Deferred } from '@firebase/util';
import { ComponentContainer } from './component_container';
import { DEFAULT_ENTRY_NAME } from './constants';
import {
InitializeOptions,
InstantiationMode,
Name,
NameServiceMapping,
OnInitCallBack
} from './types';
import { Component } from './component';
/**
* Provider for instance for service name T, e.g. 'auth', 'auth-internal'
* NameServiceMapping[T] is an alias for the type of the instance
*/
export class Provider<T extends Name> {
private component: Component<T> | null = null;
private readonly instances: Map<string, NameServiceMapping[T]> = new Map();
private readonly instancesDeferred: Map<
string,
Deferred<NameServiceMapping[T]>
> = new Map();
private readonly instancesOptions: Map<string, Record<string, unknown>> =
new Map();
private onInitCallbacks: Map<string, Set<OnInitCallBack<T>>> = new Map();
constructor(
private readonly name: T,
private readonly container: ComponentContainer
) {}
/**
* @param identifier A provider can provide multiple instances of a service
* if this.component.multipleInstances is true.
*/
get(identifier?: string): Promise<NameServiceMapping[T]> {
// if multipleInstances is not supported, use the default name
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
if (!this.instancesDeferred.has(normalizedIdentifier)) {
const deferred = new Deferred<NameServiceMapping[T]>();
this.instancesDeferred.set(normalizedIdentifier, deferred);
if (
this.isInitialized(normalizedIdentifier) ||
this.shouldAutoInitialize()
) {
// initialize the service if it can be auto-initialized
try {
const instance = this.getOrInitializeService({
instanceIdentifier: normalizedIdentifier
});
if (instance) {
deferred.resolve(instance);
}
} catch (e) {
// when the instance factory throws an exception during get(), it should not cause
// a fatal error. We just return the unresolved promise in this case.
}
}
}
return this.instancesDeferred.get(normalizedIdentifier)!.promise;
}
/**
*
* @param options.identifier A provider can provide multiple instances of a service
* if this.component.multipleInstances is true.
* @param options.optional If optional is false or not provided, the method throws an error when
* the service is not immediately available.
* If optional is true, the method returns null if the service is not immediately available.
*/
getImmediate(options: {
identifier?: string;
optional: true;
}): NameServiceMapping[T] | null;
getImmediate(options?: {
identifier?: string;
optional?: false;
}): NameServiceMapping[T];
getImmediate(options?: {
identifier?: string;
optional?: boolean;
}): NameServiceMapping[T] | null {
// if multipleInstances is not supported, use the default name
const normalizedIdentifier = this.normalizeInstanceIdentifier(
options?.identifier
);
const optional = options?.optional ?? false;
if (
this.isInitialized(normalizedIdentifier) ||
this.shouldAutoInitialize()
) {
try {
return this.getOrInitializeService({
instanceIdentifier: normalizedIdentifier
});
} catch (e) {
if (optional) {
return null;
} else {
throw e;
}
}
} else {
// In case a component is not initialized and should/cannot be auto-initialized at the moment, return null if the optional flag is set, or throw
if (optional) {
return null;
} else {
throw Error(`Service ${this.name} is not available`);
}
}
}
getComponent(): Component<T> | null {
return this.component;
}
setComponent(component: Component<T>): void {
if (component.name !== this.name) {
throw Error(
`Mismatching Component ${component.name} for Provider ${this.name}.`
);
}
if (this.component) {
throw Error(`Component for ${this.name} has already been provided`);
}
this.component = component;
// return early without attempting to initialize the component if the component requires explicit initialization (calling `Provider.initialize()`)
if (!this.shouldAutoInitialize()) {
return;
}
// if the service is eager, initialize the default instance
if (isComponentEager(component)) {
try {
this.getOrInitializeService({ instanceIdentifier: DEFAULT_ENTRY_NAME });
} catch (e) {
// when the instance factory for an eager Component throws an exception during the eager
// initialization, it should not cause a fatal error.
// TODO: Investigate if we need to make it configurable, because some component may want to cause
// a fatal error in this case?
}
}
// Create service instances for the pending promises and resolve them
// NOTE: if this.multipleInstances is false, only the default instance will be created
// and all promises with resolve with it regardless of the identifier.
for (const [
instanceIdentifier,
instanceDeferred
] of this.instancesDeferred.entries()) {
const normalizedIdentifier =
this.normalizeInstanceIdentifier(instanceIdentifier);
try {
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
const instance = this.getOrInitializeService({
instanceIdentifier: normalizedIdentifier
})!;
instanceDeferred.resolve(instance);
} catch (e) {
// when the instance factory throws an exception, it should not cause
// a fatal error. We just leave the promise unresolved.
}
}
}
clearInstance(identifier: string = DEFAULT_ENTRY_NAME): void {
this.instancesDeferred.delete(identifier);
this.instancesOptions.delete(identifier);
this.instances.delete(identifier);
}
// app.delete() will call this method on every provider to delete the services
// TODO: should we mark the provider as deleted?
async delete(): Promise<void> {
const services = Array.from(this.instances.values());
await Promise.all([
...services
.filter(service => 'INTERNAL' in service) // legacy services
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.map(service => (service as any).INTERNAL!.delete()),
...services
.filter(service => '_delete' in service) // modularized services
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.map(service => (service as any)._delete())
]);
}
isComponentSet(): boolean {
return this.component != null;
}
isInitialized(identifier: string = DEFAULT_ENTRY_NAME): boolean {
return this.instances.has(identifier);
}
getOptions(identifier: string = DEFAULT_ENTRY_NAME): Record<string, unknown> {
return this.instancesOptions.get(identifier) || {};
}
initialize(opts: InitializeOptions = {}): NameServiceMapping[T] {
const { options = {} } = opts;
const normalizedIdentifier = this.normalizeInstanceIdentifier(
opts.instanceIdentifier
);
if (this.isInitialized(normalizedIdentifier)) {
throw Error(
`${this.name}(${normalizedIdentifier}) has already been initialized`
);
}
if (!this.isComponentSet()) {
throw Error(`Component ${this.name} has not been registered yet`);
}
const instance = this.getOrInitializeService({
instanceIdentifier: normalizedIdentifier,
options
})!;
// resolve any pending promise waiting for the service instance
for (const [
instanceIdentifier,
instanceDeferred
] of this.instancesDeferred.entries()) {
const normalizedDeferredIdentifier =
this.normalizeInstanceIdentifier(instanceIdentifier);
if (normalizedIdentifier === normalizedDeferredIdentifier) {
instanceDeferred.resolve(instance);
}
}
return instance;
}
/**
*
* @param callback - a function that will be invoked after the provider has been initialized by calling provider.initialize().
* The function is invoked SYNCHRONOUSLY, so it should not execute any longrunning tasks in order to not block the program.
*
* @param identifier An optional instance identifier
* @returns a function to unregister the callback
*/
onInit(callback: OnInitCallBack<T>, identifier?: string): () => void {
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
const existingCallbacks =
this.onInitCallbacks.get(normalizedIdentifier) ??
new Set<OnInitCallBack<T>>();
existingCallbacks.add(callback);
this.onInitCallbacks.set(normalizedIdentifier, existingCallbacks);
const existingInstance = this.instances.get(normalizedIdentifier);
if (existingInstance) {
callback(existingInstance, normalizedIdentifier);
}
return () => {
existingCallbacks.delete(callback);
};
}
/**
* Invoke onInit callbacks synchronously
* @param instance the service instance`
*/
private invokeOnInitCallbacks(
instance: NameServiceMapping[T],
identifier: string
): void {
const callbacks = this.onInitCallbacks.get(identifier);
if (!callbacks) {
return;
}
for (const callback of callbacks) {
try {
callback(instance, identifier);
} catch {
// ignore errors in the onInit callback
}
}
}
private getOrInitializeService({
instanceIdentifier,
options = {}
}: {
instanceIdentifier: string;
options?: Record<string, unknown>;
}): NameServiceMapping[T] | null {
let instance = this.instances.get(instanceIdentifier);
if (!instance && this.component) {
instance = this.component.instanceFactory(this.container, {
instanceIdentifier: normalizeIdentifierForFactory(instanceIdentifier),
options
});
this.instances.set(instanceIdentifier, instance);
this.instancesOptions.set(instanceIdentifier, options);
/**
* Invoke onInit listeners.
* Note this.component.onInstanceCreated is different, which is used by the component creator,
* while onInit listeners are registered by consumers of the provider.
*/
this.invokeOnInitCallbacks(instance, instanceIdentifier);
/**
* Order is important
* onInstanceCreated() should be called after this.instances.set(instanceIdentifier, instance); which
* makes `isInitialized()` return true.
*/
if (this.component.onInstanceCreated) {
try {
this.component.onInstanceCreated(
this.container,
instanceIdentifier,
instance
);
} catch {
// ignore errors in the onInstanceCreatedCallback
}
}
}
return instance || null;
}
private normalizeInstanceIdentifier(
identifier: string = DEFAULT_ENTRY_NAME
): string {
if (this.component) {
return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME;
} else {
return identifier; // assume multiple instances are supported before the component is provided.
}
}
private shouldAutoInitialize(): boolean {
return (
!!this.component &&
this.component.instantiationMode !== InstantiationMode.EXPLICIT
);
}
}
// undefined should be passed to the service factory for the default instance
function normalizeIdentifierForFactory(identifier: string): string | undefined {
return identifier === DEFAULT_ENTRY_NAME ? undefined : identifier;
}
function isComponentEager<T extends Name>(component: Component<T>): boolean {
return component.instantiationMode === InstantiationMode.EAGER;
}