Skip to content

Commit c4b7595

Browse files
authored
add _removeServiceInstance() to app-exp (firebase#3173)
1 parent 9526955 commit c4b7595

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed

common/api-review/app-exp.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export function _registerComponent(component: Component): boolean;
5858
// @public
5959
export function registerVersion(libraryKeyOrName: string, version: string, variant?: string): void;
6060

61+
// @internal (undocumented)
62+
export function _removeServiceInstance<T extends Name>(app: FirebaseApp, name: T, instanceIdentifier?: string): void;
63+
6164
// @public
6265
export const SDK_VERSION: string;
6366

packages-exp/app-exp/src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
export const DEFAULT_ENTRY_NAME = '[DEFAULT]';
1918
import { name as appName } from '../package.json';
2019
import { name as analyticsName } from '../../../packages/analytics/package.json';
2120
import { name as authName } from '../../../packages/auth/package.json';
@@ -29,6 +28,8 @@ import { name as storageName } from '../../../packages/storage/package.json';
2928
import { name as firestoreName } from '../../../packages/firestore/package.json';
3029
import { name as packageName } from '../../../packages/firebase/package.json';
3130

31+
export const DEFAULT_ENTRY_NAME = '[DEFAULT]';
32+
3233
export const PLATFORM_LOG_STRING = {
3334
[appName]: 'fire-core',
3435
[analyticsName]: 'fire-analytics',

packages-exp/app-exp/src/internal.test.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import {
2525
_addOrOverwriteComponent,
2626
_registerComponent,
2727
_components,
28-
_clearComponents
28+
_clearComponents,
29+
_getProvider,
30+
_removeServiceInstance
2931
} from './internal';
3032
import { _FirebaseAppInternal } from '@firebase/app-types-exp';
3133

@@ -40,9 +42,10 @@ describe('Internal API tests', () => {
4042
for (const app of getApps()) {
4143
deleteApp(app).catch(() => {});
4244
}
45+
_clearComponents();
4346
});
4447

45-
describe('addComponent', () => {
48+
describe('_addComponent', () => {
4649
it('registers component with App', () => {
4750
const app = initializeApp({}) as _FirebaseAppInternal;
4851
const testComp = createTestComponent('test');
@@ -67,7 +70,7 @@ describe('Internal API tests', () => {
6770
});
6871
});
6972

70-
describe('addOrOverwriteComponent', () => {
73+
describe('_addOrOverwriteComponent', () => {
7174
it('registers component with App', () => {
7275
const app = initializeApp({}) as _FirebaseAppInternal;
7376
const testComp = createTestComponent('test');
@@ -93,11 +96,7 @@ describe('Internal API tests', () => {
9396
});
9497
});
9598

96-
describe('registerComponent', () => {
97-
afterEach(() => {
98-
_clearComponents();
99-
});
100-
99+
describe('_registerComponent', () => {
101100
it('caches a component and registers it with all Apps', () => {
102101
const app1 = initializeApp({}) as _FirebaseAppInternal;
103102
const app2 = initializeApp({}, 'app2') as _FirebaseAppInternal;
@@ -130,4 +129,33 @@ describe('Internal API tests', () => {
130129
expect(_components.get('test')).to.equal(testComp1);
131130
});
132131
});
132+
133+
describe('_getProvider', () => {
134+
it('gets provider for a service', () => {
135+
const app1 = initializeApp({}) as _FirebaseAppInternal;
136+
const testComp = createTestComponent('test');
137+
_registerComponent(testComp);
138+
139+
const provider = _getProvider(app1, 'test');
140+
expect(provider.getComponent()).to.equal(testComp);
141+
});
142+
});
143+
144+
describe('_removeServiceInstance', () => {
145+
it('removes a service instance', () => {
146+
const app1 = initializeApp({}) as _FirebaseAppInternal;
147+
const testComp = createTestComponent('test');
148+
_registerComponent(testComp);
149+
const provider = app1.container.getProvider('test');
150+
151+
// instantiate a service instance
152+
const instance1 = provider.getImmediate();
153+
_removeServiceInstance(app1, 'test');
154+
155+
// should get a new instance since the previous instance has been removed
156+
const instance2 = provider.getImmediate();
157+
158+
expect(instance1).to.not.equal(instance2);
159+
});
160+
});
133161
});

packages-exp/app-exp/src/internal.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { _FirebaseAppInternal, FirebaseApp } from '@firebase/app-types-exp';
1919
import { Component, Provider, Name } from '@firebase/component';
2020
import { logger } from './logger';
21+
import { DEFAULT_ENTRY_NAME } from './constants';
2122

2223
/**
2324
* @internal
@@ -102,6 +103,22 @@ export function _getProvider<T extends Name>(
102103
return (app as _FirebaseAppInternal).container.getProvider(name);
103104
}
104105

106+
/**
107+
*
108+
* @param app - FirebaseApp instance
109+
* @param name - service name
110+
* @param instanceIdentifier - service instance identifier in case the service supports multiple instances
111+
*
112+
* @internal
113+
*/
114+
export function _removeServiceInstance<T extends Name>(
115+
app: FirebaseApp,
116+
name: T,
117+
instanceIdentifier: string = DEFAULT_ENTRY_NAME
118+
): void {
119+
_getProvider(app, name).clearInstance(instanceIdentifier);
120+
}
121+
105122
/**
106123
* Test only
107124
*

packages-exp/app-exp/test/util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseService } from '@firebase/app-types/private';
19-
import { FirebaseApp } from '@firebase/app-types';
18+
import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp';
2019
import { ComponentType, Component } from '@firebase/component';
2120

22-
export class TestService implements FirebaseService {
21+
export class TestService implements _FirebaseService {
2322
constructor(private app_: FirebaseApp, public instanceIdentifier?: string) {}
2423

2524
get app(): FirebaseApp {
@@ -41,7 +40,8 @@ export function createTestComponent(
4140
const component = new Component(
4241
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4342
name as any,
44-
container => new TestService(container.getProvider('app').getImmediate()),
43+
container =>
44+
new TestService(container.getProvider('app-exp').getImmediate()),
4545
type
4646
);
4747
component.setMultipleInstances(multiInstances);

packages-exp/app-types-exp/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,15 @@ export interface _FirebaseService {
116116
delete(): Promise<void>;
117117
}
118118

119+
export interface VersionService {
120+
library: string;
121+
version: string;
122+
}
123+
119124
declare module '@firebase/component' {
120125
interface NameServiceMapping {
121126
'app-exp': FirebaseApp;
127+
'app-version': VersionService;
122128
'platform-logger': PlatformLoggerService;
123129
}
124130
}

0 commit comments

Comments
 (0)