11import { InjectionToken , NgZone } from '@angular/core' ;
2- import { Subscription } from 'rxjs/Subscription' ;
32import { Observable } from 'rxjs/Observable' ;
3+ import { Subscription } from 'rxjs/Subscription' ;
44import { queue } from 'rxjs/scheduler/queue' ;
5+ import { isPlatformServer } from '@angular/common' ;
6+ import { observeOn } from 'rxjs/operator/observeOn' ;
57
68import firebase from '@firebase/app' ;
79import { FirebaseApp , FirebaseOptions } from '@firebase/app-types' ;
810
911import 'zone.js' ;
1012import 'rxjs/add/operator/first' ;
11- import { Subscriber } from 'rxjs/Subscriber' ;
12- import { observeOn } from 'rxjs/operator/observeOn' ;
1313
1414export const FirebaseAppName = new InjectionToken < string > ( 'angularfire2.appName' ) ;
1515export const FirebaseAppConfig = new InjectionToken < FirebaseOptions > ( 'angularfire2.config' ) ;
@@ -18,25 +18,32 @@ export const FirebaseAppConfig = new InjectionToken<FirebaseOptions>('angularfir
1818export const RealtimeDatabaseURL = new InjectionToken < string > ( 'angularfire2.realtimeDatabaseURL' ) ;
1919
2020export class FirebaseZoneScheduler {
21- constructor ( public zone : NgZone ) { }
21+ constructor ( public zone : NgZone , private platformId : Object ) { }
2222 schedule ( ...args : any [ ] ) : Subscription {
2323 return < Subscription > this . zone . runGuarded ( function ( ) { return queue . schedule . apply ( queue , args ) } ) ;
2424 }
2525 // TODO this is a hack, clean it up
2626 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+ }
3337 }
3438 runOutsideAngular < T > ( obs$ : Observable < T > ) : Observable < T > {
35- const outsideAngular = new Observable < T > ( subscriber => {
39+ return new Observable < T > ( subscriber => {
3640 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+ ) ;
3846 } ) ;
3947 } ) ;
40- return observeOn . call ( outsideAngular , this ) ;
4148 }
4249}
0 commit comments