source: trip-planner-front/node_modules/rxjs/src/internal/scheduler/VirtualTimeScheduler.ts@ 571e0df

Last change on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1import { AsyncAction } from './AsyncAction';
2import { Subscription } from '../Subscription';
3import { AsyncScheduler } from './AsyncScheduler';
4import { SchedulerAction } from '../types';
5
6export class VirtualTimeScheduler extends AsyncScheduler {
7
8 protected static frameTimeFactor: number = 10;
9
10 public frame: number = 0;
11 public index: number = -1;
12
13 constructor(SchedulerAction: typeof AsyncAction = VirtualAction as any,
14 public maxFrames: number = Number.POSITIVE_INFINITY) {
15 super(SchedulerAction, () => this.frame);
16 }
17
18 /**
19 * Prompt the Scheduler to execute all of its queued actions, therefore
20 * clearing its queue.
21 * @return {void}
22 */
23 public flush(): void {
24
25 const {actions, maxFrames} = this;
26 let error: any, action: AsyncAction<any>;
27
28 while ((action = actions[0]) && action.delay <= maxFrames) {
29 actions.shift();
30 this.frame = action.delay;
31
32 if (error = action.execute(action.state, action.delay)) {
33 break;
34 }
35 }
36
37 if (error) {
38 while (action = actions.shift()) {
39 action.unsubscribe();
40 }
41 throw error;
42 }
43 }
44}
45
46/**
47 * We need this JSDoc comment for affecting ESDoc.
48 * @nodoc
49 */
50export class VirtualAction<T> extends AsyncAction<T> {
51
52 protected active: boolean = true;
53
54 constructor(protected scheduler: VirtualTimeScheduler,
55 protected work: (this: SchedulerAction<T>, state?: T) => void,
56 protected index: number = scheduler.index += 1) {
57 super(scheduler, work);
58 this.index = scheduler.index = index;
59 }
60
61 public schedule(state?: T, delay: number = 0): Subscription {
62 if (!this.id) {
63 return super.schedule(state, delay);
64 }
65 this.active = false;
66 // If an action is rescheduled, we save allocations by mutating its state,
67 // pushing it to the end of the scheduler queue, and recycling the action.
68 // But since the VirtualTimeScheduler is used for testing, VirtualActions
69 // must be immutable so they can be inspected later.
70 const action = new VirtualAction(this.scheduler, this.work);
71 this.add(action);
72 return action.schedule(state, delay);
73 }
74
75 protected requestAsyncId(scheduler: VirtualTimeScheduler, id?: any, delay: number = 0): any {
76 this.delay = scheduler.frame + delay;
77 const {actions} = scheduler;
78 actions.push(this);
79 (actions as Array<VirtualAction<T>>).sort(VirtualAction.sortActions);
80 return true;
81 }
82
83 protected recycleAsyncId(scheduler: VirtualTimeScheduler, id?: any, delay: number = 0): any {
84 return undefined;
85 }
86
87 protected _execute(state: T, delay: number): any {
88 if (this.active === true) {
89 return super._execute(state, delay);
90 }
91 }
92
93 public static sortActions<T>(a: VirtualAction<T>, b: VirtualAction<T>) {
94 if (a.delay === b.delay) {
95 if (a.index === b.index) {
96 return 0;
97 } else if (a.index > b.index) {
98 return 1;
99 } else {
100 return -1;
101 }
102 } else if (a.delay > b.delay) {
103 return 1;
104 } else {
105 return -1;
106 }
107 }
108}
Note: See TracBrowser for help on using the repository browser.