AnimationFrameAction.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var AsyncAction_1 = require('./AsyncAction');
  8. var AnimationFrame_1 = require('../util/AnimationFrame');
  9. /**
  10. * We need this JSDoc comment for affecting ESDoc.
  11. * @ignore
  12. * @extends {Ignored}
  13. */
  14. var AnimationFrameAction = (function (_super) {
  15. __extends(AnimationFrameAction, _super);
  16. function AnimationFrameAction(scheduler, work) {
  17. _super.call(this, scheduler, work);
  18. this.scheduler = scheduler;
  19. this.work = work;
  20. }
  21. AnimationFrameAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  22. if (delay === void 0) { delay = 0; }
  23. // If delay is greater than 0, request as an async action.
  24. if (delay !== null && delay > 0) {
  25. return _super.prototype.requestAsyncId.call(this, scheduler, id, delay);
  26. }
  27. // Push the action to the end of the scheduler queue.
  28. scheduler.actions.push(this);
  29. // If an animation frame has already been requested, don't request another
  30. // one. If an animation frame hasn't been requested yet, request one. Return
  31. // the current animation frame request id.
  32. return scheduler.scheduled || (scheduler.scheduled = AnimationFrame_1.AnimationFrame.requestAnimationFrame(scheduler.flush.bind(scheduler, null)));
  33. };
  34. AnimationFrameAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  35. if (delay === void 0) { delay = 0; }
  36. // If delay exists and is greater than 0, or if the delay is null (the
  37. // action wasn't rescheduled) but was originally scheduled as an async
  38. // action, then recycle as an async action.
  39. if ((delay !== null && delay > 0) || (delay === null && this.delay > 0)) {
  40. return _super.prototype.recycleAsyncId.call(this, scheduler, id, delay);
  41. }
  42. // If the scheduler queue is empty, cancel the requested animation frame and
  43. // set the scheduled flag to undefined so the next AnimationFrameAction will
  44. // request its own.
  45. if (scheduler.actions.length === 0) {
  46. AnimationFrame_1.AnimationFrame.cancelAnimationFrame(id);
  47. scheduler.scheduled = undefined;
  48. }
  49. // Return undefined so the action knows to request a new async id if it's rescheduled.
  50. return undefined;
  51. };
  52. return AnimationFrameAction;
  53. }(AsyncAction_1.AsyncAction));
  54. exports.AnimationFrameAction = AnimationFrameAction;
  55. //# sourceMappingURL=AnimationFrameAction.js.map