AsapAction.js 2.5 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 Immediate_1 = require('../util/Immediate');
  8. var AsyncAction_1 = require('./AsyncAction');
  9. /**
  10. * We need this JSDoc comment for affecting ESDoc.
  11. * @ignore
  12. * @extends {Ignored}
  13. */
  14. var AsapAction = (function (_super) {
  15. __extends(AsapAction, _super);
  16. function AsapAction(scheduler, work) {
  17. _super.call(this, scheduler, work);
  18. this.scheduler = scheduler;
  19. this.work = work;
  20. }
  21. AsapAction.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 a microtask has already been scheduled, don't schedule another
  30. // one. If a microtask hasn't been scheduled yet, schedule one now. Return
  31. // the current scheduled microtask id.
  32. return scheduler.scheduled || (scheduler.scheduled = Immediate_1.Immediate.setImmediate(scheduler.flush.bind(scheduler, null)));
  33. };
  34. AsapAction.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 microtask and
  43. // set the scheduled flag to undefined so the next AsapAction will schedule
  44. // its own.
  45. if (scheduler.actions.length === 0) {
  46. Immediate_1.Immediate.clearImmediate(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 AsapAction;
  53. }(AsyncAction_1.AsyncAction));
  54. exports.AsapAction = AsapAction;
  55. //# sourceMappingURL=AsapAction.js.map