VirtualTimeScheduler.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { __extends } from "tslib";
  2. import { AsyncAction } from './AsyncAction';
  3. import { Subscription } from '../Subscription';
  4. import { AsyncScheduler } from './AsyncScheduler';
  5. var VirtualTimeScheduler = (function (_super) {
  6. __extends(VirtualTimeScheduler, _super);
  7. function VirtualTimeScheduler(schedulerActionCtor, maxFrames) {
  8. if (schedulerActionCtor === void 0) { schedulerActionCtor = VirtualAction; }
  9. if (maxFrames === void 0) { maxFrames = Infinity; }
  10. var _this = _super.call(this, schedulerActionCtor, function () { return _this.frame; }) || this;
  11. _this.maxFrames = maxFrames;
  12. _this.frame = 0;
  13. _this.index = -1;
  14. return _this;
  15. }
  16. VirtualTimeScheduler.prototype.flush = function () {
  17. var _a = this, actions = _a.actions, maxFrames = _a.maxFrames;
  18. var error;
  19. var action;
  20. while ((action = actions[0]) && action.delay <= maxFrames) {
  21. actions.shift();
  22. this.frame = action.delay;
  23. if ((error = action.execute(action.state, action.delay))) {
  24. break;
  25. }
  26. }
  27. if (error) {
  28. while ((action = actions.shift())) {
  29. action.unsubscribe();
  30. }
  31. throw error;
  32. }
  33. };
  34. VirtualTimeScheduler.frameTimeFactor = 10;
  35. return VirtualTimeScheduler;
  36. }(AsyncScheduler));
  37. export { VirtualTimeScheduler };
  38. var VirtualAction = (function (_super) {
  39. __extends(VirtualAction, _super);
  40. function VirtualAction(scheduler, work, index) {
  41. if (index === void 0) { index = (scheduler.index += 1); }
  42. var _this = _super.call(this, scheduler, work) || this;
  43. _this.scheduler = scheduler;
  44. _this.work = work;
  45. _this.index = index;
  46. _this.active = true;
  47. _this.index = scheduler.index = index;
  48. return _this;
  49. }
  50. VirtualAction.prototype.schedule = function (state, delay) {
  51. if (delay === void 0) { delay = 0; }
  52. if (Number.isFinite(delay)) {
  53. if (!this.id) {
  54. return _super.prototype.schedule.call(this, state, delay);
  55. }
  56. this.active = false;
  57. var action = new VirtualAction(this.scheduler, this.work);
  58. this.add(action);
  59. return action.schedule(state, delay);
  60. }
  61. else {
  62. return Subscription.EMPTY;
  63. }
  64. };
  65. VirtualAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  66. if (delay === void 0) { delay = 0; }
  67. this.delay = scheduler.frame + delay;
  68. var actions = scheduler.actions;
  69. actions.push(this);
  70. actions.sort(VirtualAction.sortActions);
  71. return 1;
  72. };
  73. VirtualAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  74. if (delay === void 0) { delay = 0; }
  75. return undefined;
  76. };
  77. VirtualAction.prototype._execute = function (state, delay) {
  78. if (this.active === true) {
  79. return _super.prototype._execute.call(this, state, delay);
  80. }
  81. };
  82. VirtualAction.sortActions = function (a, b) {
  83. if (a.delay === b.delay) {
  84. if (a.index === b.index) {
  85. return 0;
  86. }
  87. else if (a.index > b.index) {
  88. return 1;
  89. }
  90. else {
  91. return -1;
  92. }
  93. }
  94. else if (a.delay > b.delay) {
  95. return 1;
  96. }
  97. else {
  98. return -1;
  99. }
  100. };
  101. return VirtualAction;
  102. }(AsyncAction));
  103. export { VirtualAction };
  104. //# sourceMappingURL=VirtualTimeScheduler.js.map