Debian Codegraph @

node/rxjs/src/internal/scheduler/AnimationFrameAction.ts

raw
1 import { AsyncAction } from './AsyncAction';
2 import { AnimationFrameScheduler } from './AnimationFrameScheduler';
3 import { SchedulerAction } from '../types';
4 import { animationFrameProvider } from './animationFrameProvider';
5 import { TimerHandle } from './timerHandle';
6
7 export class AnimationFrameAction<T> extends AsyncAction<T> {
8 constructor(protected scheduler: AnimationFrameScheduler, protected work: (this: SchedulerAction<T>, state?: T) => void) {
9 super(scheduler, work);
10 }
11
12 protected requestAsyncId(scheduler: AnimationFrameScheduler, id?: TimerHandle, delay: number = 0): TimerHandle {
13 // If delay is greater than 0, request as an async action.
14 if (delay !== null && delay > 0) {
15 return super.requestAsyncId(scheduler, id, delay);
16 }
17 // Push the action to the end of the scheduler queue.
18 scheduler.actions.push(this);
19 // If an animation frame has already been requested, don't request another
20 // one. If an animation frame hasn't been requested yet, request one. Return
21 // the current animation frame request id.
22 return scheduler._scheduled || (scheduler._scheduled = animationFrameProvider.requestAnimationFrame(() => scheduler.flush(undefined)));
23 }
24
25 protected recycleAsyncId(scheduler: AnimationFrameScheduler, id?: TimerHandle, delay: number = 0): TimerHandle | undefined {
26 // If delay exists and is greater than 0, or if the delay is null (the
27 // action wasn't rescheduled) but was originally scheduled as an async
28 // action, then recycle as an async action.
29 if (delay != null ? delay > 0 : this.delay > 0) {
30 return super.recycleAsyncId(scheduler, id, delay);
31 }
32 // If the scheduler queue has no remaining actions with the same async id,
33 // cancel the requested animation frame and set the scheduled flag to
34 // undefined so the next AnimationFrameAction will request its own.
35 const { actions } = scheduler;
36 if (id != null && id === scheduler._scheduled && actions[actions.length - 1]?.id !== id) {
37 animationFrameProvider.cancelAnimationFrame(id as number);
38 scheduler._scheduled = undefined;
39 }
40 // Return undefined so the action knows to request a new async id if it's rescheduled.
41 return undefined;
42 }
43 }