Skip to content

Move Messaging env checks to public methods #5357

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 3 commits into from
Aug 24, 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
39 changes: 35 additions & 4 deletions packages/messaging/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import {
import { MessagingService } from './messaging-service';
import { deleteToken as _deleteToken } from './api/deleteToken';
import { getToken as _getToken } from './api/getToken';
import { isSwSupported, isWindowSupported } from './api/isSupported';
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
import { onMessage as _onMessage } from './api/onMessage';
import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryMetricsExportedToBigQueryEnabled';
import { ERROR_FACTORY, ErrorCode } from './util/errors';

/**
* Retrieves a Firebase Cloud Messaging instance.
Expand All @@ -43,6 +45,22 @@ import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryM
* @public
*/
export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging {
// Conscious decision to make this async check non-blocking during the messaging instance
// initialization phase for performance consideration. An error would be thrown latter for
// developer's information. Developers can then choose to import and call `isSupported` for
// special handling.
isWindowSupported().then(
isSupported => {
// If `isWindowSupported()` resolved, but returned false.
if (!isSupported) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}
},
_ => {
// If `isWindowSupported()` rejected.
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
}
);
return _getProvider(getModularInstance(app), 'messaging').getImmediate();
}

Expand All @@ -54,10 +72,23 @@ export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging {
* @public
*/
export function getMessagingInSw(app: FirebaseApp = getApp()): Messaging {
return _getProvider(
getModularInstance(app),
'messaging-sw'
).getImmediate();
// Conscious decision to make this async check non-blocking during the messaging instance
// initialization phase for performance consideration. An error would be thrown latter for
// developer's information. Developers can then choose to import and call `isSupported` for
// special handling.
isSwSupported().then(
isSupported => {
// If `isSwSupported()` resolved, but returned false.
if (!isSupported) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}
},
_ => {
// If `isSwSupported()` rejected.
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
}
);
return _getProvider(getModularInstance(app), 'messaging-sw').getImmediate();
}

/**
Expand Down
36 changes: 0 additions & 36 deletions packages/messaging/src/helpers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
ComponentType,
InstanceFactory
} from '@firebase/component';
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
import { isSwSupported, isWindowSupported } from '../api/isSupported';
import {
onNotificationClick,
onPush,
Expand All @@ -40,8 +38,6 @@ import { messageEventListener } from '../listeners/window-listener';
const WindowMessagingFactory: InstanceFactory<'messaging'> = (
container: ComponentContainer
) => {
maybeThrowWindowError();

const messaging = new MessagingService(
container.getProvider('app').getImmediate(),
container.getProvider('installations-internal').getImmediate(),
Expand All @@ -58,8 +54,6 @@ const WindowMessagingFactory: InstanceFactory<'messaging'> = (
const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
container: ComponentContainer
) => {
maybeThrowWindowError();

const messaging = container
.getProvider('messaging')
.getImmediate() as MessagingService;
Expand All @@ -71,40 +65,10 @@ const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
return messagingInternal;
};

function maybeThrowWindowError(): void {
// Conscious decision to make this async check non-blocking during the messaging instance
// initialization phase for performance consideration. An error would be thrown latter for
// developer's information. Developers can then choose to import and call `isSupported` for
// special handling.
isWindowSupported()
.then(isSupported => {
if (!isSupported) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}
})
.catch(_ => {
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
});
}

declare const self: ServiceWorkerGlobalScope;
const SwMessagingFactory: InstanceFactory<'messaging'> = (
container: ComponentContainer
) => {
// Conscious decision to make this async check non-blocking during the messaging instance
// initialization phase for performance consideration. An error would be thrown latter for
// developer's information. Developers can then choose to import and call `isSupported` for
// special handling.
isSwSupported()
.then(isSupported => {
if (!isSupported) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}
})
.catch(_ => {
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
});

const messaging = new MessagingService(
container.getProvider('app').getImmediate(),
container.getProvider('installations-internal').getImmediate(),
Expand Down