Skip to content

Commit 98b4115

Browse files
zwu52hsubox76
authored andcommitted
Create FCM Interop for v9 Function (#5336)
1 parent 772d874 commit 98b4115

File tree

7 files changed

+117
-13
lines changed

7 files changed

+117
-13
lines changed

packages-exp/messaging-exp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dependencies": {
3939
"@firebase/component": "0.5.6",
4040
"@firebase/installations-exp": "0.0.900",
41+
"@firebase/messaging-interop-types": "0.0.1",
4142
"@firebase/util": "1.3.0",
4243
"idb": "3.0.2",
4344
"tslib": "^2.1.0"

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,18 @@ import {
2929
onSubChange
3030
} from '../listeners/sw-listeners';
3131

32+
import { GetTokenOptions } from '../interfaces/public-types';
33+
import { MessagingInternal } from '@firebase/messaging-interop-types';
3234
import { MessagingService } from '../messaging-service';
3335
import { ServiceWorkerGlobalScope } from '../util/sw-types';
3436
import { _registerComponent } from '@firebase/app-exp';
37+
import { getToken } from '../api/getToken';
3538
import { messageEventListener } from '../listeners/window-listener';
3639

3740
const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
3841
container: ComponentContainer
3942
) => {
40-
// Conscious decision to make this async check non-blocking during the messaging instance
41-
// initialization phase for performance consideration. An error would be thrown latter for
42-
// developer's information. Developers can then choose to import and call `isSupported` for
43-
// special handling.
44-
isWindowSupported()
45-
.then(isSupported => {
46-
if (!isSupported) {
47-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
48-
}
49-
})
50-
.catch(_ => {
51-
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
52-
});
43+
maybeThrowWindowError();
5344

5445
const messaging = new MessagingService(
5546
container.getProvider('app-exp').getImmediate(),
@@ -64,6 +55,38 @@ const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
6455
return messaging;
6556
};
6657

58+
const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
59+
container: ComponentContainer
60+
) => {
61+
maybeThrowWindowError();
62+
63+
const messaging = container
64+
.getProvider('messaging-exp')
65+
.getImmediate() as MessagingService;
66+
67+
const messagingInternal: MessagingInternal = {
68+
getToken: (options?: GetTokenOptions) => getToken(messaging, options)
69+
};
70+
71+
return messagingInternal;
72+
};
73+
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+
6790
declare const self: ServiceWorkerGlobalScope;
6891
const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
6992
container: ComponentContainer
@@ -105,6 +128,14 @@ export function registerMessagingInWindow(): void {
105128
_registerComponent(
106129
new Component('messaging-exp', WindowMessagingFactory, ComponentType.PUBLIC)
107130
);
131+
132+
_registerComponent(
133+
new Component(
134+
'messaging-internal',
135+
WindowMessagingInternalFactory,
136+
ComponentType.PRIVATE
137+
)
138+
);
108139
}
109140

110141
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @firebase/messaging-interop-types
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# @firebase/messaging-interop-types
2+
3+
**This package is not intended for direct usage, and should only be used via the officially
4+
supported [firebase](https://www.npmjs.com/package/firebase) package.**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2021 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+
export interface MessagingInternal {
19+
getToken(options?: GetTokenOptions): Promise<string>;
20+
}
21+
22+
interface GetTokenOptions {
23+
vapidKey?: string;
24+
serviceWorkerRegistration?: ServiceWorkerRegistration;
25+
}
26+
27+
export type MessagingInternalComponentName = 'messaging-internal';
28+
29+
declare module '@firebase/component' {
30+
interface NameServiceMapping {
31+
'messaging-internal': MessagingInternal;
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@firebase/messaging-interop-types",
3+
"version": "0.0.1",
4+
"description": "@firebase/messaging-interop-types Types",
5+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
6+
"license": "Apache-2.0",
7+
"scripts": {
8+
"test": "tsc",
9+
"test:ci": "node ../../scripts/run_tests_in_ci.js"
10+
},
11+
"files": [
12+
"index.d.ts"
13+
],
14+
"repository": {
15+
"directory": "packages/messaging-interop-types",
16+
"type": "git",
17+
"url": "https://github.com/firebase/firebase-js-sdk.git"
18+
},
19+
"bugs": {
20+
"url": "https://github.com/firebase/firebase-js-sdk/issues"
21+
},
22+
"devDependencies": {
23+
"typescript": "4.2.2"
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../config/tsconfig.base.json",
3+
"compilerOptions": {
4+
"noEmit": true
5+
},
6+
"exclude": [
7+
"dist/**/*"
8+
]
9+
}

0 commit comments

Comments
 (0)