a zip code crypto-currency system good for red ONLY

AsyncAction.js 4.9KB

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