Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/core/src/defer/timer_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {Injector, ɵɵdefineInjectable} from '../di';
import {arrayInsert2, arraySplice} from '../util/array_utils';
import {NgZone} from '../zone';

/**
* Returns a function that captures a provided delay.
Expand All @@ -27,8 +28,9 @@ export function onTimer(delay: number) {
*/
export function scheduleTimerTrigger(delay: number, callback: VoidFunction, injector: Injector) {
const scheduler = injector.get(TimerScheduler);
const ngZone = injector.get(NgZone);
const cleanupFn = () => scheduler.remove(callback);
scheduler.add(delay, callback);
scheduler.add(delay, callback, ngZone);
return cleanupFn;
}

Expand Down Expand Up @@ -60,10 +62,10 @@ export class TimerScheduler {
// as the shape of the `current` list.
deferred: Array<number | VoidFunction> = [];

add(delay: number, callback: VoidFunction) {
add(delay: number, callback: VoidFunction, ngZone: NgZone) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since TimerScheduler is an injectable class, we could avoid passing ngZone as an argument, but instead just inject it in the class directly (using the inject function).

const target = this.executingCallbacks ? this.deferred : this.current;
this.addToQueue(target, Date.now() + delay, callback);
this.scheduleTimer();
this.scheduleTimer(ngZone);
}

remove(callback: VoidFunction) {
Expand Down Expand Up @@ -117,7 +119,7 @@ export class TimerScheduler {
return index;
}

private scheduleTimer() {
private scheduleTimer(ngZone: NgZone) {
const callback = () => {
this.clearTimeout();

Expand Down Expand Up @@ -170,7 +172,7 @@ export class TimerScheduler {
}
this.deferred.length = 0;
}
this.scheduleTimer();
this.scheduleTimer(ngZone);
};

// Avoid running timer callbacks more than once per
Expand Down Expand Up @@ -198,7 +200,9 @@ export class TimerScheduler {

const timeout = Math.max(invokeAt - now, FRAME_DURATION_MS);
this.invokeTimerAt = invokeAt;
this.timeoutId = setTimeout(callback, timeout) as unknown as number;
this.timeoutId = ngZone.runOutsideAngular(() => {
return setTimeout(() => ngZone.run(callback), timeout) as unknown as number;
});
}
}
}
Expand Down