18
18
import { Deferred } from '@firebase/util' ;
19
19
import { ComponentContainer } from './component_container' ;
20
20
import { DEFAULT_ENTRY_NAME } from './constants' ;
21
- import { InstantiationMode , Name , NameServiceMapping } from './types' ;
21
+ import {
22
+ InitializeOptions ,
23
+ InstantiationMode ,
24
+ Name ,
25
+ NameServiceMapping
26
+ } from './types' ;
22
27
import { Component } from './component' ;
23
28
24
29
/**
@@ -51,7 +56,9 @@ export class Provider<T extends Name> {
51
56
this . instancesDeferred . set ( normalizedIdentifier , deferred ) ;
52
57
// If the service instance is available, resolve the promise with it immediately
53
58
try {
54
- const instance = this . getOrInitializeService ( normalizedIdentifier ) ;
59
+ const instance = this . getOrInitializeService ( {
60
+ instanceIdentifier : normalizedIdentifier
61
+ } ) ;
55
62
if ( instance ) {
56
63
deferred . resolve ( instance ) ;
57
64
}
@@ -92,7 +99,9 @@ export class Provider<T extends Name> {
92
99
// if multipleInstances is not supported, use the default name
93
100
const normalizedIdentifier = this . normalizeInstanceIdentifier ( identifier ) ;
94
101
try {
95
- const instance = this . getOrInitializeService ( normalizedIdentifier ) ;
102
+ const instance = this . getOrInitializeService ( {
103
+ instanceIdentifier : normalizedIdentifier
104
+ } ) ;
96
105
97
106
if ( ! instance ) {
98
107
if ( optional ) {
@@ -129,7 +138,7 @@ export class Provider<T extends Name> {
129
138
// if the service is eager, initialize the default instance
130
139
if ( isComponentEager ( component ) ) {
131
140
try {
132
- this . getOrInitializeService ( DEFAULT_ENTRY_NAME ) ;
141
+ this . getOrInitializeService ( { instanceIdentifier : DEFAULT_ENTRY_NAME } ) ;
133
142
} catch ( e ) {
134
143
// when the instance factory for an eager Component throws an exception during the eager
135
144
// initialization, it should not cause a fatal error.
@@ -151,7 +160,9 @@ export class Provider<T extends Name> {
151
160
152
161
try {
153
162
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
154
- const instance = this . getOrInitializeService ( normalizedIdentifier ) ! ;
163
+ const instance = this . getOrInitializeService ( {
164
+ instanceIdentifier : normalizedIdentifier
165
+ } ) ! ;
155
166
instanceDeferred . resolve ( instance ) ;
156
167
} catch ( e ) {
157
168
// when the instance factory throws an exception, it should not cause
@@ -190,16 +201,41 @@ export class Provider<T extends Name> {
190
201
return this . instances . has ( identifier ) ;
191
202
}
192
203
193
- private getOrInitializeService (
194
- identifier : string
195
- ) : NameServiceMapping [ T ] | null {
196
- let instance = this . instances . get ( identifier ) ;
204
+ initialize ( opts : InitializeOptions = { } ) : NameServiceMapping [ T ] {
205
+ const { instanceIdentifier = DEFAULT_ENTRY_NAME , options = { } } = opts ;
206
+ const normalizedIdentifier = this . normalizeInstanceIdentifier (
207
+ instanceIdentifier
208
+ ) ;
209
+ if ( this . isInitialized ( normalizedIdentifier ) ) {
210
+ throw Error (
211
+ `${ this . name } (${ normalizedIdentifier } ) has already been initialized`
212
+ ) ;
213
+ }
214
+
215
+ if ( ! this . isComponentSet ( ) ) {
216
+ throw Error ( `Component ${ this . name } has not been registered yet` ) ;
217
+ }
218
+
219
+ return this . getOrInitializeService ( {
220
+ instanceIdentifier : normalizedIdentifier ,
221
+ options
222
+ } ) ! ;
223
+ }
224
+
225
+ private getOrInitializeService ( {
226
+ instanceIdentifier,
227
+ options = { }
228
+ } : {
229
+ instanceIdentifier : string ;
230
+ options ?: Record < string , unknown > ;
231
+ } ) : NameServiceMapping [ T ] | null {
232
+ let instance = this . instances . get ( instanceIdentifier ) ;
197
233
if ( ! instance && this . component ) {
198
- instance = this . component . instanceFactory (
199
- this . container ,
200
- normalizeIdentifierForFactory ( identifier )
201
- ) as NameServiceMapping [ T ] ;
202
- this . instances . set ( identifier , instance ) ;
234
+ instance = this . component . instanceFactory ( this . container , {
235
+ instanceIdentifier : normalizeIdentifierForFactory ( instanceIdentifier ) ,
236
+ options
237
+ } ) ;
238
+ this . instances . set ( instanceIdentifier , instance ) ;
203
239
}
204
240
205
241
return instance || null ;
0 commit comments