Skip to content

Commit 5c2669a

Browse files
authored
Add initializePerformance() to performance-exp (#4456)
* Add initializePerformance() * add test for initializePerforamnce
1 parent ac05b13 commit 5c2669a

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
import { stub } from 'sinon';
20+
import { initializePerformance } from './index';
21+
import { ERROR_FACTORY, ErrorCode } from './utils/errors';
22+
import * as firebase from '@firebase/app-exp';
23+
import { Provider } from '@firebase/component';
24+
import '../test/setup';
25+
26+
const fakeFirebaseConfig = {
27+
apiKey: 'api-key',
28+
authDomain: 'project-id.firebaseapp.com',
29+
databaseURL: 'https://project-id.firebaseio.com',
30+
projectId: 'project-id',
31+
storageBucket: 'project-id.appspot.com',
32+
messagingSenderId: 'sender-id',
33+
appId: '1:111:web:a1234'
34+
};
35+
36+
const fakeFirebaseApp = ({
37+
options: fakeFirebaseConfig
38+
} as unknown) as firebase.FirebaseApp;
39+
40+
describe('Firebase Performance > initializePerformance()', () => {
41+
it('throws if a perf instance has already been created', () => {
42+
stub(firebase, '_getProvider').returns(({
43+
isInitialized: () => true
44+
} as unknown) as Provider<'performance-exp'>);
45+
const expectedError = ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
46+
expect(() => initializePerformance(fakeFirebaseApp)).to.throw(
47+
expectedError.message
48+
);
49+
});
50+
});

packages-exp/performance-exp/src/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@ const DEFAULT_ENTRY_NAME = '[DEFAULT]';
4444
/**
4545
* Returns a FirebasePerformance instance for the given app.
4646
* @param app - The FirebaseApp to use.
47+
* @public
48+
*/
49+
export function getPerformance(app: FirebaseApp): FirebasePerformance {
50+
const provider = _getProvider(app, 'performance-exp');
51+
const perfInstance = provider.getImmediate() as PerformanceController;
52+
return perfInstance;
53+
}
54+
55+
/**
56+
* Returns a FirebasePerformance instance for the given app. Can only be called once.
57+
* @param app - The FirebaseApp to use.
4758
* @param settings - Optional settings for the Performance instance.
4859
* @public
4960
*/
50-
export function getPerformance(
61+
export function initializePerformance(
5162
app: FirebaseApp,
5263
settings?: PerformanceSettings
5364
): FirebasePerformance {
5465
const provider = _getProvider(app, 'performance-exp');
66+
67+
// throw if an instance was already created.
68+
// It could happen if initializePerformance() is called more than once, or getPerformance() is called first.
69+
if (provider.isInitialized()) {
70+
throw ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
71+
}
72+
5573
const perfInstance = provider.getImmediate() as PerformanceController;
5674
perfInstance._init(settings);
5775
return perfInstance;

packages-exp/performance-exp/src/utils/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const enum ErrorCode {
3333
INVALID_ATTRIBUTE_NAME = 'invalid attribute name',
3434
INVALID_ATTRIBUTE_VALUE = 'invalid attribute value',
3535
INVALID_CUSTOM_METRIC_NAME = 'invalid custom metric name',
36-
INVALID_STRING_MERGER_PARAMETER = 'invalid String merger input'
36+
INVALID_STRING_MERGER_PARAMETER = 'invalid String merger input',
37+
ALREADY_INITIALIZED = 'already initialized'
3738
}
3839

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

6466
interface ErrorParams {

packages-exp/performance-exp/src/utils/string_merger.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import { expect } from 'chai';
1919

2020
import { mergeStrings } from './string_merger';
2121
import { FirebaseError } from '@firebase/util';
22-
// import { ERROR_FACTORY, ErrorCode } from './errors';
2322
import '../../test/setup';
2423

2524
describe('Firebase Performance > string_merger', () => {
2625
describe('#mergeStrings', () => {
2726
it('Throws exception when string length has | diff | > 1', () => {
28-
// const expectedError = ERROR_FACTORY.create(ErrorCode.INVALID_STRING_MERGER_PARAMETER);
2927
expect(() => mergeStrings('', '123')).to.throw(
3028
FirebaseError,
3129
'performance/invalid String merger input'

packages/component/src/provider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ export class Provider<T extends Name> {
186186
return this.component != null;
187187
}
188188

189+
isInitialized(identifier: string = DEFAULT_ENTRY_NAME): boolean {
190+
return this.instances.has(identifier);
191+
}
192+
189193
private getOrInitializeService(
190194
identifier: string
191195
): NameServiceMapping[T] | null {

0 commit comments

Comments
 (0)