Skip to content

Instantly share code, notes, and snippets.

@zakhenry
Last active October 19, 2017 16:03
Show Gist options
  • Save zakhenry/0f050d169c1d8373c94b5c68374e3394 to your computer and use it in GitHub Desktop.
Save zakhenry/0f050d169c1d8373c94b5c68374e3394 to your computer and use it in GitHub Desktop.
Display duration Angular Zone has been stable for
<pre>NgZone has been stable for {{stableSinceSecondsAgo | async | number:'.3-3'}} seconds</pre>
class SomeComponent {
public stableSinceSecondsAgo: Observable<number>;
constructor(zone: NgZone, cdr: ChangeDetectorRef) {
zone.runOutsideAngular(() => {
this.stableSinceSecondsAgo = this.zone.onStable
.timestamp()
.throttleTime(300)
.switchMap((ts) => Observable.timer(0, 300).mapTo(ts))
.map(ts => (new Date().valueOf() - ts.timestamp) / 1000)
.do(() => cdr.detectChanges());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment