Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Injectable({ providedIn: 'root' })
- export class ZoneService {
- constructor(private ngZone: NgZone) {}
- /**
- * @description Watch for observable emits outside of the zone and run inside zone next or error emits
- */
- public watch<T, E extends any>(observable: Observable<T>): Observable<T> {
- return new Observable(
- (observer: Observer<T>): TeardownLogic => {
- let subscription: Subscription;
- this.ngZone.runOutsideAngular(() => {
- subscription = observable.subscribe({
- next: (result: T): void => this.ngZone.run(() => observer.next(result)),
- error: (err: E): void => this.ngZone.run(() => observer.error(err)),
- complete: (): void => {}
- });
- });
- return (): void => {
- if (subscription != null) {
- subscription.unsubscribe();
- }
- };
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement