AsapAction.js 2.6 KB

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