@firebase/component 0.6.10 → 0.6.11
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/esm/index.d.ts +20 -20
- package/dist/esm/index.esm2017.js +397 -397
- package/dist/esm/index.esm2017.js.map +1 -1
- package/dist/esm/src/component.d.ts +43 -43
- package/dist/esm/src/component_container.d.ts +47 -47
- package/dist/esm/src/component_container.test.d.ts +24 -24
- package/dist/esm/src/constants.d.ts +17 -17
- package/dist/esm/src/provider.d.ts +79 -79
- package/dist/esm/src/provider.test.d.ts +23 -23
- package/dist/esm/src/types.d.ts +62 -62
- package/dist/esm/test/setup.d.ts +17 -17
- package/dist/esm/test/util.d.ts +5 -5
- package/dist/index.cjs.js +397 -397
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +20 -20
- package/dist/src/component.d.ts +43 -43
- package/dist/src/component_container.d.ts +47 -47
- package/dist/src/component_container.test.d.ts +24 -24
- package/dist/src/constants.d.ts +17 -17
- package/dist/src/provider.d.ts +79 -79
- package/dist/src/provider.test.d.ts +23 -23
- package/dist/src/types.d.ts +62 -62
- package/dist/test/setup.d.ts +17 -17
- package/dist/test/util.d.ts +5 -5
- package/package.json +3 -3
package/dist/index.cjs.js
CHANGED
|
@@ -4,409 +4,409 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var util = require('@firebase/util');
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Component for service name T, e.g. `auth`, `auth-internal`
|
|
9
|
-
*/
|
|
10
|
-
class Component {
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param name The public service name, e.g. app, auth, firestore, database
|
|
14
|
-
* @param instanceFactory Service factory responsible for creating the public interface
|
|
15
|
-
* @param type whether the service provided by the component is public or private
|
|
16
|
-
*/
|
|
17
|
-
constructor(name, instanceFactory, type) {
|
|
18
|
-
this.name = name;
|
|
19
|
-
this.instanceFactory = instanceFactory;
|
|
20
|
-
this.type = type;
|
|
21
|
-
this.multipleInstances = false;
|
|
22
|
-
/**
|
|
23
|
-
* Properties to be added to the service namespace
|
|
24
|
-
*/
|
|
25
|
-
this.serviceProps = {};
|
|
26
|
-
this.instantiationMode = "LAZY" /* InstantiationMode.LAZY */;
|
|
27
|
-
this.onInstanceCreated = null;
|
|
28
|
-
}
|
|
29
|
-
setInstantiationMode(mode) {
|
|
30
|
-
this.instantiationMode = mode;
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
setMultipleInstances(multipleInstances) {
|
|
34
|
-
this.multipleInstances = multipleInstances;
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
setServiceProps(props) {
|
|
38
|
-
this.serviceProps = props;
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
setInstanceCreatedCallback(callback) {
|
|
42
|
-
this.onInstanceCreated = callback;
|
|
43
|
-
return this;
|
|
44
|
-
}
|
|
7
|
+
/**
|
|
8
|
+
* Component for service name T, e.g. `auth`, `auth-internal`
|
|
9
|
+
*/
|
|
10
|
+
class Component {
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param name The public service name, e.g. app, auth, firestore, database
|
|
14
|
+
* @param instanceFactory Service factory responsible for creating the public interface
|
|
15
|
+
* @param type whether the service provided by the component is public or private
|
|
16
|
+
*/
|
|
17
|
+
constructor(name, instanceFactory, type) {
|
|
18
|
+
this.name = name;
|
|
19
|
+
this.instanceFactory = instanceFactory;
|
|
20
|
+
this.type = type;
|
|
21
|
+
this.multipleInstances = false;
|
|
22
|
+
/**
|
|
23
|
+
* Properties to be added to the service namespace
|
|
24
|
+
*/
|
|
25
|
+
this.serviceProps = {};
|
|
26
|
+
this.instantiationMode = "LAZY" /* InstantiationMode.LAZY */;
|
|
27
|
+
this.onInstanceCreated = null;
|
|
28
|
+
}
|
|
29
|
+
setInstantiationMode(mode) {
|
|
30
|
+
this.instantiationMode = mode;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
setMultipleInstances(multipleInstances) {
|
|
34
|
+
this.multipleInstances = multipleInstances;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
setServiceProps(props) {
|
|
38
|
+
this.serviceProps = props;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
setInstanceCreatedCallback(callback) {
|
|
42
|
+
this.onInstanceCreated = callback;
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
/**
|
|
48
|
-
* @license
|
|
49
|
-
* Copyright 2019 Google LLC
|
|
50
|
-
*
|
|
51
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
52
|
-
* you may not use this file except in compliance with the License.
|
|
53
|
-
* You may obtain a copy of the License at
|
|
54
|
-
*
|
|
55
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
56
|
-
*
|
|
57
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
58
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
59
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
60
|
-
* See the License for the specific language governing permissions and
|
|
61
|
-
* limitations under the License.
|
|
62
|
-
*/
|
|
47
|
+
/**
|
|
48
|
+
* @license
|
|
49
|
+
* Copyright 2019 Google LLC
|
|
50
|
+
*
|
|
51
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
52
|
+
* you may not use this file except in compliance with the License.
|
|
53
|
+
* You may obtain a copy of the License at
|
|
54
|
+
*
|
|
55
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
56
|
+
*
|
|
57
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
58
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
59
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
60
|
+
* See the License for the specific language governing permissions and
|
|
61
|
+
* limitations under the License.
|
|
62
|
+
*/
|
|
63
63
|
const DEFAULT_ENTRY_NAME = '[DEFAULT]';
|
|
64
64
|
|
|
65
|
-
/**
|
|
66
|
-
* @license
|
|
67
|
-
* Copyright 2019 Google LLC
|
|
68
|
-
*
|
|
69
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
70
|
-
* you may not use this file except in compliance with the License.
|
|
71
|
-
* You may obtain a copy of the License at
|
|
72
|
-
*
|
|
73
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
74
|
-
*
|
|
75
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
76
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
77
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
78
|
-
* See the License for the specific language governing permissions and
|
|
79
|
-
* limitations under the License.
|
|
80
|
-
*/
|
|
81
|
-
/**
|
|
82
|
-
* Provider for instance for service name T, e.g. 'auth', 'auth-internal'
|
|
83
|
-
* NameServiceMapping[T] is an alias for the type of the instance
|
|
84
|
-
*/
|
|
85
|
-
class Provider {
|
|
86
|
-
constructor(name, container) {
|
|
87
|
-
this.name = name;
|
|
88
|
-
this.container = container;
|
|
89
|
-
this.component = null;
|
|
90
|
-
this.instances = new Map();
|
|
91
|
-
this.instancesDeferred = new Map();
|
|
92
|
-
this.instancesOptions = new Map();
|
|
93
|
-
this.onInitCallbacks = new Map();
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* @param identifier A provider can provide multiple instances of a service
|
|
97
|
-
* if this.component.multipleInstances is true.
|
|
98
|
-
*/
|
|
99
|
-
get(identifier) {
|
|
100
|
-
// if multipleInstances is not supported, use the default name
|
|
101
|
-
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
|
|
102
|
-
if (!this.instancesDeferred.has(normalizedIdentifier)) {
|
|
103
|
-
const deferred = new util.Deferred();
|
|
104
|
-
this.instancesDeferred.set(normalizedIdentifier, deferred);
|
|
105
|
-
if (this.isInitialized(normalizedIdentifier) ||
|
|
106
|
-
this.shouldAutoInitialize()) {
|
|
107
|
-
// initialize the service if it can be auto-initialized
|
|
108
|
-
try {
|
|
109
|
-
const instance = this.getOrInitializeService({
|
|
110
|
-
instanceIdentifier: normalizedIdentifier
|
|
111
|
-
});
|
|
112
|
-
if (instance) {
|
|
113
|
-
deferred.resolve(instance);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
catch (e) {
|
|
117
|
-
// when the instance factory throws an exception during get(), it should not cause
|
|
118
|
-
// a fatal error. We just return the unresolved promise in this case.
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return this.instancesDeferred.get(normalizedIdentifier).promise;
|
|
123
|
-
}
|
|
124
|
-
getImmediate(options) {
|
|
125
|
-
var _a;
|
|
126
|
-
// if multipleInstances is not supported, use the default name
|
|
127
|
-
const normalizedIdentifier = this.normalizeInstanceIdentifier(options === null || options === void 0 ? void 0 : options.identifier);
|
|
128
|
-
const optional = (_a = options === null || options === void 0 ? void 0 : options.optional) !== null && _a !== void 0 ? _a : false;
|
|
129
|
-
if (this.isInitialized(normalizedIdentifier) ||
|
|
130
|
-
this.shouldAutoInitialize()) {
|
|
131
|
-
try {
|
|
132
|
-
return this.getOrInitializeService({
|
|
133
|
-
instanceIdentifier: normalizedIdentifier
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
catch (e) {
|
|
137
|
-
if (optional) {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
throw e;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
// 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
|
|
147
|
-
if (optional) {
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
throw Error(`Service ${this.name} is not available`);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
getComponent() {
|
|
156
|
-
return this.component;
|
|
157
|
-
}
|
|
158
|
-
setComponent(component) {
|
|
159
|
-
if (component.name !== this.name) {
|
|
160
|
-
throw Error(`Mismatching Component ${component.name} for Provider ${this.name}.`);
|
|
161
|
-
}
|
|
162
|
-
if (this.component) {
|
|
163
|
-
throw Error(`Component for ${this.name} has already been provided`);
|
|
164
|
-
}
|
|
165
|
-
this.component = component;
|
|
166
|
-
// return early without attempting to initialize the component if the component requires explicit initialization (calling `Provider.initialize()`)
|
|
167
|
-
if (!this.shouldAutoInitialize()) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
// if the service is eager, initialize the default instance
|
|
171
|
-
if (isComponentEager(component)) {
|
|
172
|
-
try {
|
|
173
|
-
this.getOrInitializeService({ instanceIdentifier: DEFAULT_ENTRY_NAME });
|
|
174
|
-
}
|
|
175
|
-
catch (e) {
|
|
176
|
-
// when the instance factory for an eager Component throws an exception during the eager
|
|
177
|
-
// initialization, it should not cause a fatal error.
|
|
178
|
-
// TODO: Investigate if we need to make it configurable, because some component may want to cause
|
|
179
|
-
// a fatal error in this case?
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
// Create service instances for the pending promises and resolve them
|
|
183
|
-
// NOTE: if this.multipleInstances is false, only the default instance will be created
|
|
184
|
-
// and all promises with resolve with it regardless of the identifier.
|
|
185
|
-
for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
|
|
186
|
-
const normalizedIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
|
|
187
|
-
try {
|
|
188
|
-
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
|
|
189
|
-
const instance = this.getOrInitializeService({
|
|
190
|
-
instanceIdentifier: normalizedIdentifier
|
|
191
|
-
});
|
|
192
|
-
instanceDeferred.resolve(instance);
|
|
193
|
-
}
|
|
194
|
-
catch (e) {
|
|
195
|
-
// when the instance factory throws an exception, it should not cause
|
|
196
|
-
// a fatal error. We just leave the promise unresolved.
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
clearInstance(identifier = DEFAULT_ENTRY_NAME) {
|
|
201
|
-
this.instancesDeferred.delete(identifier);
|
|
202
|
-
this.instancesOptions.delete(identifier);
|
|
203
|
-
this.instances.delete(identifier);
|
|
204
|
-
}
|
|
205
|
-
// app.delete() will call this method on every provider to delete the services
|
|
206
|
-
// TODO: should we mark the provider as deleted?
|
|
207
|
-
async delete() {
|
|
208
|
-
const services = Array.from(this.instances.values());
|
|
209
|
-
await Promise.all([
|
|
210
|
-
...services
|
|
211
|
-
.filter(service => 'INTERNAL' in service) // legacy services
|
|
212
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
|
-
.map(service => service.INTERNAL.delete()),
|
|
214
|
-
...services
|
|
215
|
-
.filter(service => '_delete' in service) // modularized services
|
|
216
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
217
|
-
.map(service => service._delete())
|
|
218
|
-
]);
|
|
219
|
-
}
|
|
220
|
-
isComponentSet() {
|
|
221
|
-
return this.component != null;
|
|
222
|
-
}
|
|
223
|
-
isInitialized(identifier = DEFAULT_ENTRY_NAME) {
|
|
224
|
-
return this.instances.has(identifier);
|
|
225
|
-
}
|
|
226
|
-
getOptions(identifier = DEFAULT_ENTRY_NAME) {
|
|
227
|
-
return this.instancesOptions.get(identifier) || {};
|
|
228
|
-
}
|
|
229
|
-
initialize(opts = {}) {
|
|
230
|
-
const { options = {} } = opts;
|
|
231
|
-
const normalizedIdentifier = this.normalizeInstanceIdentifier(opts.instanceIdentifier);
|
|
232
|
-
if (this.isInitialized(normalizedIdentifier)) {
|
|
233
|
-
throw Error(`${this.name}(${normalizedIdentifier}) has already been initialized`);
|
|
234
|
-
}
|
|
235
|
-
if (!this.isComponentSet()) {
|
|
236
|
-
throw Error(`Component ${this.name} has not been registered yet`);
|
|
237
|
-
}
|
|
238
|
-
const instance = this.getOrInitializeService({
|
|
239
|
-
instanceIdentifier: normalizedIdentifier,
|
|
240
|
-
options
|
|
241
|
-
});
|
|
242
|
-
// resolve any pending promise waiting for the service instance
|
|
243
|
-
for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
|
|
244
|
-
const normalizedDeferredIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
|
|
245
|
-
if (normalizedIdentifier === normalizedDeferredIdentifier) {
|
|
246
|
-
instanceDeferred.resolve(instance);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
return instance;
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
*
|
|
253
|
-
* @param callback - a function that will be invoked after the provider has been initialized by calling provider.initialize().
|
|
254
|
-
* The function is invoked SYNCHRONOUSLY, so it should not execute any longrunning tasks in order to not block the program.
|
|
255
|
-
*
|
|
256
|
-
* @param identifier An optional instance identifier
|
|
257
|
-
* @returns a function to unregister the callback
|
|
258
|
-
*/
|
|
259
|
-
onInit(callback, identifier) {
|
|
260
|
-
var _a;
|
|
261
|
-
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
|
|
262
|
-
const existingCallbacks = (_a = this.onInitCallbacks.get(normalizedIdentifier)) !== null && _a !== void 0 ? _a : new Set();
|
|
263
|
-
existingCallbacks.add(callback);
|
|
264
|
-
this.onInitCallbacks.set(normalizedIdentifier, existingCallbacks);
|
|
265
|
-
const existingInstance = this.instances.get(normalizedIdentifier);
|
|
266
|
-
if (existingInstance) {
|
|
267
|
-
callback(existingInstance, normalizedIdentifier);
|
|
268
|
-
}
|
|
269
|
-
return () => {
|
|
270
|
-
existingCallbacks.delete(callback);
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Invoke onInit callbacks synchronously
|
|
275
|
-
* @param instance the service instance`
|
|
276
|
-
*/
|
|
277
|
-
invokeOnInitCallbacks(instance, identifier) {
|
|
278
|
-
const callbacks = this.onInitCallbacks.get(identifier);
|
|
279
|
-
if (!callbacks) {
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
for (const callback of callbacks) {
|
|
283
|
-
try {
|
|
284
|
-
callback(instance, identifier);
|
|
285
|
-
}
|
|
286
|
-
catch (_a) {
|
|
287
|
-
// ignore errors in the onInit callback
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
getOrInitializeService({ instanceIdentifier, options = {} }) {
|
|
292
|
-
let instance = this.instances.get(instanceIdentifier);
|
|
293
|
-
if (!instance && this.component) {
|
|
294
|
-
instance = this.component.instanceFactory(this.container, {
|
|
295
|
-
instanceIdentifier: normalizeIdentifierForFactory(instanceIdentifier),
|
|
296
|
-
options
|
|
297
|
-
});
|
|
298
|
-
this.instances.set(instanceIdentifier, instance);
|
|
299
|
-
this.instancesOptions.set(instanceIdentifier, options);
|
|
300
|
-
/**
|
|
301
|
-
* Invoke onInit listeners.
|
|
302
|
-
* Note this.component.onInstanceCreated is different, which is used by the component creator,
|
|
303
|
-
* while onInit listeners are registered by consumers of the provider.
|
|
304
|
-
*/
|
|
305
|
-
this.invokeOnInitCallbacks(instance, instanceIdentifier);
|
|
306
|
-
/**
|
|
307
|
-
* Order is important
|
|
308
|
-
* onInstanceCreated() should be called after this.instances.set(instanceIdentifier, instance); which
|
|
309
|
-
* makes `isInitialized()` return true.
|
|
310
|
-
*/
|
|
311
|
-
if (this.component.onInstanceCreated) {
|
|
312
|
-
try {
|
|
313
|
-
this.component.onInstanceCreated(this.container, instanceIdentifier, instance);
|
|
314
|
-
}
|
|
315
|
-
catch (_a) {
|
|
316
|
-
// ignore errors in the onInstanceCreatedCallback
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return instance || null;
|
|
321
|
-
}
|
|
322
|
-
normalizeInstanceIdentifier(identifier = DEFAULT_ENTRY_NAME) {
|
|
323
|
-
if (this.component) {
|
|
324
|
-
return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME;
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
return identifier; // assume multiple instances are supported before the component is provided.
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
shouldAutoInitialize() {
|
|
331
|
-
return (!!this.component &&
|
|
332
|
-
this.component.instantiationMode !== "EXPLICIT" /* InstantiationMode.EXPLICIT */);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
// undefined should be passed to the service factory for the default instance
|
|
336
|
-
function normalizeIdentifierForFactory(identifier) {
|
|
337
|
-
return identifier === DEFAULT_ENTRY_NAME ? undefined : identifier;
|
|
338
|
-
}
|
|
339
|
-
function isComponentEager(component) {
|
|
340
|
-
return component.instantiationMode === "EAGER" /* InstantiationMode.EAGER */;
|
|
65
|
+
/**
|
|
66
|
+
* @license
|
|
67
|
+
* Copyright 2019 Google LLC
|
|
68
|
+
*
|
|
69
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
70
|
+
* you may not use this file except in compliance with the License.
|
|
71
|
+
* You may obtain a copy of the License at
|
|
72
|
+
*
|
|
73
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
74
|
+
*
|
|
75
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
76
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
77
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
78
|
+
* See the License for the specific language governing permissions and
|
|
79
|
+
* limitations under the License.
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Provider for instance for service name T, e.g. 'auth', 'auth-internal'
|
|
83
|
+
* NameServiceMapping[T] is an alias for the type of the instance
|
|
84
|
+
*/
|
|
85
|
+
class Provider {
|
|
86
|
+
constructor(name, container) {
|
|
87
|
+
this.name = name;
|
|
88
|
+
this.container = container;
|
|
89
|
+
this.component = null;
|
|
90
|
+
this.instances = new Map();
|
|
91
|
+
this.instancesDeferred = new Map();
|
|
92
|
+
this.instancesOptions = new Map();
|
|
93
|
+
this.onInitCallbacks = new Map();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @param identifier A provider can provide multiple instances of a service
|
|
97
|
+
* if this.component.multipleInstances is true.
|
|
98
|
+
*/
|
|
99
|
+
get(identifier) {
|
|
100
|
+
// if multipleInstances is not supported, use the default name
|
|
101
|
+
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
|
|
102
|
+
if (!this.instancesDeferred.has(normalizedIdentifier)) {
|
|
103
|
+
const deferred = new util.Deferred();
|
|
104
|
+
this.instancesDeferred.set(normalizedIdentifier, deferred);
|
|
105
|
+
if (this.isInitialized(normalizedIdentifier) ||
|
|
106
|
+
this.shouldAutoInitialize()) {
|
|
107
|
+
// initialize the service if it can be auto-initialized
|
|
108
|
+
try {
|
|
109
|
+
const instance = this.getOrInitializeService({
|
|
110
|
+
instanceIdentifier: normalizedIdentifier
|
|
111
|
+
});
|
|
112
|
+
if (instance) {
|
|
113
|
+
deferred.resolve(instance);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
// when the instance factory throws an exception during get(), it should not cause
|
|
118
|
+
// a fatal error. We just return the unresolved promise in this case.
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return this.instancesDeferred.get(normalizedIdentifier).promise;
|
|
123
|
+
}
|
|
124
|
+
getImmediate(options) {
|
|
125
|
+
var _a;
|
|
126
|
+
// if multipleInstances is not supported, use the default name
|
|
127
|
+
const normalizedIdentifier = this.normalizeInstanceIdentifier(options === null || options === void 0 ? void 0 : options.identifier);
|
|
128
|
+
const optional = (_a = options === null || options === void 0 ? void 0 : options.optional) !== null && _a !== void 0 ? _a : false;
|
|
129
|
+
if (this.isInitialized(normalizedIdentifier) ||
|
|
130
|
+
this.shouldAutoInitialize()) {
|
|
131
|
+
try {
|
|
132
|
+
return this.getOrInitializeService({
|
|
133
|
+
instanceIdentifier: normalizedIdentifier
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
if (optional) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
throw e;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
// 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
|
|
147
|
+
if (optional) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
throw Error(`Service ${this.name} is not available`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
getComponent() {
|
|
156
|
+
return this.component;
|
|
157
|
+
}
|
|
158
|
+
setComponent(component) {
|
|
159
|
+
if (component.name !== this.name) {
|
|
160
|
+
throw Error(`Mismatching Component ${component.name} for Provider ${this.name}.`);
|
|
161
|
+
}
|
|
162
|
+
if (this.component) {
|
|
163
|
+
throw Error(`Component for ${this.name} has already been provided`);
|
|
164
|
+
}
|
|
165
|
+
this.component = component;
|
|
166
|
+
// return early without attempting to initialize the component if the component requires explicit initialization (calling `Provider.initialize()`)
|
|
167
|
+
if (!this.shouldAutoInitialize()) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
// if the service is eager, initialize the default instance
|
|
171
|
+
if (isComponentEager(component)) {
|
|
172
|
+
try {
|
|
173
|
+
this.getOrInitializeService({ instanceIdentifier: DEFAULT_ENTRY_NAME });
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
// when the instance factory for an eager Component throws an exception during the eager
|
|
177
|
+
// initialization, it should not cause a fatal error.
|
|
178
|
+
// TODO: Investigate if we need to make it configurable, because some component may want to cause
|
|
179
|
+
// a fatal error in this case?
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Create service instances for the pending promises and resolve them
|
|
183
|
+
// NOTE: if this.multipleInstances is false, only the default instance will be created
|
|
184
|
+
// and all promises with resolve with it regardless of the identifier.
|
|
185
|
+
for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
|
|
186
|
+
const normalizedIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
|
|
187
|
+
try {
|
|
188
|
+
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
|
|
189
|
+
const instance = this.getOrInitializeService({
|
|
190
|
+
instanceIdentifier: normalizedIdentifier
|
|
191
|
+
});
|
|
192
|
+
instanceDeferred.resolve(instance);
|
|
193
|
+
}
|
|
194
|
+
catch (e) {
|
|
195
|
+
// when the instance factory throws an exception, it should not cause
|
|
196
|
+
// a fatal error. We just leave the promise unresolved.
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
clearInstance(identifier = DEFAULT_ENTRY_NAME) {
|
|
201
|
+
this.instancesDeferred.delete(identifier);
|
|
202
|
+
this.instancesOptions.delete(identifier);
|
|
203
|
+
this.instances.delete(identifier);
|
|
204
|
+
}
|
|
205
|
+
// app.delete() will call this method on every provider to delete the services
|
|
206
|
+
// TODO: should we mark the provider as deleted?
|
|
207
|
+
async delete() {
|
|
208
|
+
const services = Array.from(this.instances.values());
|
|
209
|
+
await Promise.all([
|
|
210
|
+
...services
|
|
211
|
+
.filter(service => 'INTERNAL' in service) // legacy services
|
|
212
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
|
+
.map(service => service.INTERNAL.delete()),
|
|
214
|
+
...services
|
|
215
|
+
.filter(service => '_delete' in service) // modularized services
|
|
216
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
217
|
+
.map(service => service._delete())
|
|
218
|
+
]);
|
|
219
|
+
}
|
|
220
|
+
isComponentSet() {
|
|
221
|
+
return this.component != null;
|
|
222
|
+
}
|
|
223
|
+
isInitialized(identifier = DEFAULT_ENTRY_NAME) {
|
|
224
|
+
return this.instances.has(identifier);
|
|
225
|
+
}
|
|
226
|
+
getOptions(identifier = DEFAULT_ENTRY_NAME) {
|
|
227
|
+
return this.instancesOptions.get(identifier) || {};
|
|
228
|
+
}
|
|
229
|
+
initialize(opts = {}) {
|
|
230
|
+
const { options = {} } = opts;
|
|
231
|
+
const normalizedIdentifier = this.normalizeInstanceIdentifier(opts.instanceIdentifier);
|
|
232
|
+
if (this.isInitialized(normalizedIdentifier)) {
|
|
233
|
+
throw Error(`${this.name}(${normalizedIdentifier}) has already been initialized`);
|
|
234
|
+
}
|
|
235
|
+
if (!this.isComponentSet()) {
|
|
236
|
+
throw Error(`Component ${this.name} has not been registered yet`);
|
|
237
|
+
}
|
|
238
|
+
const instance = this.getOrInitializeService({
|
|
239
|
+
instanceIdentifier: normalizedIdentifier,
|
|
240
|
+
options
|
|
241
|
+
});
|
|
242
|
+
// resolve any pending promise waiting for the service instance
|
|
243
|
+
for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
|
|
244
|
+
const normalizedDeferredIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
|
|
245
|
+
if (normalizedIdentifier === normalizedDeferredIdentifier) {
|
|
246
|
+
instanceDeferred.resolve(instance);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return instance;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
*
|
|
253
|
+
* @param callback - a function that will be invoked after the provider has been initialized by calling provider.initialize().
|
|
254
|
+
* The function is invoked SYNCHRONOUSLY, so it should not execute any longrunning tasks in order to not block the program.
|
|
255
|
+
*
|
|
256
|
+
* @param identifier An optional instance identifier
|
|
257
|
+
* @returns a function to unregister the callback
|
|
258
|
+
*/
|
|
259
|
+
onInit(callback, identifier) {
|
|
260
|
+
var _a;
|
|
261
|
+
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
|
|
262
|
+
const existingCallbacks = (_a = this.onInitCallbacks.get(normalizedIdentifier)) !== null && _a !== void 0 ? _a : new Set();
|
|
263
|
+
existingCallbacks.add(callback);
|
|
264
|
+
this.onInitCallbacks.set(normalizedIdentifier, existingCallbacks);
|
|
265
|
+
const existingInstance = this.instances.get(normalizedIdentifier);
|
|
266
|
+
if (existingInstance) {
|
|
267
|
+
callback(existingInstance, normalizedIdentifier);
|
|
268
|
+
}
|
|
269
|
+
return () => {
|
|
270
|
+
existingCallbacks.delete(callback);
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Invoke onInit callbacks synchronously
|
|
275
|
+
* @param instance the service instance`
|
|
276
|
+
*/
|
|
277
|
+
invokeOnInitCallbacks(instance, identifier) {
|
|
278
|
+
const callbacks = this.onInitCallbacks.get(identifier);
|
|
279
|
+
if (!callbacks) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
for (const callback of callbacks) {
|
|
283
|
+
try {
|
|
284
|
+
callback(instance, identifier);
|
|
285
|
+
}
|
|
286
|
+
catch (_a) {
|
|
287
|
+
// ignore errors in the onInit callback
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
getOrInitializeService({ instanceIdentifier, options = {} }) {
|
|
292
|
+
let instance = this.instances.get(instanceIdentifier);
|
|
293
|
+
if (!instance && this.component) {
|
|
294
|
+
instance = this.component.instanceFactory(this.container, {
|
|
295
|
+
instanceIdentifier: normalizeIdentifierForFactory(instanceIdentifier),
|
|
296
|
+
options
|
|
297
|
+
});
|
|
298
|
+
this.instances.set(instanceIdentifier, instance);
|
|
299
|
+
this.instancesOptions.set(instanceIdentifier, options);
|
|
300
|
+
/**
|
|
301
|
+
* Invoke onInit listeners.
|
|
302
|
+
* Note this.component.onInstanceCreated is different, which is used by the component creator,
|
|
303
|
+
* while onInit listeners are registered by consumers of the provider.
|
|
304
|
+
*/
|
|
305
|
+
this.invokeOnInitCallbacks(instance, instanceIdentifier);
|
|
306
|
+
/**
|
|
307
|
+
* Order is important
|
|
308
|
+
* onInstanceCreated() should be called after this.instances.set(instanceIdentifier, instance); which
|
|
309
|
+
* makes `isInitialized()` return true.
|
|
310
|
+
*/
|
|
311
|
+
if (this.component.onInstanceCreated) {
|
|
312
|
+
try {
|
|
313
|
+
this.component.onInstanceCreated(this.container, instanceIdentifier, instance);
|
|
314
|
+
}
|
|
315
|
+
catch (_a) {
|
|
316
|
+
// ignore errors in the onInstanceCreatedCallback
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return instance || null;
|
|
321
|
+
}
|
|
322
|
+
normalizeInstanceIdentifier(identifier = DEFAULT_ENTRY_NAME) {
|
|
323
|
+
if (this.component) {
|
|
324
|
+
return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
return identifier; // assume multiple instances are supported before the component is provided.
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
shouldAutoInitialize() {
|
|
331
|
+
return (!!this.component &&
|
|
332
|
+
this.component.instantiationMode !== "EXPLICIT" /* InstantiationMode.EXPLICIT */);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
// undefined should be passed to the service factory for the default instance
|
|
336
|
+
function normalizeIdentifierForFactory(identifier) {
|
|
337
|
+
return identifier === DEFAULT_ENTRY_NAME ? undefined : identifier;
|
|
338
|
+
}
|
|
339
|
+
function isComponentEager(component) {
|
|
340
|
+
return component.instantiationMode === "EAGER" /* InstantiationMode.EAGER */;
|
|
341
341
|
}
|
|
342
342
|
|
|
343
|
-
/**
|
|
344
|
-
* @license
|
|
345
|
-
* Copyright 2019 Google LLC
|
|
346
|
-
*
|
|
347
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
348
|
-
* you may not use this file except in compliance with the License.
|
|
349
|
-
* You may obtain a copy of the License at
|
|
350
|
-
*
|
|
351
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
352
|
-
*
|
|
353
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
354
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
355
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
356
|
-
* See the License for the specific language governing permissions and
|
|
357
|
-
* limitations under the License.
|
|
358
|
-
*/
|
|
359
|
-
/**
|
|
360
|
-
* ComponentContainer that provides Providers for service name T, e.g. `auth`, `auth-internal`
|
|
361
|
-
*/
|
|
362
|
-
class ComponentContainer {
|
|
363
|
-
constructor(name) {
|
|
364
|
-
this.name = name;
|
|
365
|
-
this.providers = new Map();
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
*
|
|
369
|
-
* @param component Component being added
|
|
370
|
-
* @param overwrite When a component with the same name has already been registered,
|
|
371
|
-
* if overwrite is true: overwrite the existing component with the new component and create a new
|
|
372
|
-
* provider with the new component. It can be useful in tests where you want to use different mocks
|
|
373
|
-
* for different tests.
|
|
374
|
-
* if overwrite is false: throw an exception
|
|
375
|
-
*/
|
|
376
|
-
addComponent(component) {
|
|
377
|
-
const provider = this.getProvider(component.name);
|
|
378
|
-
if (provider.isComponentSet()) {
|
|
379
|
-
throw new Error(`Component ${component.name} has already been registered with ${this.name}`);
|
|
380
|
-
}
|
|
381
|
-
provider.setComponent(component);
|
|
382
|
-
}
|
|
383
|
-
addOrOverwriteComponent(component) {
|
|
384
|
-
const provider = this.getProvider(component.name);
|
|
385
|
-
if (provider.isComponentSet()) {
|
|
386
|
-
// delete the existing provider from the container, so we can register the new component
|
|
387
|
-
this.providers.delete(component.name);
|
|
388
|
-
}
|
|
389
|
-
this.addComponent(component);
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* getProvider provides a type safe interface where it can only be called with a field name
|
|
393
|
-
* present in NameServiceMapping interface.
|
|
394
|
-
*
|
|
395
|
-
* Firebase SDKs providing services should extend NameServiceMapping interface to register
|
|
396
|
-
* themselves.
|
|
397
|
-
*/
|
|
398
|
-
getProvider(name) {
|
|
399
|
-
if (this.providers.has(name)) {
|
|
400
|
-
return this.providers.get(name);
|
|
401
|
-
}
|
|
402
|
-
// create a Provider for a service that hasn't registered with Firebase
|
|
403
|
-
const provider = new Provider(name, this);
|
|
404
|
-
this.providers.set(name, provider);
|
|
405
|
-
return provider;
|
|
406
|
-
}
|
|
407
|
-
getProviders() {
|
|
408
|
-
return Array.from(this.providers.values());
|
|
409
|
-
}
|
|
343
|
+
/**
|
|
344
|
+
* @license
|
|
345
|
+
* Copyright 2019 Google LLC
|
|
346
|
+
*
|
|
347
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
348
|
+
* you may not use this file except in compliance with the License.
|
|
349
|
+
* You may obtain a copy of the License at
|
|
350
|
+
*
|
|
351
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
352
|
+
*
|
|
353
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
354
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
355
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
356
|
+
* See the License for the specific language governing permissions and
|
|
357
|
+
* limitations under the License.
|
|
358
|
+
*/
|
|
359
|
+
/**
|
|
360
|
+
* ComponentContainer that provides Providers for service name T, e.g. `auth`, `auth-internal`
|
|
361
|
+
*/
|
|
362
|
+
class ComponentContainer {
|
|
363
|
+
constructor(name) {
|
|
364
|
+
this.name = name;
|
|
365
|
+
this.providers = new Map();
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
*
|
|
369
|
+
* @param component Component being added
|
|
370
|
+
* @param overwrite When a component with the same name has already been registered,
|
|
371
|
+
* if overwrite is true: overwrite the existing component with the new component and create a new
|
|
372
|
+
* provider with the new component. It can be useful in tests where you want to use different mocks
|
|
373
|
+
* for different tests.
|
|
374
|
+
* if overwrite is false: throw an exception
|
|
375
|
+
*/
|
|
376
|
+
addComponent(component) {
|
|
377
|
+
const provider = this.getProvider(component.name);
|
|
378
|
+
if (provider.isComponentSet()) {
|
|
379
|
+
throw new Error(`Component ${component.name} has already been registered with ${this.name}`);
|
|
380
|
+
}
|
|
381
|
+
provider.setComponent(component);
|
|
382
|
+
}
|
|
383
|
+
addOrOverwriteComponent(component) {
|
|
384
|
+
const provider = this.getProvider(component.name);
|
|
385
|
+
if (provider.isComponentSet()) {
|
|
386
|
+
// delete the existing provider from the container, so we can register the new component
|
|
387
|
+
this.providers.delete(component.name);
|
|
388
|
+
}
|
|
389
|
+
this.addComponent(component);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* getProvider provides a type safe interface where it can only be called with a field name
|
|
393
|
+
* present in NameServiceMapping interface.
|
|
394
|
+
*
|
|
395
|
+
* Firebase SDKs providing services should extend NameServiceMapping interface to register
|
|
396
|
+
* themselves.
|
|
397
|
+
*/
|
|
398
|
+
getProvider(name) {
|
|
399
|
+
if (this.providers.has(name)) {
|
|
400
|
+
return this.providers.get(name);
|
|
401
|
+
}
|
|
402
|
+
// create a Provider for a service that hasn't registered with Firebase
|
|
403
|
+
const provider = new Provider(name, this);
|
|
404
|
+
this.providers.set(name, provider);
|
|
405
|
+
return provider;
|
|
406
|
+
}
|
|
407
|
+
getProviders() {
|
|
408
|
+
return Array.from(this.providers.values());
|
|
409
|
+
}
|
|
410
410
|
}
|
|
411
411
|
|
|
412
412
|
exports.Component = Component;
|