Skip to content

Commit

Permalink
add _removeServiceInstance() to app-exp (#3173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feiyang1 authored Jun 16, 2020
1 parent 9526955 commit c4b7595
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
3 changes: 3 additions & 0 deletions common/api-review/app-exp.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Name>(app: FirebaseApp, name: T, instanceIdentifier?: string): void;

// @public
export const SDK_VERSION: string;

Expand Down
3 changes: 2 additions & 1 deletion packages-exp/app-exp/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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',
Expand Down
44 changes: 36 additions & 8 deletions packages-exp/app-exp/src/internal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
_addOrOverwriteComponent,
_registerComponent,
_components,
_clearComponents
_clearComponents,
_getProvider,
_removeServiceInstance
} from './internal';
import { _FirebaseAppInternal } from '@firebase/app-types-exp';

Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
});
});
17 changes: 17 additions & 0 deletions packages-exp/app-exp/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,6 +103,22 @@ export function _getProvider<T extends Name>(
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<T extends Name>(
app: FirebaseApp,
name: T,
instanceIdentifier: string = DEFAULT_ENTRY_NAME
): void {
_getProvider(app, name).clearInstance(instanceIdentifier);
}

/**
* Test only
*
Expand Down
8 changes: 4 additions & 4 deletions packages-exp/app-exp/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions packages-exp/app-types-exp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,15 @@ export interface _FirebaseService {
delete(): Promise<void>;
}

export interface VersionService {
library: string;
version: string;
}

declare module '@firebase/component' {
interface NameServiceMapping {
'app-exp': FirebaseApp;
'app-version': VersionService;
'platform-logger': PlatformLoggerService;
}
}

0 comments on commit c4b7595

Please sign in to comment.