1
1
import { InjectionToken , NgZone } from '@angular/core' ;
2
- import { Subscription } from 'rxjs/Subscription' ;
3
2
import { Observable } from 'rxjs/Observable' ;
3
+ import { Subscription } from 'rxjs/Subscription' ;
4
4
import { queue } from 'rxjs/scheduler/queue' ;
5
+ import { isPlatformServer } from '@angular/common' ;
6
+ import { observeOn } from 'rxjs/operator/observeOn' ;
5
7
6
8
import firebase from '@firebase/app' ;
7
9
import { FirebaseApp , FirebaseOptions } from '@firebase/app-types' ;
8
10
9
11
import 'zone.js' ;
10
12
import 'rxjs/add/operator/first' ;
11
- import { Subscriber } from 'rxjs/Subscriber' ;
12
- import { observeOn } from 'rxjs/operator/observeOn' ;
13
13
14
14
export const FirebaseAppName = new InjectionToken < string > ( 'angularfire2.appName' ) ;
15
15
export const FirebaseAppConfig = new InjectionToken < FirebaseOptions > ( 'angularfire2.config' ) ;
@@ -18,25 +18,32 @@ export const FirebaseAppConfig = new InjectionToken<FirebaseOptions>('angularfir
18
18
export const RealtimeDatabaseURL = new InjectionToken < string > ( 'angularfire2.realtimeDatabaseURL' ) ;
19
19
20
20
export class FirebaseZoneScheduler {
21
- constructor ( public zone : NgZone ) { }
21
+ constructor ( public zone : NgZone , private platformId : Object ) { }
22
22
schedule ( ...args : any [ ] ) : Subscription {
23
23
return < Subscription > this . zone . runGuarded ( function ( ) { return queue . schedule . apply ( queue , args ) } ) ;
24
24
}
25
25
// TODO this is a hack, clean it up
26
26
keepUnstableUntilFirst < T > ( obs$ : Observable < T > ) {
27
- return new Observable < T > ( subscriber => {
28
- const noop = ( ) => { } ;
29
- const task = Zone . current . scheduleMacroTask ( 'firebaseZoneBlock' , noop , { } , noop , noop ) ;
30
- obs$ . first ( ) . subscribe ( ( ) => this . zone . runOutsideAngular ( ( ) => task . invoke ( ) ) ) ;
31
- return obs$ . subscribe ( subscriber ) ;
32
- } ) ;
27
+ if ( isPlatformServer ( this . platformId ) ) {
28
+ return new Observable < T > ( subscriber => {
29
+ const noop = ( ) => { } ;
30
+ const task = Zone . current . scheduleMacroTask ( 'firebaseZoneBlock' , noop , { } , noop , noop ) ;
31
+ obs$ . first ( ) . subscribe ( ( ) => this . zone . runOutsideAngular ( ( ) => task . invoke ( ) ) ) ;
32
+ return obs$ . subscribe ( subscriber ) ;
33
+ } ) ;
34
+ } else {
35
+ return obs$ ;
36
+ }
33
37
}
34
38
runOutsideAngular < T > ( obs$ : Observable < T > ) : Observable < T > {
35
- const outsideAngular = new Observable < T > ( subscriber => {
39
+ return new Observable < T > ( subscriber => {
36
40
return this . zone . runOutsideAngular ( ( ) => {
37
- return obs$ . subscribe ( subscriber ) ;
41
+ return obs$ . subscribe (
42
+ value => this . zone . run ( ( ) => subscriber . next ( value ) ) ,
43
+ error => this . zone . run ( ( ) => subscriber . error ( error ) ) ,
44
+ ( ) => this . zone . run ( ( ) => subscriber . complete ( ) ) ,
45
+ ) ;
38
46
} ) ;
39
47
} ) ;
40
- return observeOn . call ( outsideAngular , this ) ;
41
48
}
42
49
}
0 commit comments