Skip to content

Commit 1d934d4

Browse files
committed
Separate window/sw context check
Deprecating #4446 in favor of this PR
1 parent 65a4e85 commit 1d934d4

File tree

6 files changed

+63
-31
lines changed

6 files changed

+63
-31
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@
1515
* limitations under the License.
1616
*/
1717

18-
import {
19-
FirebaseMessaging,
20-
MessagePayload,
21-
NextFn,
22-
Observer,
23-
Unsubscribe
24-
} from './interfaces/public-types';
18+
import { FirebaseApp, _getProvider } from '@firebase/app-exp';
19+
import { FirebaseMessaging, MessagePayload } from './interfaces/public-types';
20+
import { NextFn, Observer, Unsubscribe } from '@firebase/util';
2521

2622
import { MessagingService } from './messaging-service';
2723
import { Provider } from '@firebase/component';
2824
import { deleteToken as _deleteToken } from './api/deleteToken';
29-
import { _getProvider, FirebaseApp } from '@firebase/app-exp';
3025
import { getToken as _getToken } from './api/getToken';
26+
import { isSupported as _isSupported } from './api/isSupported';
3127
import { onBackgroundMessage as _onBackgroundMessage } from './api/onBackgroundMessage';
3228
import { onMessage as _onMessage } from './api/onMessage';
3329
import { getModularInstance } from '@firebase/util';
@@ -131,7 +127,7 @@ export function onMessage(
131127
*
132128
* @returns To stop listening for messages execute this returned function
133129
*
134-
* make it internal to hide it from the browser entrypoint
130+
* make it internal to hide it from the browser entry point.
135131
* @internal
136132
*/
137133
export function onBackgroundMessage(
@@ -141,3 +137,10 @@ export function onBackgroundMessage(
141137
messaging = getModularInstance(messaging);
142138
return _onBackgroundMessage(messaging as MessagingService, nextOrObserver);
143139
}
140+
141+
/**
142+
* Checks if the current browser is supported.
143+
*/
144+
export async function isSupported(): Promise<boolean> {
145+
return _isSupported();
146+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { isSwSupported, isWindowSupported } from '../helpers/check-browser-env';
19+
20+
export async function isSupported(): Promise<boolean> {
21+
if (self && 'ServiceWorkerGlobalScope' in self) {
22+
// Running in ServiceWorker context
23+
return isWindowSupported();
24+
} else {
25+
// Assume we are in the window context.
26+
return isSwSupported();
27+
}
28+
}

packages-exp/messaging-exp/src/helpers/isSupported.ts renamed to packages-exp/messaging-exp/src/helpers/check-browser-env.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@
1515
* limitations under the License.
1616
*/
1717

18-
export function isSupported(): boolean {
19-
if (self && 'ServiceWorkerGlobalScope' in self) {
20-
// Running in ServiceWorker context
21-
return isSWControllerSupported();
22-
} else {
23-
// Assume we are in the window context.
24-
return isWindowControllerSupported();
25-
}
26-
}
27-
2818
/**
2919
* Checks to see if the required APIs exist.
3020
*/
31-
function isWindowControllerSupported(): boolean {
21+
export function isWindowSupported(): boolean {
3222
return (
3323
'indexedDB' in window &&
3424
indexedDB !== null &&
@@ -45,7 +35,7 @@ function isWindowControllerSupported(): boolean {
4535
/**
4636
* Checks to see if the required APIs exist within SW Context.
4737
*/
48-
function isSWControllerSupported(): boolean {
38+
export function isSwSupported(): boolean {
4939
return (
5040
'indexedDB' in self &&
5141
indexedDB !== null &&

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ import {
2121
ComponentType,
2222
InstanceFactory
2323
} from '@firebase/component';
24-
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
2524

2625
import { MessagingService } from '../messaging-service';
2726
import { _registerComponent } from '@firebase/app-exp';
28-
import { isSupported } from './isSupported';
2927

3028
const messagingFactory: InstanceFactory<'messaging-exp'> = (
3129
container: ComponentContainer
3230
) => {
33-
if (!isSupported()) {
34-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
35-
}
36-
3731
return new MessagingService(
3832
container.getProvider('app-exp').getImmediate(),
3933
container.getProvider('installations-exp-internal').getImmediate(),

packages-exp/messaging-exp/src/index.sw.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,24 @@
1515
* limitations under the License.
1616
*/
1717

18+
import '@firebase/installations-exp';
19+
20+
import { ERROR_FACTORY, ErrorCode } from './util/errors';
21+
1822
import { FirebaseMessaging } from './interfaces/public-types';
23+
import { isSwSupported } from './helpers/check-browser-env';
1924
import { registerMessaging } from './helpers/register';
20-
import '@firebase/installations-exp';
2125

22-
export { onBackgroundMessage, getMessaging } from './api';
26+
export { onBackgroundMessage, getMessaging, isSupported } from './api';
2327

2428
declare module '@firebase/component' {
2529
interface NameServiceMapping {
2630
'messaging-exp': FirebaseMessaging;
2731
}
2832
}
2933

34+
if (!isSwSupported()) {
35+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
36+
}
37+
3038
registerMessaging();

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@
1515
* limitations under the License.
1616
*/
1717

18+
import '@firebase/installations-exp';
19+
20+
import { ERROR_FACTORY, ErrorCode } from './util/errors';
21+
1822
import { FirebaseMessaging } from './interfaces/public-types';
23+
import { isWindowSupported } from './helpers/check-browser-env';
1924
import { registerMessaging } from './helpers/register';
20-
import '@firebase/installations-exp';
2125

2226
export {
2327
getToken,
2428
deleteToken,
2529
onMessage,
2630
getMessaging,
27-
onBackgroundMessage
31+
onBackgroundMessage,
32+
isSupported
2833
} from './api';
2934
export * from './interfaces/public-types';
3035

@@ -34,4 +39,8 @@ declare module '@firebase/component' {
3439
}
3540
}
3641

42+
if (!isWindowSupported()) {
43+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
44+
}
45+
3746
registerMessaging();

0 commit comments

Comments
 (0)