File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed
packages-exp/performance-exp/src Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -44,14 +44,32 @@ const DEFAULT_ENTRY_NAME = '[DEFAULT]';
44
44
/**
45
45
* Returns a FirebasePerformance instance for the given app.
46
46
* @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.
47
58
* @param settings - Optional settings for the Performance instance.
48
59
* @public
49
60
*/
50
- export function getPerformance (
61
+ export function initializePerformance (
51
62
app : FirebaseApp ,
52
63
settings ?: PerformanceSettings
53
64
) : FirebasePerformance {
54
65
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
+
55
73
const perfInstance = provider . getImmediate ( ) as PerformanceController ;
56
74
perfInstance . _init ( settings ) ;
57
75
return perfInstance ;
Original file line number Diff line number Diff line change @@ -33,7 +33,8 @@ export const enum ErrorCode {
33
33
INVALID_ATTRIBUTE_NAME = 'invalid attribute name' ,
34
34
INVALID_ATTRIBUTE_VALUE = 'invalid attribute value' ,
35
35
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'
37
38
}
38
39
39
40
const ERROR_DESCRIPTION_MAP : { readonly [ key in ErrorCode ] : string } = {
@@ -58,7 +59,8 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
58
59
[ ErrorCode . INVALID_CUSTOM_METRIC_NAME ] :
59
60
'Custom metric name {$customMetricName} is invalid' ,
60
61
[ 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.'
62
64
} ;
63
65
64
66
interface ErrorParams {
Original file line number Diff line number Diff line change @@ -186,6 +186,10 @@ export class Provider<T extends Name> {
186
186
return this . component != null ;
187
187
}
188
188
189
+ isInitialized ( identifier : string = DEFAULT_ENTRY_NAME ) : boolean {
190
+ return this . instances . has ( identifier ) ;
191
+ }
192
+
189
193
private getOrInitializeService (
190
194
identifier : string
191
195
) : NameServiceMapping [ T ] | null {
You can’t perform that action at this time.
0 commit comments