Skip to content

Add initializePerformance() to performance-exp #4456

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 2 commits into from
Feb 11, 2021
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
50 changes: 50 additions & 0 deletions packages-exp/performance-exp/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { stub } from 'sinon';
import { initializePerformance } from './index';
import { ERROR_FACTORY, ErrorCode } from './utils/errors';
import * as firebase from '@firebase/app-exp';
import { Provider } from '@firebase/component';
import '../test/setup';

const fakeFirebaseConfig = {
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
databaseURL: 'https://project-id.firebaseio.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: '1:111:web:a1234'
};

const fakeFirebaseApp = ({
options: fakeFirebaseConfig
} as unknown) as firebase.FirebaseApp;

describe('Firebase Performance > initializePerformance()', () => {
it('throws if a perf instance has already been created', () => {
stub(firebase, '_getProvider').returns(({
isInitialized: () => true
} as unknown) as Provider<'performance-exp'>);
const expectedError = ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
expect(() => initializePerformance(fakeFirebaseApp)).to.throw(
expectedError.message
);
});
});
20 changes: 19 additions & 1 deletion packages-exp/performance-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,32 @@ const DEFAULT_ENTRY_NAME = '[DEFAULT]';
/**
* Returns a FirebasePerformance instance for the given app.
* @param app - The FirebaseApp to use.
* @public
*/
export function getPerformance(app: FirebaseApp): FirebasePerformance {
const provider = _getProvider(app, 'performance-exp');
const perfInstance = provider.getImmediate() as PerformanceController;
return perfInstance;
}

/**
* Returns a FirebasePerformance instance for the given app. Can only be called once.
* @param app - The FirebaseApp to use.
* @param settings - Optional settings for the Performance instance.
* @public
*/
export function getPerformance(
export function initializePerformance(
app: FirebaseApp,
settings?: PerformanceSettings
): FirebasePerformance {
const provider = _getProvider(app, 'performance-exp');

// throw if an instance was already created.
// It could happen if initializePerformance() is called more than once, or getPerformance() is called first.
if (provider.isInitialized()) {
throw ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
}

const perfInstance = provider.getImmediate() as PerformanceController;
perfInstance._init(settings);
return perfInstance;
Expand Down
6 changes: 4 additions & 2 deletions packages-exp/performance-exp/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const enum ErrorCode {
INVALID_ATTRIBUTE_NAME = 'invalid attribute name',
INVALID_ATTRIBUTE_VALUE = 'invalid attribute value',
INVALID_CUSTOM_METRIC_NAME = 'invalid custom metric name',
INVALID_STRING_MERGER_PARAMETER = 'invalid String merger input'
INVALID_STRING_MERGER_PARAMETER = 'invalid String merger input',
ALREADY_INITIALIZED = 'already initialized'
}

const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
Expand All @@ -58,7 +59,8 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
[ErrorCode.INVALID_CUSTOM_METRIC_NAME]:
'Custom metric name {$customMetricName} is invalid',
[ErrorCode.INVALID_STRING_MERGER_PARAMETER]:
'Input for String merger is invalid, contact support team to resolve.'
'Input for String merger is invalid, contact support team to resolve.',
[ErrorCode.ALREADY_INITIALIZED]: 'Performance can only be initialized once.'
};

interface ErrorParams {
Expand Down
2 changes: 0 additions & 2 deletions packages-exp/performance-exp/src/utils/string_merger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import { expect } from 'chai';

import { mergeStrings } from './string_merger';
import { FirebaseError } from '@firebase/util';
// import { ERROR_FACTORY, ErrorCode } from './errors';
import '../../test/setup';

describe('Firebase Performance > string_merger', () => {
describe('#mergeStrings', () => {
it('Throws exception when string length has | diff | > 1', () => {
// const expectedError = ERROR_FACTORY.create(ErrorCode.INVALID_STRING_MERGER_PARAMETER);
expect(() => mergeStrings('', '123')).to.throw(
FirebaseError,
'performance/invalid String merger input'
Expand Down
4 changes: 4 additions & 0 deletions packages/component/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export class Provider<T extends Name> {
return this.component != null;
}

isInitialized(identifier: string = DEFAULT_ENTRY_NAME): boolean {
return this.instances.has(identifier);
}

private getOrInitializeService(
identifier: string
): NameServiceMapping[T] | null {
Expand Down