Skip to content

Migrate Performance to component framework #2325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions packages/performance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
*/

import firebase from '@firebase/app';
import '@firebase/installations';
import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
import {
_FirebaseNamespace,
FirebaseServiceFactory
} from '@firebase/app-types/private';
import { _FirebaseNamespace } from '@firebase/app-types/private';
import { PerformanceController } from './src/controllers/perf';
import { setupApi } from './src/services/api_service';
import { SettingsService } from './src/services/settings_service';
import { ERROR_FACTORY, ErrorCode } from './src/utils/errors';
import { FirebasePerformance } from '@firebase/performance-types';
import { Component, ComponentType } from '@firebase/component';
import { FirebaseInstallations } from '@firebase/installations-types';

const DEFAULT_ENTRY_NAME = '[DEFAULT]';

export function registerPerformance(instance: FirebaseNamespace): void {
const factoryMethod: FirebaseServiceFactory = (
app: FirebaseApp
const factoryMethod = (
app: FirebaseApp,
installations: FirebaseInstallations
): PerformanceController => {
if (app.name !== DEFAULT_ENTRY_NAME) {
throw ERROR_FACTORY.create(ErrorCode.FB_NOT_DEFAULT);
Expand All @@ -41,15 +42,27 @@ export function registerPerformance(instance: FirebaseNamespace): void {
}
setupApi(window);
SettingsService.getInstance().firebaseAppInstance = app;
SettingsService.getInstance().installationsService = installations;
return new PerformanceController(app);
};

// Register performance with firebase-app.
const namespaceExports = {};
(instance as _FirebaseNamespace).INTERNAL.registerService(
'performance',
factoryMethod,
namespaceExports
(instance as _FirebaseNamespace).INTERNAL.registerComponent(
new Component(
'performance',
container => {
/* Dependencies */
// getImmediate for FirebaseApp will always succeed
const app = container.getProvider('app').getImmediate();
// The following call will always succeed because perf has `import '@firebase/installations'`
const installations = container
.getProvider('installations')
.getImmediate();

return factoryMethod(app, installations);
},
ComponentType.PUBLIC
)
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/performance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@firebase/installations": "0.3.2",
"@firebase/util": "0.2.31",
"@firebase/performance-types": "0.0.5",
"@firebase/component": "0.1.0",
"tslib": "1.10.0"
},
"license": "Apache-2.0",
Expand Down
15 changes: 9 additions & 6 deletions packages/performance/src/services/iid_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ import {
getAuthenticationToken,
getAuthTokenPromise
} from './iid_service';
import { FirebaseApp } from '@firebase/app-types';
import '../../test/setup';
import { FirebaseInstallations } from '@firebase/installations-types';

describe('Firebase Perofmrance > iid_service', () => {
const IID = 'fid';
const AUTH_TOKEN = 'authToken';
const getId = stub().resolves(IID);
const getToken = stub().resolves(AUTH_TOKEN);

SettingsService.prototype.firebaseAppInstance = ({
installations: () => ({ getId, getToken })
} as unknown) as FirebaseApp;
before(() => {
const getId = stub().resolves(IID);
const getToken = stub().resolves(AUTH_TOKEN);
SettingsService.prototype.installationsService = ({
getId,
getToken
} as unknown) as FirebaseInstallations;
});

describe('getIidPromise', () => {
it('provides iid', async () => {
Expand Down
10 changes: 2 additions & 8 deletions packages/performance/src/services/iid_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import '@firebase/installations';
import { SettingsService } from './settings_service';

let iid: string | undefined;
let authToken: string | undefined;

export function getIidPromise(): Promise<string> {
const iidPromise = SettingsService.getInstance()
.firebaseAppInstance.installations()
.getId();
const iidPromise = SettingsService.getInstance().installationsService.getId();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
iidPromise.then((iidVal: string) => {
iid = iidVal;
Expand All @@ -38,9 +34,7 @@ export function getIid(): string | undefined {
}

export function getAuthTokenPromise(): Promise<string> {
const authTokenPromise = SettingsService.getInstance()
.firebaseAppInstance.installations()
.getToken();
const authTokenPromise = SettingsService.getInstance().installationsService.getToken();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
authTokenPromise.then((authTokenVal: string) => {
authToken = authTokenVal;
Expand Down
3 changes: 3 additions & 0 deletions packages/performance/src/services/settings_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { FirebaseApp } from '@firebase/app-types';
import { ERROR_FACTORY, ErrorCode } from '../utils/errors';
import { FirebaseInstallations } from '@firebase/installations-types';

let settingsServiceInstance: SettingsService | undefined;

Expand Down Expand Up @@ -46,6 +47,8 @@ export class SettingsService {

firebaseAppInstance!: FirebaseApp;

installationsService!: FirebaseInstallations;

getAppId(): string {
const appId =
this.firebaseAppInstance &&
Expand Down