Guest User

Untitled

a guest
Oct 13th, 2019
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Injectable({ providedIn: 'root' })
  2. export class ZoneService {
  3. constructor(private ngZone: NgZone) {}
  4.  
  5. /**
  6. * @description Watch for observable emits outside of the zone and run inside zone next or error emits
  7. */
  8. public watch<T, E extends any>(observable: Observable<T>): Observable<T> {
  9. return new Observable(
  10. (observer: Observer<T>): TeardownLogic => {
  11. let subscription: Subscription;
  12. this.ngZone.runOutsideAngular(() => {
  13. subscription = observable.subscribe({
  14. next: (result: T): void => this.ngZone.run(() => observer.next(result)),
  15. error: (err: E): void => this.ngZone.run(() => observer.error(err)),
  16. complete: (): void => {}
  17. });
  18. });
  19. return (): void => {
  20. if (subscription != null) {
  21. subscription.unsubscribe();
  22. }
  23. };
  24. }
  25. );
  26. }
  27. }
RAW Paste Data