|
import { AsyncAction } from './AsyncAction'; |
|
import { AnimationFrameScheduler } from './AnimationFrameScheduler'; |
|
import { SchedulerAction } from '../types'; |
|
import { animationFrameProvider } from './animationFrameProvider'; |
|
import { TimerHandle } from './timerHandle'; |
|
|
|
export class AnimationFrameAction<T> extends AsyncAction<T> { |
|
constructor(protected scheduler: AnimationFrameScheduler, protected work: (this: SchedulerAction<T>, state?: T) => void) { |
|
super(scheduler, work); |
|
} |
|
|
|
protected requestAsyncId(scheduler: AnimationFrameScheduler, id?: TimerHandle, delay: number = 0): TimerHandle { |
|
|
|
if (delay !== null && delay > 0) { |
|
return super.requestAsyncId(scheduler, id, delay); |
|
} |
|
|
|
scheduler.actions.push(this); |
|
|
|
|
|
|
|
return scheduler._scheduled || (scheduler._scheduled = animationFrameProvider.requestAnimationFrame(() => scheduler.flush(undefined))); |
|
} |
|
|
|
protected recycleAsyncId(scheduler: AnimationFrameScheduler, id?: TimerHandle, delay: number = 0): TimerHandle | undefined { |
|
|
|
|
|
|
|
if (delay != null ? delay > 0 : this.delay > 0) { |
|
return super.recycleAsyncId(scheduler, id, delay); |
|
} |
|
|
|
|
|
|
|
const { actions } = scheduler; |
|
if (id != null && actions[actions.length - 1]?.id !== id) { |
|
animationFrameProvider.cancelAnimationFrame(id as number); |
|
scheduler._scheduled = undefined; |
|
} |
|
|
|
return undefined; |
|
} |
|
} |
|
|