842b887901481eafce919dc5651370f22804ed73b6a433662046aa85a76c44e3c399153607234328e7a75fc6cb016b2b18fea762aae3d474d23a22d5d69ba4 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { AsyncAction } from './AsyncAction';
  2. import { Subscription } from '../Subscription';
  3. import { AsyncScheduler } from './AsyncScheduler';
  4. import { SchedulerAction } from '../types';
  5. import { TimerHandle } from './timerHandle';
  6. export class VirtualTimeScheduler extends AsyncScheduler {
  7. /** @deprecated Not used in VirtualTimeScheduler directly. Will be removed in v8. */
  8. static frameTimeFactor = 10;
  9. /**
  10. * The current frame for the state of the virtual scheduler instance. The difference
  11. * between two "frames" is synonymous with the passage of "virtual time units". So if
  12. * you record `scheduler.frame` to be `1`, then later, observe `scheduler.frame` to be at `11`,
  13. * that means `10` virtual time units have passed.
  14. */
  15. public frame: number = 0;
  16. /**
  17. * Used internally to examine the current virtual action index being processed.
  18. * @deprecated Internal implementation detail, do not use directly. Will be made internal in v8.
  19. */
  20. public index: number = -1;
  21. /**
  22. * This creates an instance of a `VirtualTimeScheduler`. Experts only. The signature of
  23. * this constructor is likely to change in the long run.
  24. *
  25. * @param schedulerActionCtor The type of Action to initialize when initializing actions during scheduling.
  26. * @param maxFrames The maximum number of frames to process before stopping. Used to prevent endless flush cycles.
  27. */
  28. constructor(schedulerActionCtor: typeof AsyncAction = VirtualAction as any, public maxFrames: number = Infinity) {
  29. super(schedulerActionCtor, () => this.frame);
  30. }
  31. /**
  32. * Prompt the Scheduler to execute all of its queued actions, therefore
  33. * clearing its queue.
  34. */
  35. public flush(): void {
  36. const { actions, maxFrames } = this;
  37. let error: any;
  38. let action: AsyncAction<any> | undefined;
  39. while ((action = actions[0]) && action.delay <= maxFrames) {
  40. actions.shift();
  41. this.frame = action.delay;
  42. if ((error = action.execute(action.state, action.delay))) {
  43. break;
  44. }
  45. }
  46. if (error) {
  47. while ((action = actions.shift())) {
  48. action.unsubscribe();
  49. }
  50. throw error;
  51. }
  52. }
  53. }
  54. export class VirtualAction<T> extends AsyncAction<T> {
  55. protected active: boolean = true;
  56. constructor(
  57. protected scheduler: VirtualTimeScheduler,
  58. protected work: (this: SchedulerAction<T>, state?: T) => void,
  59. protected index: number = (scheduler.index += 1)
  60. ) {
  61. super(scheduler, work);
  62. this.index = scheduler.index = index;
  63. }
  64. public schedule(state?: T, delay: number = 0): Subscription {
  65. if (Number.isFinite(delay)) {
  66. if (!this.id) {
  67. return super.schedule(state, delay);
  68. }
  69. this.active = false;
  70. // If an action is rescheduled, we save allocations by mutating its state,
  71. // pushing it to the end of the scheduler queue, and recycling the action.
  72. // But since the VirtualTimeScheduler is used for testing, VirtualActions
  73. // must be immutable so they can be inspected later.
  74. const action = new VirtualAction(this.scheduler, this.work);
  75. this.add(action);
  76. return action.schedule(state, delay);
  77. } else {
  78. // If someone schedules something with Infinity, it'll never happen. So we
  79. // don't even schedule it.
  80. return Subscription.EMPTY;
  81. }
  82. }
  83. protected requestAsyncId(scheduler: VirtualTimeScheduler, id?: any, delay: number = 0): TimerHandle {
  84. this.delay = scheduler.frame + delay;
  85. const { actions } = scheduler;
  86. actions.push(this);
  87. (actions as Array<VirtualAction<T>>).sort(VirtualAction.sortActions);
  88. return 1;
  89. }
  90. protected recycleAsyncId(scheduler: VirtualTimeScheduler, id?: any, delay: number = 0): TimerHandle | undefined {
  91. return undefined;
  92. }
  93. protected _execute(state: T, delay: number): any {
  94. if (this.active === true) {
  95. return super._execute(state, delay);
  96. }
  97. }
  98. private static sortActions<T>(a: VirtualAction<T>, b: VirtualAction<T>) {
  99. if (a.delay === b.delay) {
  100. if (a.index === b.index) {
  101. return 0;
  102. } else if (a.index > b.index) {
  103. return 1;
  104. } else {
  105. return -1;
  106. }
  107. } else if (a.delay > b.delay) {
  108. return 1;
  109. } else {
  110. return -1;
  111. }
  112. }
  113. }