Debian Codegraph @

node/rxjs/src/internal/scheduler/VirtualTimeScheduler.ts

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