Debian Codegraph @

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

raw
1 import { AsyncAction } from './AsyncAction';
2 import { AsapScheduler } from './AsapScheduler';
3 import { SchedulerAction } from '../types';
4 import { immediateProvider } from './immediateProvider';
5 import { TimerHandle } from './timerHandle';
6
7 export class AsapAction<T> extends AsyncAction<T> {
8 constructor(protected scheduler: AsapScheduler, protected work: (this: SchedulerAction<T>, state?: T) => void) {
9 super(scheduler, work);
10 }
11
12 protected requestAsyncId(scheduler: AsapScheduler, id?: TimerHandle, delay: number = 0): TimerHandle {
13 // If delay is greater than 0, request as an async action.
14 if (delay !== null && delay > 0) {
15 return super.requestAsyncId(scheduler, id, delay);
16 }
17 // Push the action to the end of the scheduler queue.
18 scheduler.actions.push(this);
19 // If a microtask has already been scheduled, don't schedule another
20 // one. If a microtask hasn't been scheduled yet, schedule one now. Return
21 // the current scheduled microtask id.
22 return scheduler._scheduled || (scheduler._scheduled = immediateProvider.setImmediate(scheduler.flush.bind(scheduler, undefined)));
23 }
24
25 protected recycleAsyncId(scheduler: AsapScheduler, id?: TimerHandle, delay: number = 0): TimerHandle | undefined {
26 // If delay exists and is greater than 0, or if the delay is null (the
27 // action wasn't rescheduled) but was originally scheduled as an async
28 // action, then recycle as an async action.
29 if (delay != null ? delay > 0 : this.delay > 0) {
30 return super.recycleAsyncId(scheduler, id, delay);
31 }
32 // If the scheduler queue has no remaining actions with the same async id,
33 // cancel the requested microtask and set the scheduled flag to undefined
34 // so the next AsapAction will request its own.
35 const { actions } = scheduler;
36 if (id != null && actions[actions.length - 1]?.id !== id) {
37 immediateProvider.clearImmediate(id);
38 if (scheduler._scheduled === id) {
39 scheduler._scheduled = undefined;
40 }
41 }
42 // Return undefined so the action knows to request a new async id if it's rescheduled.
43 return undefined;
44 }
45 }