@firebase/component 0.6.10 → 0.6.11-canary.ffbf5a60a

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