Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

AsyncAction.ts 4.8KB

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