diff --git a/common/api-review/app-exp.api.md b/common/api-review/app-exp.api.md index 6b21f3620ed..d635a36ec9b 100644 --- a/common/api-review/app-exp.api.md +++ b/common/api-review/app-exp.api.md @@ -58,6 +58,9 @@ export function _registerComponent(component: Component): boolean; // @public export function registerVersion(libraryKeyOrName: string, version: string, variant?: string): void; +// @internal (undocumented) +export function _removeServiceInstance(app: FirebaseApp, name: T, instanceIdentifier?: string): void; + // @public export const SDK_VERSION: string; diff --git a/packages-exp/app-exp/src/constants.ts b/packages-exp/app-exp/src/constants.ts index e1881057de9..c72a9afdfbc 100644 --- a/packages-exp/app-exp/src/constants.ts +++ b/packages-exp/app-exp/src/constants.ts @@ -15,7 +15,6 @@ * limitations under the License. */ -export const DEFAULT_ENTRY_NAME = '[DEFAULT]'; import { name as appName } from '../package.json'; import { name as analyticsName } from '../../../packages/analytics/package.json'; import { name as authName } from '../../../packages/auth/package.json'; @@ -29,6 +28,8 @@ import { name as storageName } from '../../../packages/storage/package.json'; import { name as firestoreName } from '../../../packages/firestore/package.json'; import { name as packageName } from '../../../packages/firebase/package.json'; +export const DEFAULT_ENTRY_NAME = '[DEFAULT]'; + export const PLATFORM_LOG_STRING = { [appName]: 'fire-core', [analyticsName]: 'fire-analytics', diff --git a/packages-exp/app-exp/src/internal.test.ts b/packages-exp/app-exp/src/internal.test.ts index cfb8971ef8e..b52cacaaba7 100644 --- a/packages-exp/app-exp/src/internal.test.ts +++ b/packages-exp/app-exp/src/internal.test.ts @@ -25,7 +25,9 @@ import { _addOrOverwriteComponent, _registerComponent, _components, - _clearComponents + _clearComponents, + _getProvider, + _removeServiceInstance } from './internal'; import { _FirebaseAppInternal } from '@firebase/app-types-exp'; @@ -40,9 +42,10 @@ describe('Internal API tests', () => { for (const app of getApps()) { deleteApp(app).catch(() => {}); } + _clearComponents(); }); - describe('addComponent', () => { + describe('_addComponent', () => { it('registers component with App', () => { const app = initializeApp({}) as _FirebaseAppInternal; const testComp = createTestComponent('test'); @@ -67,7 +70,7 @@ describe('Internal API tests', () => { }); }); - describe('addOrOverwriteComponent', () => { + describe('_addOrOverwriteComponent', () => { it('registers component with App', () => { const app = initializeApp({}) as _FirebaseAppInternal; const testComp = createTestComponent('test'); @@ -93,11 +96,7 @@ describe('Internal API tests', () => { }); }); - describe('registerComponent', () => { - afterEach(() => { - _clearComponents(); - }); - + describe('_registerComponent', () => { it('caches a component and registers it with all Apps', () => { const app1 = initializeApp({}) as _FirebaseAppInternal; const app2 = initializeApp({}, 'app2') as _FirebaseAppInternal; @@ -130,4 +129,33 @@ describe('Internal API tests', () => { expect(_components.get('test')).to.equal(testComp1); }); }); + + describe('_getProvider', () => { + it('gets provider for a service', () => { + const app1 = initializeApp({}) as _FirebaseAppInternal; + const testComp = createTestComponent('test'); + _registerComponent(testComp); + + const provider = _getProvider(app1, 'test'); + expect(provider.getComponent()).to.equal(testComp); + }); + }); + + describe('_removeServiceInstance', () => { + it('removes a service instance', () => { + const app1 = initializeApp({}) as _FirebaseAppInternal; + const testComp = createTestComponent('test'); + _registerComponent(testComp); + const provider = app1.container.getProvider('test'); + + // instantiate a service instance + const instance1 = provider.getImmediate(); + _removeServiceInstance(app1, 'test'); + + // should get a new instance since the previous instance has been removed + const instance2 = provider.getImmediate(); + + expect(instance1).to.not.equal(instance2); + }); + }); }); diff --git a/packages-exp/app-exp/src/internal.ts b/packages-exp/app-exp/src/internal.ts index d95d05d5074..3fb863d0db2 100644 --- a/packages-exp/app-exp/src/internal.ts +++ b/packages-exp/app-exp/src/internal.ts @@ -18,6 +18,7 @@ import { _FirebaseAppInternal, FirebaseApp } from '@firebase/app-types-exp'; import { Component, Provider, Name } from '@firebase/component'; import { logger } from './logger'; +import { DEFAULT_ENTRY_NAME } from './constants'; /** * @internal @@ -102,6 +103,22 @@ export function _getProvider( return (app as _FirebaseAppInternal).container.getProvider(name); } +/** + * + * @param app - FirebaseApp instance + * @param name - service name + * @param instanceIdentifier - service instance identifier in case the service supports multiple instances + * + * @internal + */ +export function _removeServiceInstance( + app: FirebaseApp, + name: T, + instanceIdentifier: string = DEFAULT_ENTRY_NAME +): void { + _getProvider(app, name).clearInstance(instanceIdentifier); +} + /** * Test only * diff --git a/packages-exp/app-exp/test/util.ts b/packages-exp/app-exp/test/util.ts index 3161ca8bdb6..5de89e70294 100644 --- a/packages-exp/app-exp/test/util.ts +++ b/packages-exp/app-exp/test/util.ts @@ -15,11 +15,10 @@ * limitations under the License. */ -import { FirebaseService } from '@firebase/app-types/private'; -import { FirebaseApp } from '@firebase/app-types'; +import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp'; import { ComponentType, Component } from '@firebase/component'; -export class TestService implements FirebaseService { +export class TestService implements _FirebaseService { constructor(private app_: FirebaseApp, public instanceIdentifier?: string) {} get app(): FirebaseApp { @@ -41,7 +40,8 @@ export function createTestComponent( const component = new Component( // eslint-disable-next-line @typescript-eslint/no-explicit-any name as any, - container => new TestService(container.getProvider('app').getImmediate()), + container => + new TestService(container.getProvider('app-exp').getImmediate()), type ); component.setMultipleInstances(multiInstances); diff --git a/packages-exp/app-types-exp/index.d.ts b/packages-exp/app-types-exp/index.d.ts index e561a2c21aa..91e4a57c534 100644 --- a/packages-exp/app-types-exp/index.d.ts +++ b/packages-exp/app-types-exp/index.d.ts @@ -116,9 +116,15 @@ export interface _FirebaseService { delete(): Promise; } +export interface VersionService { + library: string; + version: string; +} + declare module '@firebase/component' { interface NameServiceMapping { 'app-exp': FirebaseApp; + 'app-version': VersionService; 'platform-logger': PlatformLoggerService; } }