Skip to content

Commit 80ae4d1

Browse files
authored
Added an internal API to allow removing a service instance (#2020)
* Add an internal API to allow removing a service instance * [AUTOMATED]: Prettier Code Styling * add a test case for service that supports multiple instances * [AUTOMATED]: Prettier Code Styling
1 parent d21965d commit 80ae4d1

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

packages/app-types/private.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export interface FirebaseAppInternals {
7474

7575
export interface _FirebaseApp extends FirebaseApp {
7676
INTERNAL: FirebaseAppInternals;
77+
_removeServiceInstance: (name: string, instanceIdentifier?: string) => void;
7778
}
7879
export interface _FirebaseNamespace extends FirebaseNamespace {
7980
INTERNAL: {

packages/app/src/firebaseApp.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class FirebaseAppImpl implements FirebaseApp {
129129
* Return a service instance associated with this app (creating it
130130
* on demand), identified by the passed instanceIdentifier.
131131
*
132-
* NOTE: Currently storage is the only one that is leveraging this
132+
* NOTE: Currently storage and functions are the only ones that are leveraging this
133133
* functionality. They invoke it by calling:
134134
*
135135
* ```javascript
@@ -168,6 +168,24 @@ export class FirebaseAppImpl implements FirebaseApp {
168168

169169
return this.services_[name][instanceIdentifier];
170170
}
171+
/**
172+
* Remove a service instance from the cache, so we will create a new instance for this service
173+
* when people try to get this service again.
174+
*
175+
* NOTE: currently only firestore is using this functionality to support firestore shutdown.
176+
*
177+
* @param name The service name
178+
* @param instanceIdentifier instance identifier in case multiple instances are allowed
179+
* @internal
180+
*/
181+
_removeServiceInstance(
182+
name: string,
183+
instanceIdentifier: string = DEFAULT_ENTRY_NAME
184+
): void {
185+
if (this.services_[name] && this.services_[name][instanceIdentifier]) {
186+
delete this.services_[name][instanceIdentifier];
187+
}
188+
}
171189

172190
/**
173191
* Callback function used to extend an App instance at the time

packages/app/test/firebaseApp.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,58 @@ function executeFirebaseTests(): void {
276276
assert.equal('tokenFor1', token!.accessToken);
277277
});
278278
});
279+
280+
it(`Should create a new instance of a service after removing the existing instance`, () => {
281+
const app = firebase.initializeApp({});
282+
(firebase as _FirebaseNamespace).INTERNAL.registerService(
283+
'test',
284+
(app: FirebaseApp) => {
285+
return new TestService(app);
286+
}
287+
);
288+
289+
const service = (firebase as any).test();
290+
291+
assert.equal(service, (firebase as any).test());
292+
293+
(app as _FirebaseApp)._removeServiceInstance('test');
294+
295+
assert.notEqual(service, (firebase as any).test());
296+
});
297+
298+
it(`Should create a new instance of a service after removing the existing instance - for service that supports multiple instances`, () => {
299+
const app = firebase.initializeApp({});
300+
(firebase as _FirebaseNamespace).INTERNAL.registerService(
301+
'multiInstance',
302+
(...args) => {
303+
const [app, , instanceIdentifier] = args;
304+
return new TestService(app, instanceIdentifier);
305+
},
306+
undefined,
307+
undefined,
308+
true
309+
);
310+
311+
// default instance
312+
const instance1 = (firebase.app() as any).multiInstance();
313+
const serviceIdentifier = 'custom instance identifier';
314+
const instance2 = (firebase.app() as any).multiInstance(
315+
serviceIdentifier
316+
);
317+
318+
(app as _FirebaseApp)._removeServiceInstance(
319+
'multiInstance',
320+
serviceIdentifier
321+
);
322+
323+
// default instance should not be changed
324+
assert.equal(instance1, (firebase.app() as any).multiInstance());
325+
326+
assert.notEqual(
327+
instance2,
328+
(firebase.app() as any).multiInstance(serviceIdentifier)
329+
);
330+
});
279331
});
280332
}
281333

0 commit comments

Comments
 (0)