VirtualTimeScheduler.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /** PURE_IMPORTS_START ._AsyncAction,._AsyncScheduler 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 { AsyncAction } from './AsyncAction';
  10. import { AsyncScheduler } from './AsyncScheduler';
  11. export var VirtualTimeScheduler = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  12. __extends(VirtualTimeScheduler, _super);
  13. function VirtualTimeScheduler(SchedulerAction, maxFrames) {
  14. var _this = this;
  15. if (SchedulerAction === void 0) {
  16. SchedulerAction = VirtualAction;
  17. }
  18. if (maxFrames === void 0) {
  19. maxFrames = Number.POSITIVE_INFINITY;
  20. }
  21. _super.call(this, SchedulerAction, function () { return _this.frame; });
  22. this.maxFrames = maxFrames;
  23. this.frame = 0;
  24. this.index = -1;
  25. }
  26. /**
  27. * Prompt the Scheduler to execute all of its queued actions, therefore
  28. * clearing its queue.
  29. * @return {void}
  30. */
  31. VirtualTimeScheduler.prototype.flush = function () {
  32. var _a = this, actions = _a.actions, maxFrames = _a.maxFrames;
  33. var error, action;
  34. while ((action = actions.shift()) && (this.frame = action.delay) <= maxFrames) {
  35. if (error = action.execute(action.state, action.delay)) {
  36. break;
  37. }
  38. }
  39. if (error) {
  40. while (action = actions.shift()) {
  41. action.unsubscribe();
  42. }
  43. throw error;
  44. }
  45. };
  46. VirtualTimeScheduler.frameTimeFactor = 10;
  47. return VirtualTimeScheduler;
  48. }(AsyncScheduler));
  49. /**
  50. * We need this JSDoc comment for affecting ESDoc.
  51. * @ignore
  52. * @extends {Ignored}
  53. */
  54. export var VirtualAction = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  55. __extends(VirtualAction, _super);
  56. function VirtualAction(scheduler, work, index) {
  57. if (index === void 0) {
  58. index = scheduler.index += 1;
  59. }
  60. _super.call(this, scheduler, work);
  61. this.scheduler = scheduler;
  62. this.work = work;
  63. this.index = index;
  64. this.active = true;
  65. this.index = scheduler.index = index;
  66. }
  67. VirtualAction.prototype.schedule = function (state, delay) {
  68. if (delay === void 0) {
  69. delay = 0;
  70. }
  71. if (!this.id) {
  72. return _super.prototype.schedule.call(this, state, delay);
  73. }
  74. this.active = false;
  75. // If an action is rescheduled, we save allocations by mutating its state,
  76. // pushing it to the end of the scheduler queue, and recycling the action.
  77. // But since the VirtualTimeScheduler is used for testing, VirtualActions
  78. // must be immutable so they can be inspected later.
  79. var action = new VirtualAction(this.scheduler, this.work);
  80. this.add(action);
  81. return action.schedule(state, delay);
  82. };
  83. VirtualAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  84. if (delay === void 0) {
  85. delay = 0;
  86. }
  87. this.delay = scheduler.frame + delay;
  88. var actions = scheduler.actions;
  89. actions.push(this);
  90. actions.sort(VirtualAction.sortActions);
  91. return true;
  92. };
  93. VirtualAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  94. if (delay === void 0) {
  95. delay = 0;
  96. }
  97. return undefined;
  98. };
  99. VirtualAction.prototype._execute = function (state, delay) {
  100. if (this.active === true) {
  101. return _super.prototype._execute.call(this, state, delay);
  102. }
  103. };
  104. VirtualAction.sortActions = function (a, b) {
  105. if (a.delay === b.delay) {
  106. if (a.index === b.index) {
  107. return 0;
  108. }
  109. else if (a.index > b.index) {
  110. return 1;
  111. }
  112. else {
  113. return -1;
  114. }
  115. }
  116. else if (a.delay > b.delay) {
  117. return 1;
  118. }
  119. else {
  120. return -1;
  121. }
  122. };
  123. return VirtualAction;
  124. }(AsyncAction));
  125. //# sourceMappingURL=VirtualTimeScheduler.js.map