source: trip-planner-front/node_modules/rxjs/src/internal/scheduler/AsyncScheduler.ts@ 6fe77af

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

initial commit

  • Property mode set to 100644
File size: 1.9 KB
Line 
1import { Scheduler } from '../Scheduler';
2import { Action } from './Action';
3import { AsyncAction } from './AsyncAction';
4import { SchedulerAction } from '../types';
5import { Subscription } from '../Subscription';
6
7export class AsyncScheduler extends Scheduler {
8 public static delegate?: Scheduler;
9 public actions: Array<AsyncAction<any>> = [];
10 /**
11 * A flag to indicate whether the Scheduler is currently executing a batch of
12 * queued actions.
13 * @type {boolean}
14 * @deprecated internal use only
15 */
16 public active: boolean = false;
17 /**
18 * An internal ID used to track the latest asynchronous task such as those
19 * coming from `setTimeout`, `setInterval`, `requestAnimationFrame`, and
20 * others.
21 * @type {any}
22 * @deprecated internal use only
23 */
24 public scheduled: any = undefined;
25
26 constructor(SchedulerAction: typeof Action,
27 now: () => number = Scheduler.now) {
28 super(SchedulerAction, () => {
29 if (AsyncScheduler.delegate && AsyncScheduler.delegate !== this) {
30 return AsyncScheduler.delegate.now();
31 } else {
32 return now();
33 }
34 });
35 }
36
37 public schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay: number = 0, state?: T): Subscription {
38 if (AsyncScheduler.delegate && AsyncScheduler.delegate !== this) {
39 return AsyncScheduler.delegate.schedule(work, delay, state);
40 } else {
41 return super.schedule(work, delay, state);
42 }
43 }
44
45 public flush(action: AsyncAction<any>): void {
46
47 const {actions} = this;
48
49 if (this.active) {
50 actions.push(action);
51 return;
52 }
53
54 let error: any;
55 this.active = true;
56
57 do {
58 if (error = action.execute(action.state, action.delay)) {
59 break;
60 }
61 } while (action = actions.shift()); // exhaust the scheduler queue
62
63 this.active = false;
64
65 if (error) {
66 while (action = actions.shift()) {
67 action.unsubscribe();
68 }
69 throw error;
70 }
71 }
72}
Note: See TracBrowser for help on using the repository browser.