VirtualTimeScheduler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.VirtualAction = exports.VirtualTimeScheduler = void 0;
  19. var AsyncAction_1 = require("./AsyncAction");
  20. var Subscription_1 = require("../Subscription");
  21. var AsyncScheduler_1 = require("./AsyncScheduler");
  22. var VirtualTimeScheduler = (function (_super) {
  23. __extends(VirtualTimeScheduler, _super);
  24. function VirtualTimeScheduler(schedulerActionCtor, maxFrames) {
  25. if (schedulerActionCtor === void 0) { schedulerActionCtor = VirtualAction; }
  26. if (maxFrames === void 0) { maxFrames = Infinity; }
  27. var _this = _super.call(this, schedulerActionCtor, function () { return _this.frame; }) || this;
  28. _this.maxFrames = maxFrames;
  29. _this.frame = 0;
  30. _this.index = -1;
  31. return _this;
  32. }
  33. VirtualTimeScheduler.prototype.flush = function () {
  34. var _a = this, actions = _a.actions, maxFrames = _a.maxFrames;
  35. var error;
  36. var action;
  37. while ((action = actions[0]) && action.delay <= maxFrames) {
  38. actions.shift();
  39. this.frame = action.delay;
  40. if ((error = action.execute(action.state, action.delay))) {
  41. break;
  42. }
  43. }
  44. if (error) {
  45. while ((action = actions.shift())) {
  46. action.unsubscribe();
  47. }
  48. throw error;
  49. }
  50. };
  51. VirtualTimeScheduler.frameTimeFactor = 10;
  52. return VirtualTimeScheduler;
  53. }(AsyncScheduler_1.AsyncScheduler));
  54. exports.VirtualTimeScheduler = VirtualTimeScheduler;
  55. var VirtualAction = (function (_super) {
  56. __extends(VirtualAction, _super);
  57. function VirtualAction(scheduler, work, index) {
  58. if (index === void 0) { index = (scheduler.index += 1); }
  59. var _this = _super.call(this, scheduler, work) || this;
  60. _this.scheduler = scheduler;
  61. _this.work = work;
  62. _this.index = index;
  63. _this.active = true;
  64. _this.index = scheduler.index = index;
  65. return _this;
  66. }
  67. VirtualAction.prototype.schedule = function (state, delay) {
  68. if (delay === void 0) { delay = 0; }
  69. if (Number.isFinite(delay)) {
  70. if (!this.id) {
  71. return _super.prototype.schedule.call(this, state, delay);
  72. }
  73. this.active = false;
  74. var action = new VirtualAction(this.scheduler, this.work);
  75. this.add(action);
  76. return action.schedule(state, delay);
  77. }
  78. else {
  79. return Subscription_1.Subscription.EMPTY;
  80. }
  81. };
  82. VirtualAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  83. if (delay === void 0) { delay = 0; }
  84. this.delay = scheduler.frame + delay;
  85. var actions = scheduler.actions;
  86. actions.push(this);
  87. actions.sort(VirtualAction.sortActions);
  88. return 1;
  89. };
  90. VirtualAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  91. if (delay === void 0) { delay = 0; }
  92. return undefined;
  93. };
  94. VirtualAction.prototype._execute = function (state, delay) {
  95. if (this.active === true) {
  96. return _super.prototype._execute.call(this, state, delay);
  97. }
  98. };
  99. VirtualAction.sortActions = function (a, b) {
  100. if (a.delay === b.delay) {
  101. if (a.index === b.index) {
  102. return 0;
  103. }
  104. else if (a.index > b.index) {
  105. return 1;
  106. }
  107. else {
  108. return -1;
  109. }
  110. }
  111. else if (a.delay > b.delay) {
  112. return 1;
  113. }
  114. else {
  115. return -1;
  116. }
  117. };
  118. return VirtualAction;
  119. }(AsyncAction_1.AsyncAction));
  120. exports.VirtualAction = VirtualAction;
  121. //# sourceMappingURL=VirtualTimeScheduler.js.map