-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathangularfire2.ts
48 lines (43 loc) · 1.98 KB
/
angularfire2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { InjectionToken, NgZone } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { Observable, Subscription } from 'rxjs';
import { queue } from 'rxjs/scheduler/queue';
import { first } from 'rxjs/operators';
import firebase from '@firebase/app';
import { FirebaseApp, FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
import {} from 'zone.js';
export const FirebaseAppNameToken = new InjectionToken<string|undefined>('angularfire2.app.name');
export const FirebaseOptionsToken = new InjectionToken<FirebaseOptions>('angularfire2.app.options');
export const FirebaseAppConfigToken = new InjectionToken<FirebaseAppConfig|undefined>('angularfire2.app.config');
// Put in database.ts when we drop database-depreciated
export const RealtimeDatabaseURL = new InjectionToken<string>('angularfire2.realtimeDatabaseURL');
export class FirebaseZoneScheduler {
constructor(public zone: NgZone, private platformId: Object) {}
schedule(...args: any[]): Subscription {
return <Subscription>this.zone.runGuarded(function() { return queue.schedule.apply(queue, args)});
}
// TODO this is a hack, clean it up
keepUnstableUntilFirst<T>(obs$: Observable<T>) {
if (isPlatformServer(this.platformId)) {
return new Observable<T>(subscriber => {
const noop = () => {};
const task = Zone.current.scheduleMacroTask('firebaseZoneBlock', noop, {}, noop, noop);
obs$.pipe(first()).subscribe(() => this.zone.runOutsideAngular(() => task.invoke()));
return obs$.subscribe(subscriber);
});
} else {
return obs$;
}
}
runOutsideAngular<T>(obs$: Observable<T>): Observable<T> {
return new Observable<T>(subscriber => {
return this.zone.runOutsideAngular(() => {
return obs$.subscribe(
value => this.zone.run(() => subscriber.next(value)),
error => this.zone.run(() => subscriber.error(error)),
() => this.zone.run(() => subscriber.complete()),
);
});
});
}
}