Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

AnimationFrameAction.ts 2.0KB

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