1
1
import { Injectable , Inject , Optional , NgZone , PLATFORM_ID } from '@angular/core' ;
2
2
import { isPlatformBrowser } from '@angular/common' ;
3
3
import { messaging } from 'firebase' ;
4
- import { requestPermission } from './observable/request-permission' ;
5
4
import { Observable , empty , from , of , throwError } from 'rxjs' ;
6
- import { mergeMap , catchError } from 'rxjs/operators' ;
7
- import { FirebaseOptions , FirebaseAppConfig } from 'angularfire2' ;
5
+ import { mergeMap , catchError , map , switchMap , concat , defaultIfEmpty } from 'rxjs/operators' ;
6
+ import { FirebaseOptions , FirebaseAppConfig , runOutsideAngular } from 'angularfire2' ;
8
7
import { FirebaseOptionsToken , FirebaseNameOrConfigToken , _firebaseAppFactory , FirebaseZoneScheduler } from 'angularfire2' ;
9
8
10
9
@Injectable ( )
11
10
export class AngularFireMessaging {
12
- messaging : messaging . Messaging ;
11
+ messaging : Observable < messaging . Messaging > ;
13
12
requestPermission : Observable < void > ;
14
13
getToken : Observable < string | null > ;
15
14
tokenChanges : Observable < string | null > ;
@@ -23,52 +22,58 @@ export class AngularFireMessaging {
23
22
@Inject ( PLATFORM_ID ) platformId : Object ,
24
23
zone : NgZone
25
24
) {
26
- const scheduler = new FirebaseZoneScheduler ( zone , platformId ) ;
27
- this . messaging = zone . runOutsideAngular ( ( ) => {
28
- const app = _firebaseAppFactory ( options , nameOrConfig ) ;
29
- return app . messaging ( ) ;
30
- } ) ;
31
25
32
26
if ( isPlatformBrowser ( platformId ) ) {
33
27
34
- this . requestPermission = scheduler . runOutsideAngular (
35
- requestPermission ( this . messaging )
36
- ) ;
37
-
38
- this . getToken = scheduler . runOutsideAngular (
39
- from ( this . messaging . getToken ( ) )
40
- ) ;
41
-
42
- this . tokenChanges = scheduler . runOutsideAngular (
43
- new Observable ( subscriber => {
44
- this . messaging . getToken ( ) . then ( t => subscriber . next ( t ) ) ;
45
- this . messaging . onTokenRefresh ( subscriber . next ) ;
46
- } )
47
- ) ;
28
+ const requireMessaging = from ( require ( 'firebase/messaging' ) ) ;
48
29
49
- this . messages = scheduler . runOutsideAngular (
50
- new Observable ( subscriber => {
51
- this . messaging . onMessage ( subscriber . next ) ;
52
- } )
30
+ this . messaging = requireMessaging . pipe (
31
+ map ( ( ) => _firebaseAppFactory ( options , nameOrConfig ) ) ,
32
+ map ( app => app . messaging ( ) ) ,
33
+ runOutsideAngular ( zone )
53
34
) ;
54
35
55
- this . requestToken = this . requestPermission . pipe (
56
- catchError ( ( ) => of ( null ) ) ,
57
- mergeMap ( ( ) => this . tokenChanges ) ,
36
+ this . requestPermission = this . messaging . pipe (
37
+ switchMap ( messaging => messaging . requestPermission ( ) ) ,
38
+ runOutsideAngular ( zone )
58
39
) ;
59
40
60
41
} else {
61
42
43
+ this . messaging = empty ( ) ;
62
44
this . requestPermission = throwError ( 'Not available on server platform.' ) ;
63
- this . getToken = of ( null ) ;
64
- this . tokenChanges = of ( null ) ;
65
- this . messages = empty ( ) ;
66
- this . requestToken = of ( null ) ;
67
45
68
46
}
69
47
70
- this . deleteToken = ( token : string ) => scheduler . runOutsideAngular (
71
- from ( this . messaging . deleteToken ( token ) )
48
+ this . getToken = this . messaging . pipe (
49
+ switchMap ( messaging => messaging . getToken ( ) ) ,
50
+ defaultIfEmpty ( null ) ,
51
+ runOutsideAngular ( zone )
52
+ ) ;
53
+
54
+ const tokenChanges = this . messaging . pipe (
55
+ switchMap ( messaging => new Observable ( messaging . onTokenRefresh ) ) ,
56
+ runOutsideAngular ( zone )
57
+ ) ;
58
+
59
+ this . tokenChanges = this . getToken . pipe (
60
+ concat ( tokenChanges )
61
+ ) ;
62
+
63
+ this . messages = this . messaging . pipe (
64
+ switchMap ( messaging => new Observable ( messaging . onMessage ) ) ,
65
+ runOutsideAngular ( zone )
66
+ ) ;
67
+
68
+ this . requestToken = this . requestPermission . pipe (
69
+ catchError ( ( ) => of ( null ) ) ,
70
+ mergeMap ( ( ) => this . tokenChanges )
71
+ ) ;
72
+
73
+ this . deleteToken = ( token : string ) => this . messaging . pipe (
74
+ switchMap ( messaging => messaging . deleteToken ( token ) ) ,
75
+ defaultIfEmpty ( false ) ,
76
+ runOutsideAngular ( zone )
72
77
) ;
73
78
}
74
79
0 commit comments