Skip to content

Commit 0b95a51

Browse files
committed
Create registerMessagingCompat.ts
1 parent a8bf697 commit 0b95a51

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 {
19+
Component,
20+
ComponentContainer,
21+
ComponentType,
22+
InstanceFactory
23+
} from '@firebase/component';
24+
import firebase, { _FirebaseNamespace } from '@firebase/app-compat';
25+
26+
import { MessagingCompatImpl } from './messaging-compat';
27+
28+
declare module '@firebase/component' {
29+
interface NameServiceMapping {
30+
'messaging-compat': MessagingCompatImpl;
31+
}
32+
}
33+
34+
const messagingCompatFactory: InstanceFactory<'messaging-compat'> = (
35+
container: ComponentContainer
36+
) => {
37+
if (self && 'ServiceWorkerGlobalScope' in self) {
38+
// in sw
39+
return new MessagingCompatImpl(
40+
container.getProvider('app-compat').getImmediate(),
41+
container.getProvider('messaging-sw-exp').getImmediate()
42+
);
43+
} else {
44+
// in window
45+
return new MessagingCompatImpl(
46+
container.getProvider('app-compat').getImmediate(),
47+
container.getProvider('messaging-exp').getImmediate()
48+
);
49+
}
50+
};
51+
52+
export function registerMessagingCompat(): void {
53+
(firebase as _FirebaseNamespace).INTERNAL.registerComponent(
54+
new Component(
55+
'messaging-compat',
56+
messagingCompatFactory,
57+
ComponentType.PUBLIC
58+
)
59+
);
60+
}

0 commit comments

Comments
 (0)