@@ -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' ;
3234import { MessagingService } from '../messaging-service' ;
3335import { ServiceWorkerGlobalScope } from '../util/sw-types' ;
3436import { _registerComponent } from '@firebase/app-exp' ;
37+ import { getToken } from '../api/getToken' ;
3538import { messageEventListener } from '../listeners/window-listener' ;
3639
3740const 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+
6790declare const self : ServiceWorkerGlobalScope ;
6891const 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/**
0 commit comments