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