Skip to content

Commit fe890a1

Browse files
authored
Move Messaging env checks to public methods (#5357)
* Move env checks to public methods * Simplify error handling * prettier
1 parent 7ff2f2b commit fe890a1

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

packages/messaging/src/api.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import {
3131
import { MessagingService } from './messaging-service';
3232
import { deleteToken as _deleteToken } from './api/deleteToken';
3333
import { getToken as _getToken } from './api/getToken';
34+
import { isSwSupported, isWindowSupported } from './api/isSupported';
3435
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
3536
import { onMessage as _onMessage } from './api/onMessage';
3637
import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryMetricsExportedToBigQueryEnabled';
38+
import { ERROR_FACTORY, ErrorCode } from './util/errors';
3739

3840
/**
3941
* Retrieves a Firebase Cloud Messaging instance.
@@ -43,6 +45,22 @@ import { _setDeliveryMetricsExportedToBigQueryEnabled } from './api/setDeliveryM
4345
* @public
4446
*/
4547
export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging {
48+
// Conscious decision to make this async check non-blocking during the messaging instance
49+
// initialization phase for performance consideration. An error would be thrown latter for
50+
// developer's information. Developers can then choose to import and call `isSupported` for
51+
// special handling.
52+
isWindowSupported().then(
53+
isSupported => {
54+
// If `isWindowSupported()` resolved, but returned false.
55+
if (!isSupported) {
56+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
57+
}
58+
},
59+
_ => {
60+
// If `isWindowSupported()` rejected.
61+
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
62+
}
63+
);
4664
return _getProvider(getModularInstance(app), 'messaging').getImmediate();
4765
}
4866

@@ -54,10 +72,23 @@ export function getMessagingInWindow(app: FirebaseApp = getApp()): Messaging {
5472
* @public
5573
*/
5674
export function getMessagingInSw(app: FirebaseApp = getApp()): Messaging {
57-
return _getProvider(
58-
getModularInstance(app),
59-
'messaging-sw'
60-
).getImmediate();
75+
// Conscious decision to make this async check non-blocking during the messaging instance
76+
// initialization phase for performance consideration. An error would be thrown latter for
77+
// developer's information. Developers can then choose to import and call `isSupported` for
78+
// special handling.
79+
isSwSupported().then(
80+
isSupported => {
81+
// If `isSwSupported()` resolved, but returned false.
82+
if (!isSupported) {
83+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
84+
}
85+
},
86+
_ => {
87+
// If `isSwSupported()` rejected.
88+
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
89+
}
90+
);
91+
return _getProvider(getModularInstance(app), 'messaging-sw').getImmediate();
6192
}
6293

6394
/**

packages/messaging/src/helpers/register.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import {
2121
ComponentType,
2222
InstanceFactory
2323
} from '@firebase/component';
24-
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
25-
import { isSwSupported, isWindowSupported } from '../api/isSupported';
2624
import {
2725
onNotificationClick,
2826
onPush,
@@ -40,8 +38,6 @@ import { messageEventListener } from '../listeners/window-listener';
4038
const WindowMessagingFactory: InstanceFactory<'messaging'> = (
4139
container: ComponentContainer
4240
) => {
43-
maybeThrowWindowError();
44-
4541
const messaging = new MessagingService(
4642
container.getProvider('app').getImmediate(),
4743
container.getProvider('installations-internal').getImmediate(),
@@ -58,8 +54,6 @@ const WindowMessagingFactory: InstanceFactory<'messaging'> = (
5854
const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
5955
container: ComponentContainer
6056
) => {
61-
maybeThrowWindowError();
62-
6357
const messaging = container
6458
.getProvider('messaging')
6559
.getImmediate() as MessagingService;
@@ -71,40 +65,10 @@ const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
7165
return messagingInternal;
7266
};
7367

74-
function maybeThrowWindowError(): void {
75-
// Conscious decision to make this async check non-blocking during the messaging instance
76-
// initialization phase for performance consideration. An error would be thrown latter for
77-
// developer's information. Developers can then choose to import and call `isSupported` for
78-
// special handling.
79-
isWindowSupported()
80-
.then(isSupported => {
81-
if (!isSupported) {
82-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
83-
}
84-
})
85-
.catch(_ => {
86-
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
87-
});
88-
}
89-
9068
declare const self: ServiceWorkerGlobalScope;
9169
const SwMessagingFactory: InstanceFactory<'messaging'> = (
9270
container: ComponentContainer
9371
) => {
94-
// Conscious decision to make this async check non-blocking during the messaging instance
95-
// initialization phase for performance consideration. An error would be thrown latter for
96-
// developer's information. Developers can then choose to import and call `isSupported` for
97-
// special handling.
98-
isSwSupported()
99-
.then(isSupported => {
100-
if (!isSupported) {
101-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
102-
}
103-
})
104-
.catch(_ => {
105-
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
106-
});
107-
10872
const messaging = new MessagingService(
10973
container.getProvider('app').getImmediate(),
11074
container.getProvider('installations-internal').getImmediate(),

0 commit comments

Comments
 (0)