Skip to content

Implement interop API for Analytics, Perf, RC and FCM #4700

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 4 commits into from
Mar 31, 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
9 changes: 8 additions & 1 deletion packages-exp/analytics-exp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
isIndexedDBAvailable,
validateIndexedDBOpenable,
areCookiesEnabled,
isBrowserExtension
isBrowserExtension,
getModularInstance
} from '@firebase/util';
import { ANALYTICS_TYPE } from './constants';
import {
Expand Down Expand Up @@ -63,6 +64,7 @@ declare module '@firebase/component' {
* @param app - The FirebaseApp to use.
*/
export function getAnalytics(app: FirebaseApp): Analytics {
app = getModularInstance(app);
// Dependencies
const analyticsProvider: Provider<'analytics-exp'> = _getProvider(
app,
Expand Down Expand Up @@ -115,6 +117,7 @@ export function setCurrentScreen(
screenName: string,
options?: AnalyticsCallOptions
): void {
analyticsInstance = getModularInstance(analyticsInstance);
internalSetCurrentScreen(
wrappedGtagFunction,
initializationPromisesMap[analyticsInstance.app.options.appId!],
Expand All @@ -136,6 +139,7 @@ export function setUserId(
id: string,
options?: AnalyticsCallOptions
): void {
analyticsInstance = getModularInstance(analyticsInstance);
internalSetUserId(
wrappedGtagFunction,
initializationPromisesMap[analyticsInstance.app.options.appId!],
Expand All @@ -154,6 +158,7 @@ export function setUserProperties(
properties: CustomParams,
options?: AnalyticsCallOptions
): void {
analyticsInstance = getModularInstance(analyticsInstance);
internalSetUserProperties(
wrappedGtagFunction,
initializationPromisesMap[analyticsInstance.app.options.appId!],
Expand All @@ -175,6 +180,7 @@ export function setAnalyticsCollectionEnabled(
analyticsInstance: Analytics,
enabled: boolean
): void {
analyticsInstance = getModularInstance(analyticsInstance);
internalSetAnalyticsCollectionEnabled(
initializationPromisesMap[analyticsInstance.app.options.appId!],
enabled
Expand Down Expand Up @@ -663,6 +669,7 @@ export function logEvent(
eventParams?: EventParams,
options?: AnalyticsCallOptions
): void {
analyticsInstance = getModularInstance(analyticsInstance);
internalLogEvent(
wrappedGtagFunction,
initializationPromisesMap[analyticsInstance.app.options.appId!],
Expand Down
6 changes: 6 additions & 0 deletions packages-exp/messaging-exp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { _getProvider, FirebaseApp } from '@firebase/app-exp';
import { getToken as _getToken } from './api/getToken';
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
import { onMessage as _onMessage } from './api/onMessage';
import { getModularInstance } from '@firebase/util';

/**
* Retrieves a firebase messaging instance.
Expand All @@ -39,6 +40,7 @@ import { onMessage as _onMessage } from './api/onMessage';
* @public
*/
export function getMessaging(app: FirebaseApp): FirebaseMessaging {
app = getModularInstance(app);
const messagingProvider: Provider<'messaging-exp'> = _getProvider(
app,
'messaging-exp'
Expand Down Expand Up @@ -79,6 +81,7 @@ export async function getToken(
messaging: FirebaseMessaging,
options?: { vapidKey?: string; swReg?: ServiceWorkerRegistration }
): Promise<string> {
messaging = getModularInstance(messaging);
return _getToken(messaging as MessagingService, options);
}

Expand All @@ -93,6 +96,7 @@ export async function getToken(
* @public
*/
export function deleteToken(messaging: FirebaseMessaging): Promise<boolean> {
messaging = getModularInstance(messaging);
return _deleteToken(messaging as MessagingService);
}

Expand All @@ -113,6 +117,7 @@ export function onMessage(
messaging: FirebaseMessaging,
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
): Unsubscribe {
messaging = getModularInstance(messaging);
return _onMessage(messaging as MessagingService, nextOrObserver);
}

Expand All @@ -133,5 +138,6 @@ export function onBackgroundMessage(
messaging: FirebaseMessaging,
nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>
): Unsubscribe {
messaging = getModularInstance(messaging);
return _onBackgroundMessage(messaging as MessagingService, nextOrObserver);
}
4 changes: 4 additions & 0 deletions packages-exp/performance-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import { name, version } from '../package.json';
import { Trace } from './resources/trace';
import '@firebase/installations-exp';
import { getModularInstance } from '@firebase/util';

const DEFAULT_ENTRY_NAME = '[DEFAULT]';

Expand All @@ -47,6 +48,7 @@ const DEFAULT_ENTRY_NAME = '[DEFAULT]';
* @public
*/
export function getPerformance(app: FirebaseApp): FirebasePerformance {
app = getModularInstance(app);
const provider = _getProvider(app, 'performance-exp');
const perfInstance = provider.getImmediate() as PerformanceController;
return perfInstance;
Expand All @@ -62,6 +64,7 @@ export function initializePerformance(
app: FirebaseApp,
settings?: PerformanceSettings
): FirebasePerformance {
app = getModularInstance(app);
const provider = _getProvider(app, 'performance-exp');

// throw if an instance was already created.
Expand All @@ -86,6 +89,7 @@ export function trace(
performance: FirebasePerformance,
name: string
): PerformanceTrace {
performance = getModularInstance(performance);
return new Trace(performance as PerformanceController, name);
}

Expand Down
20 changes: 11 additions & 9 deletions packages-exp/remote-config-exp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ErrorCode, hasErrorCode } from './errors';
import { RemoteConfig as RemoteConfigImpl } from './remote_config';
import { Value as ValueImpl } from './value';
import { LogLevel as FirebaseLogLevel } from '@firebase/logger';
import { getModularInstance } from '@firebase/util';

/**
*
Expand All @@ -36,6 +37,7 @@ import { LogLevel as FirebaseLogLevel } from '@firebase/logger';
* @public
*/
export function getRemoteConfig(app: FirebaseApp): RemoteConfig {
app = getModularInstance(app);
const rcProvider = _getProvider(app, RC_COMPONENT_NAME);
return rcProvider.getImmediate();
}
Expand All @@ -49,7 +51,7 @@ export function getRemoteConfig(app: FirebaseApp): RemoteConfig {
* @public
*/
export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
const rc = remoteConfig as RemoteConfigImpl;
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
const [lastSuccessfulFetchResponse, activeConfigEtag] = await Promise.all([
rc._storage.getLastSuccessfulFetchResponse(),
rc._storage.getActiveConfigEtag()
Expand Down Expand Up @@ -79,7 +81,7 @@ export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
* @public
*/
export function ensureInitialized(remoteConfig: RemoteConfig): Promise<void> {
const rc = remoteConfig as RemoteConfigImpl;
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
if (!rc._initializePromise) {
rc._initializePromise = rc._storageCache.loadFromStorage().then(() => {
rc._isInitializationComplete = true;
Expand All @@ -94,7 +96,7 @@ export function ensureInitialized(remoteConfig: RemoteConfig): Promise<void> {
* @public
*/
export async function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
const rc = remoteConfig as RemoteConfigImpl;
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
// Aborts the request after the given timeout, causing the fetch call to
// reject with an AbortError.
//
Expand Down Expand Up @@ -138,7 +140,7 @@ export async function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
* @public
*/
export function getAll(remoteConfig: RemoteConfig): Record<string, Value> {
const rc = remoteConfig as RemoteConfigImpl;
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
return getAllKeys(
rc._storageCache.getActiveConfig(),
rc.defaultConfig
Expand All @@ -160,7 +162,7 @@ export function getAll(remoteConfig: RemoteConfig): Record<string, Value> {
* @public
*/
export function getBoolean(remoteConfig: RemoteConfig, key: string): boolean {
return getValue(remoteConfig, key).asBoolean();
return getValue(getModularInstance(remoteConfig), key).asBoolean();
}

/**
Expand All @@ -176,7 +178,7 @@ export function getBoolean(remoteConfig: RemoteConfig, key: string): boolean {
* @public
*/
export function getNumber(remoteConfig: RemoteConfig, key: string): number {
return getValue(remoteConfig, key).asNumber();
return getValue(getModularInstance(remoteConfig), key).asNumber();
}

/**
Expand All @@ -191,7 +193,7 @@ export function getNumber(remoteConfig: RemoteConfig, key: string): number {
* @public
*/
export function getString(remoteConfig: RemoteConfig, key: string): string {
return getValue(remoteConfig, key).asString();
return getValue(getModularInstance(remoteConfig), key).asString();
}

/**
Expand All @@ -205,7 +207,7 @@ export function getString(remoteConfig: RemoteConfig, key: string): string {
* @public
*/
export function getValue(remoteConfig: RemoteConfig, key: string): Value {
const rc = remoteConfig as RemoteConfigImpl;
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
if (!rc._isInitializationComplete) {
rc._logger.debug(
`A value was requested for key "${key}" before SDK initialization completed.` +
Expand Down Expand Up @@ -237,7 +239,7 @@ export function setLogLevel(
remoteConfig: RemoteConfig,
logLevel: RemoteConfigLogLevel
): void {
const rc = remoteConfig as RemoteConfigImpl;
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
switch (logLevel) {
case 'debug':
rc._logger.logLevel = FirebaseLogLevel.DEBUG;
Expand Down
2 changes: 2 additions & 0 deletions packages-exp/remote-config-exp/src/api2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { RemoteConfig } from './public_types';
import { activate, fetchConfig } from './api';
import { getModularInstance } from '@firebase/util';

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