Skip to content

Commit 3476288

Browse files
committed
Separate window/sw context check
Deprecating #4446 in favor of this PR
1 parent 4c02056 commit 3476288

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { MessagingService } from './messaging-service';
2323
import { Provider } from '@firebase/component';
2424
import { deleteToken as _deleteToken } from './api/deleteToken';
2525
import { getToken as _getToken } from './api/getToken';
26+
import { isSupported as _isSupported } from './api/isSupported';
2627
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
2728
import { onMessage as _onMessage } from './api/onMessage';
2829
import { getModularInstance } from '@firebase/util';
@@ -136,3 +137,10 @@ export function onBackgroundMessage(
136137
messaging = getModularInstance(messaging);
137138
return _onBackgroundMessage(messaging as MessagingService, nextOrObserver);
138139
}
140+
141+
/**
142+
* Checks if the current browser is supported.
143+
*/
144+
export async function isSupported(): Promise<boolean> {
145+
return _isSupported();
146+
}

packages-exp/messaging-exp/src/helpers/register.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,50 @@ import { isSwSupported, isWindowSupported } from '../api/isSupported';
2727
import { MessagingService } from '../messaging-service';
2828
import { _registerComponent } from '@firebase/app-exp';
2929

30-
const messagingFactory: InstanceFactory<'messaging-exp'> = (
30+
const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
3131
container: ComponentContainer
3232
) => {
33+
// Top-level 'await' requires 'module' option set to 'esnext' or 'system', and 'target' option
34+
// set to 'es2017' or higher. For compatibility, use async expression here.
35+
void (async () => {
36+
if (!(await isWindowSupported())) {
37+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
38+
}
39+
})();
40+
3341
return new MessagingService(
3442
container.getProvider('app-exp').getImmediate(),
3543
container.getProvider('installations-exp-internal').getImmediate(),
3644
container.getProvider('analytics-internal')
3745
);
3846
};
3947

40-
export function registerWindowMessaging(): void {
48+
const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
49+
container: ComponentContainer
50+
) => {
4151
// Top-level 'await' requires 'module' option set to 'esnext' or 'system', and 'target' option
4252
// set to 'es2017' or higher. For compatibility, use async expression here.
4353
void (async () => {
44-
if (!(await isWindowSupported())) {
54+
if (!(await isSwSupported())) {
4555
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
4656
}
4757
})();
4858

59+
return new MessagingService(
60+
container.getProvider('app-exp').getImmediate(),
61+
container.getProvider('installations-exp-internal').getImmediate(),
62+
container.getProvider('analytics-internal')
63+
);
64+
};
65+
66+
export function registerWindowMessaging(): void {
4967
_registerComponent(
50-
new Component('messaging-exp', messagingFactory, ComponentType.PUBLIC)
68+
new Component('messaging-exp', WindowMessagingFactory, ComponentType.PUBLIC)
5169
);
5270
}
5371

5472
export function registerSwMessaging(): void {
55-
void (async () => {
56-
if (!(await isSwSupported())) {
57-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
58-
}
59-
})();
60-
6173
_registerComponent(
62-
new Component('messaging-exp', messagingFactory, ComponentType.PUBLIC)
74+
new Component('messaging-exp', SwMessagingFactory, ComponentType.PUBLIC)
6375
);
6476
}

0 commit comments

Comments
 (0)