Front end of the Slack clone application.

VirtualTimeScheduler.js 4.1KB

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