question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subscriptions are not executed in NgZone

See original GitHub issue

If a subscription event comes, the store is updated, the observable emits, but no change detection is running in angular. (I need to interact with a component to get the view being updated)

// service
const gameChanges = gql`
  subscription GameChanges {
    gameChanged {
      ...GameFragment
    }
  }
  ${UserFragment}
  ${GameFragment}
`;

this.gameChanges$ = apollo.subscribe({
      query: gameChanges
    }).map((r: { gameChanged: Game }) => r.gameChanged)

// component
this._gameService.gameChanges$.subscribe(g => console.log(`game changed: ${g.id}`));

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ribizlicommented, Sep 6, 2017

Workaround No.2 - wrapping the watchQuery observable into NgZone:

function observeOnZone<T>(this: Observable<T>, zone: NgZone): Observable<T> {
    return Observable.create(observer => {
        const onNext = (value) => zone.run(() => observer.next(value));
        const onError = (e) => zone.run(() => observer.error(e));
        const onComplete = () => zone.run(() => observer.complete());
        return this.subscribe(onNext, onError, onComplete);
    });
};

Observable.prototype.observeOnZone = observeOnZone;

declare module 'rxjs/Observable' {
  interface Observable<T> {
    observeOnZone: typeof observeOnZone;
  }
}

// in service

this._apollo.watchQuery<CurrentPlaylistQueryResult>({
  query: currentPlaylistQuery
}).map(r => r.data.currentPlaylist)
  .observeOnZone(this._ngZone);
0reactions
kamilkisielacommented, Nov 10, 2017

works in apollo-angular@1.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscriptions are not executed in NgZone · Issue #320 - GitHub
If a subscription event comes, the store is updated, the observable emits, but no change detection is running in angular.
Read more >
Subscribe not working even in NgZone.run() - angular
I have a service which gets the data from API. Data is getting returned from API but subscribe is not working in the...
Read more >
NgZone - Angular
A zone is an execution context that persists across async tasks. ... serverUrl).subscribe(response => { // user does not need to trigger change...
Read more >
Running event listeners outside of the NgZone - InDepth.Dev
NgZone notifies Angular when to perform the change detection process (e.g. a DOM event with bound listener is one of the triggerers).
Read more >
Boosting performance of Angular applications with manual ...
We can perform an async operation without triggering change detection. NgZone has an API runOutsideAngular that runs code in NgZone's _outer zone, here...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found