Skip to content

Commit d148196

Browse files
committed
Add initializePerformance()
1 parent f513922 commit d148196

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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/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)