Front end of the Slack clone application.

AsyncAction.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /** PURE_IMPORTS_START .._util_root,._Action 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 { root } from '../util/root';
  10. import { Action } from './Action';
  11. /**
  12. * We need this JSDoc comment for affecting ESDoc.
  13. * @ignore
  14. * @extends {Ignored}
  15. */
  16. export var AsyncAction = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  17. __extends(AsyncAction, _super);
  18. function AsyncAction(scheduler, work) {
  19. _super.call(this, scheduler, work);
  20. this.scheduler = scheduler;
  21. this.work = work;
  22. this.pending = false;
  23. }
  24. AsyncAction.prototype.schedule = function (state, delay) {
  25. if (delay === void 0) {
  26. delay = 0;
  27. }
  28. if (this.closed) {
  29. return this;
  30. }
  31. // Always replace the current state with the new state.
  32. this.state = state;
  33. // Set the pending flag indicating that this action has been scheduled, or
  34. // has recursively rescheduled itself.
  35. this.pending = true;
  36. var id = this.id;
  37. var scheduler = this.scheduler;
  38. //
  39. // Important implementation note:
  40. //
  41. // Actions only execute once by default, unless rescheduled from within the
  42. // scheduled callback. This allows us to implement single and repeat
  43. // actions via the same code path, without adding API surface area, as well
  44. // as mimic traditional recursion but across asynchronous boundaries.
  45. //
  46. // However, JS runtimes and timers distinguish between intervals achieved by
  47. // serial `setTimeout` calls vs. a single `setInterval` call. An interval of
  48. // serial `setTimeout` calls can be individually delayed, which delays
  49. // scheduling the next `setTimeout`, and so on. `setInterval` attempts to
  50. // guarantee the interval callback will be invoked more precisely to the
  51. // interval period, regardless of load.
  52. //
  53. // Therefore, we use `setInterval` to schedule single and repeat actions.
  54. // If the action reschedules itself with the same delay, the interval is not
  55. // canceled. If the action doesn't reschedule, or reschedules with a
  56. // different delay, the interval will be canceled after scheduled callback
  57. // execution.
  58. //
  59. if (id != null) {
  60. this.id = this.recycleAsyncId(scheduler, id, delay);
  61. }
  62. this.delay = delay;
  63. // If this action has already an async Id, don't request a new one.
  64. this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
  65. return this;
  66. };
  67. AsyncAction.prototype.requestAsyncId = function (scheduler, id, delay) {
  68. if (delay === void 0) {
  69. delay = 0;
  70. }
  71. return root.setInterval(scheduler.flush.bind(scheduler, this), delay);
  72. };
  73. AsyncAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
  74. if (delay === void 0) {
  75. delay = 0;
  76. }
  77. // If this action is rescheduled with the same delay time, don't clear the interval id.
  78. if (delay !== null && this.delay === delay && this.pending === false) {
  79. return id;
  80. }
  81. // Otherwise, if the action's delay time is different from the current delay,
  82. // or the action has been rescheduled before it's executed, clear the interval id
  83. return root.clearInterval(id) && undefined || undefined;
  84. };
  85. /**
  86. * Immediately executes this action and the `work` it contains.
  87. * @return {any}
  88. */
  89. AsyncAction.prototype.execute = function (state, delay) {
  90. if (this.closed) {
  91. return new Error('executing a cancelled action');
  92. }
  93. this.pending = false;
  94. var error = this._execute(state, delay);
  95. if (error) {
  96. return error;
  97. }
  98. else if (this.pending === false && this.id != null) {
  99. // Dequeue if the action didn't reschedule itself. Don't call
  100. // unsubscribe(), because the action could reschedule later.
  101. // For example:
  102. // ```
  103. // scheduler.schedule(function doWork(counter) {
  104. // /* ... I'm a busy worker bee ... */
  105. // var originalAction = this;
  106. // /* wait 100ms before rescheduling the action */
  107. // setTimeout(function () {
  108. // originalAction.schedule(counter + 1);
  109. // }, 100);
  110. // }, 1000);
  111. // ```
  112. this.id = this.recycleAsyncId(this.scheduler, this.id, null);
  113. }
  114. };
  115. AsyncAction.prototype._execute = function (state, delay) {
  116. var errored = false;
  117. var errorValue = undefined;
  118. try {
  119. this.work(state);
  120. }
  121. catch (e) {
  122. errored = true;
  123. errorValue = !!e && e || new Error(e);
  124. }
  125. if (errored) {
  126. this.unsubscribe();
  127. return errorValue;
  128. }
  129. };
  130. /** @deprecated internal use only */ AsyncAction.prototype._unsubscribe = function () {
  131. var id = this.id;
  132. var scheduler = this.scheduler;
  133. var actions = scheduler.actions;
  134. var index = actions.indexOf(this);
  135. this.work = null;
  136. this.state = null;
  137. this.pending = false;
  138. this.scheduler = null;
  139. if (index !== -1) {
  140. actions.splice(index, 1);
  141. }
  142. if (id != null) {
  143. this.id = this.recycleAsyncId(scheduler, id, null);
  144. }
  145. this.delay = null;
  146. };
  147. return AsyncAction;
  148. }(Action));
  149. //# sourceMappingURL=AsyncAction.js.map