Skip to content

Commit 19c0c01

Browse files
authored
Implement interop API for Analytics, Perf, RC and FCM (#4700)
* implement interop for analytics-exp * implement interop API for messaging-exp * implement interop API for perf-exp * implement interop API for rc-exp
1 parent 3aa0b36 commit 19c0c01

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

packages-exp/analytics-exp/src/api.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import {
3030
isIndexedDBAvailable,
3131
validateIndexedDBOpenable,
3232
areCookiesEnabled,
33-
isBrowserExtension
33+
isBrowserExtension,
34+
getModularInstance
3435
} from '@firebase/util';
3536
import { ANALYTICS_TYPE } from './constants';
3637
import {
@@ -63,6 +64,7 @@ declare module '@firebase/component' {
6364
* @param app - The FirebaseApp to use.
6465
*/
6566
export function getAnalytics(app: FirebaseApp): Analytics {
67+
app = getModularInstance(app);
6668
// Dependencies
6769
const analyticsProvider: Provider<'analytics-exp'> = _getProvider(
6870
app,
@@ -115,6 +117,7 @@ export function setCurrentScreen(
115117
screenName: string,
116118
options?: AnalyticsCallOptions
117119
): void {
120+
analyticsInstance = getModularInstance(analyticsInstance);
118121
internalSetCurrentScreen(
119122
wrappedGtagFunction,
120123
initializationPromisesMap[analyticsInstance.app.options.appId!],
@@ -136,6 +139,7 @@ export function setUserId(
136139
id: string,
137140
options?: AnalyticsCallOptions
138141
): void {
142+
analyticsInstance = getModularInstance(analyticsInstance);
139143
internalSetUserId(
140144
wrappedGtagFunction,
141145
initializationPromisesMap[analyticsInstance.app.options.appId!],
@@ -154,6 +158,7 @@ export function setUserProperties(
154158
properties: CustomParams,
155159
options?: AnalyticsCallOptions
156160
): void {
161+
analyticsInstance = getModularInstance(analyticsInstance);
157162
internalSetUserProperties(
158163
wrappedGtagFunction,
159164
initializationPromisesMap[analyticsInstance.app.options.appId!],
@@ -175,6 +180,7 @@ export function setAnalyticsCollectionEnabled(
175180
analyticsInstance: Analytics,
176181
enabled: boolean
177182
): void {
183+
analyticsInstance = getModularInstance(analyticsInstance);
178184
internalSetAnalyticsCollectionEnabled(
179185
initializationPromisesMap[analyticsInstance.app.options.appId!],
180186
enabled
@@ -663,6 +669,7 @@ export function logEvent(
663669
eventParams?: EventParams,
664670
options?: AnalyticsCallOptions
665671
): void {
672+
analyticsInstance = getModularInstance(analyticsInstance);
666673
internalLogEvent(
667674
wrappedGtagFunction,
668675
initializationPromisesMap[analyticsInstance.app.options.appId!],

packages-exp/messaging-exp/src/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { _getProvider, FirebaseApp } from '@firebase/app-exp';
3030
import { getToken as _getToken } from './api/getToken';
3131
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
3232
import { onMessage as _onMessage } from './api/onMessage';
33+
import { getModularInstance } from '@firebase/util';
3334

3435
/**
3536
* Retrieves a firebase messaging instance.
@@ -39,6 +40,7 @@ import { onMessage as _onMessage } from './api/onMessage';
3940
* @public
4041
*/
4142
export function getMessaging(app: FirebaseApp): FirebaseMessaging {
43+
app = getModularInstance(app);
4244
const messagingProvider: Provider<'messaging-exp'> = _getProvider(
4345
app,
4446
'messaging-exp'
@@ -79,6 +81,7 @@ export async function getToken(
7981
messaging: FirebaseMessaging,
8082
options?: { vapidKey?: string; swReg?: ServiceWorkerRegistration }
8183
): Promise<string> {
84+
messaging = getModularInstance(messaging);
8285
return _getToken(messaging as MessagingService, options);
8386
}
8487

@@ -93,6 +96,7 @@ export async function getToken(
9396
* @public
9497
*/
9598
export function deleteToken(messaging: FirebaseMessaging): Promise<boolean> {
99+
messaging = getModularInstance(messaging);
96100
return _deleteToken(messaging as MessagingService);
97101
}
98102

@@ -113,6 +117,7 @@ export function onMessage(
113117
messaging: FirebaseMessaging,
114118
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
115119
): Unsubscribe {
120+
messaging = getModularInstance(messaging);
116121
return _onMessage(messaging as MessagingService, nextOrObserver);
117122
}
118123

@@ -133,5 +138,6 @@ export function onBackgroundMessage(
133138
messaging: FirebaseMessaging,
134139
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
135140
): Unsubscribe {
141+
messaging = getModularInstance(messaging);
136142
return _onBackgroundMessage(messaging as MessagingService, nextOrObserver);
137143
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
import { name, version } from '../package.json';
3939
import { Trace } from './resources/trace';
4040
import '@firebase/installations-exp';
41+
import { getModularInstance } from '@firebase/util';
4142

4243
const DEFAULT_ENTRY_NAME = '[DEFAULT]';
4344

@@ -47,6 +48,7 @@ const DEFAULT_ENTRY_NAME = '[DEFAULT]';
4748
* @public
4849
*/
4950
export function getPerformance(app: FirebaseApp): FirebasePerformance {
51+
app = getModularInstance(app);
5052
const provider = _getProvider(app, 'performance-exp');
5153
const perfInstance = provider.getImmediate() as PerformanceController;
5254
return perfInstance;
@@ -62,6 +64,7 @@ export function initializePerformance(
6264
app: FirebaseApp,
6365
settings?: PerformanceSettings
6466
): FirebasePerformance {
67+
app = getModularInstance(app);
6568
const provider = _getProvider(app, 'performance-exp');
6669

6770
// throw if an instance was already created.
@@ -86,6 +89,7 @@ export function trace(
8689
performance: FirebasePerformance,
8790
name: string
8891
): PerformanceTrace {
92+
performance = getModularInstance(performance);
8993
return new Trace(performance as PerformanceController, name);
9094
}
9195

packages-exp/remote-config-exp/src/api.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { ErrorCode, hasErrorCode } from './errors';
2727
import { RemoteConfig as RemoteConfigImpl } from './remote_config';
2828
import { Value as ValueImpl } from './value';
2929
import { LogLevel as FirebaseLogLevel } from '@firebase/logger';
30+
import { getModularInstance } from '@firebase/util';
3031

3132
/**
3233
*
@@ -36,6 +37,7 @@ import { LogLevel as FirebaseLogLevel } from '@firebase/logger';
3637
* @public
3738
*/
3839
export function getRemoteConfig(app: FirebaseApp): RemoteConfig {
40+
app = getModularInstance(app);
3941
const rcProvider = _getProvider(app, RC_COMPONENT_NAME);
4042
return rcProvider.getImmediate();
4143
}
@@ -49,7 +51,7 @@ export function getRemoteConfig(app: FirebaseApp): RemoteConfig {
4951
* @public
5052
*/
5153
export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
52-
const rc = remoteConfig as RemoteConfigImpl;
54+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
5355
const [lastSuccessfulFetchResponse, activeConfigEtag] = await Promise.all([
5456
rc._storage.getLastSuccessfulFetchResponse(),
5557
rc._storage.getActiveConfigEtag()
@@ -79,7 +81,7 @@ export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
7981
* @public
8082
*/
8183
export function ensureInitialized(remoteConfig: RemoteConfig): Promise<void> {
82-
const rc = remoteConfig as RemoteConfigImpl;
84+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
8385
if (!rc._initializePromise) {
8486
rc._initializePromise = rc._storageCache.loadFromStorage().then(() => {
8587
rc._isInitializationComplete = true;
@@ -94,7 +96,7 @@ export function ensureInitialized(remoteConfig: RemoteConfig): Promise<void> {
9496
* @public
9597
*/
9698
export async function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
97-
const rc = remoteConfig as RemoteConfigImpl;
99+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
98100
// Aborts the request after the given timeout, causing the fetch call to
99101
// reject with an AbortError.
100102
//
@@ -138,7 +140,7 @@ export async function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
138140
* @public
139141
*/
140142
export function getAll(remoteConfig: RemoteConfig): Record<string, Value> {
141-
const rc = remoteConfig as RemoteConfigImpl;
143+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
142144
return getAllKeys(
143145
rc._storageCache.getActiveConfig(),
144146
rc.defaultConfig
@@ -160,7 +162,7 @@ export function getAll(remoteConfig: RemoteConfig): Record<string, Value> {
160162
* @public
161163
*/
162164
export function getBoolean(remoteConfig: RemoteConfig, key: string): boolean {
163-
return getValue(remoteConfig, key).asBoolean();
165+
return getValue(getModularInstance(remoteConfig), key).asBoolean();
164166
}
165167

166168
/**
@@ -176,7 +178,7 @@ export function getBoolean(remoteConfig: RemoteConfig, key: string): boolean {
176178
* @public
177179
*/
178180
export function getNumber(remoteConfig: RemoteConfig, key: string): number {
179-
return getValue(remoteConfig, key).asNumber();
181+
return getValue(getModularInstance(remoteConfig), key).asNumber();
180182
}
181183

182184
/**
@@ -191,7 +193,7 @@ export function getNumber(remoteConfig: RemoteConfig, key: string): number {
191193
* @public
192194
*/
193195
export function getString(remoteConfig: RemoteConfig, key: string): string {
194-
return getValue(remoteConfig, key).asString();
196+
return getValue(getModularInstance(remoteConfig), key).asString();
195197
}
196198

197199
/**
@@ -205,7 +207,7 @@ export function getString(remoteConfig: RemoteConfig, key: string): string {
205207
* @public
206208
*/
207209
export function getValue(remoteConfig: RemoteConfig, key: string): Value {
208-
const rc = remoteConfig as RemoteConfigImpl;
210+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
209211
if (!rc._isInitializationComplete) {
210212
rc._logger.debug(
211213
`A value was requested for key "${key}" before SDK initialization completed.` +
@@ -237,7 +239,7 @@ export function setLogLevel(
237239
remoteConfig: RemoteConfig,
238240
logLevel: RemoteConfigLogLevel
239241
): void {
240-
const rc = remoteConfig as RemoteConfigImpl;
242+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
241243
switch (logLevel) {
242244
case 'debug':
243245
rc._logger.logLevel = FirebaseLogLevel.DEBUG;

packages-exp/remote-config-exp/src/api2.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { RemoteConfig } from './public_types';
1919
import { activate, fetchConfig } from './api';
20+
import { getModularInstance } from '@firebase/util';
2021

2122
// This API is put in a separate file, so we can stub fetchConfig and activate in tests.
2223
// It's not possible to stub standalone functions from the same module.
@@ -34,6 +35,7 @@ import { activate, fetchConfig } from './api';
3435
export async function fetchAndActivate(
3536
remoteConfig: RemoteConfig
3637
): Promise<boolean> {
38+
remoteConfig = getModularInstance(remoteConfig);
3739
await fetchConfig(remoteConfig);
3840
return activate(remoteConfig);
3941
}

0 commit comments

Comments
 (0)