@@ -29,27 +29,18 @@ import {
29
29
onSubChange
30
30
} from '../listeners/sw-listeners' ;
31
31
32
+ import { GetTokenOptions } from '../interfaces/public-types' ;
33
+ import { MessagingInternal } from '@firebase/messaging-interop-types' ;
32
34
import { MessagingService } from '../messaging-service' ;
33
35
import { ServiceWorkerGlobalScope } from '../util/sw-types' ;
34
36
import { _registerComponent } from '@firebase/app-exp' ;
37
+ import { getToken } from '../api/getToken' ;
35
38
import { messageEventListener } from '../listeners/window-listener' ;
36
39
37
40
const WindowMessagingFactory : InstanceFactory < 'messaging-exp' > = (
38
41
container : ComponentContainer
39
42
) => {
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 ( ) ;
53
44
54
45
const messaging = new MessagingService (
55
46
container . getProvider ( 'app-exp' ) . getImmediate ( ) ,
@@ -64,6 +55,38 @@ const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
64
55
return messaging ;
65
56
} ;
66
57
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
+
67
90
declare const self : ServiceWorkerGlobalScope ;
68
91
const SwMessagingFactory : InstanceFactory < 'messaging-exp' > = (
69
92
container : ComponentContainer
@@ -105,6 +128,14 @@ export function registerMessagingInWindow(): void {
105
128
_registerComponent (
106
129
new Component ( 'messaging-exp' , WindowMessagingFactory , ComponentType . PUBLIC )
107
130
) ;
131
+
132
+ _registerComponent (
133
+ new Component (
134
+ 'messaging-internal' ,
135
+ WindowMessagingInternalFactory ,
136
+ ComponentType . PRIVATE
137
+ )
138
+ ) ;
108
139
}
109
140
110
141
/**
0 commit comments