forked from ngrx/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tick-scheduler.ts
41 lines (36 loc) · 951 Bytes
/
tick-scheduler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ApplicationRef, inject, Injectable, NgZone } from '@angular/core';
import { isNgZone } from './zone-helpers';
@Injectable({
providedIn: 'root',
useFactory: () => {
const zone = inject(NgZone);
return isNgZone(zone)
? new NoopTickScheduler()
: inject(AnimationFrameTickScheduler);
},
})
export abstract class TickScheduler {
abstract schedule(): void;
}
@Injectable({
providedIn: 'root',
})
export class AnimationFrameTickScheduler extends TickScheduler {
private isScheduled = false;
constructor(private readonly appRef: ApplicationRef) {
super();
}
schedule(): void {
if (!this.isScheduled) {
this.isScheduled = true;
requestAnimationFrame(() => {
this.appRef.tick();
this.isScheduled = false;
});
}
}
}
export class NoopTickScheduler extends TickScheduler {
// eslint-disable-next-line @typescript-eslint/no-empty-function
schedule(): void {}
}