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