VirtualTimeScheduler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var AsyncAction_1 = require("./AsyncAction");
  17. var AsyncScheduler_1 = require("./AsyncScheduler");
  18. var VirtualTimeScheduler = (function (_super) {
  19. __extends(VirtualTimeScheduler, _super);
  20. function VirtualTimeScheduler(SchedulerAction, maxFrames) {
  21. if (SchedulerAction === void 0) { SchedulerAction = VirtualAction; }
  22. if (maxFrames === void 0) { maxFrames = Number.POSITIVE_INFINITY; }
  23. var _this = _super.call(this, SchedulerAction, function () { return _this.frame; }) || this;
  24. _this.maxFrames = maxFrames;
  25. _this.frame = 0;
  26. _this.index = -1;
  27. return _this;
  28. }
  29. VirtualTimeScheduler.prototype.flush = function () {
  30. var _a = this, actions = _a.actions, maxFrames = _a.maxFrames;
  31. var error, action;
  32. while ((action = actions[0]) && action.delay <= maxFrames) {
  33. actions.shift();
  34. this.frame = action.delay;
  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_1.AsyncScheduler));
  49. exports.VirtualTimeScheduler = VirtualTimeScheduler;
  50. var VirtualAction = (function (_super) {
  51. __extends(VirtualAction, _super);
  52. function VirtualAction(scheduler, work, index) {
  53. if (index === void 0) { index = scheduler.index += 1; }
  54. var _this = _super.call(this, scheduler, work) || this;
  55. _this.scheduler = scheduler;
  56. _this.work = work;
  57. _this.index = index;
  58. _this.active = true;
  59. _this.index = scheduler.index = index;
  60. return _this;
  61. }
  62. VirtualAction.prototype.schedule = function (state, delay) {
  63. if (delay === void 0) { delay = 0; }
  64. if (!this.id) {
  65. return _super.prototype.schedule.call(this, state, delay);
  66. }
  67. this.active = false;
  68. var action = new VirtualAction(this.scheduler, this.work);
  69. this.add(action);
  70. return action.schedule(state, delay);
  71. };
  72. VirtualAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  73. if (delay === void 0) { delay = 0; }
  74. this.delay = scheduler.frame + delay;
  75. var actions = scheduler.actions;
  76. actions.push(this);
  77. actions.sort(VirtualAction.sortActions);
  78. return true;
  79. };
  80. VirtualAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  81. if (delay === void 0) { delay = 0; }
  82. return undefined;
  83. };
  84. VirtualAction.prototype._execute = function (state, delay) {
  85. if (this.active === true) {
  86. return _super.prototype._execute.call(this, state, delay);
  87. }
  88. };
  89. VirtualAction.sortActions = function (a, b) {
  90. if (a.delay === b.delay) {
  91. if (a.index === b.index) {
  92. return 0;
  93. }
  94. else if (a.index > b.index) {
  95. return 1;
  96. }
  97. else {
  98. return -1;
  99. }
  100. }
  101. else if (a.delay > b.delay) {
  102. return 1;
  103. }
  104. else {
  105. return -1;
  106. }
  107. };
  108. return VirtualAction;
  109. }(AsyncAction_1.AsyncAction));
  110. exports.VirtualAction = VirtualAction;
  111. //# sourceMappingURL=VirtualTimeScheduler.js.map