queue.ts
2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { QueueAction } from './QueueAction';
import { QueueScheduler } from './QueueScheduler';
/**
*
* Queue Scheduler
*
* <span class="informal">Put every next task on a queue, instead of executing it immediately</span>
*
* `queue` scheduler, when used with delay, behaves the same as {@link asyncScheduler} scheduler.
*
* When used without delay, it schedules given task synchronously - executes it right when
* it is scheduled. However when called recursively, that is when inside the scheduled task,
* another task is scheduled with queue scheduler, instead of executing immediately as well,
* that task will be put on a queue and wait for current one to finish.
*
* This means that when you execute task with `queue` scheduler, you are sure it will end
* before any other task scheduled with that scheduler will start.
*
* ## Examples
* Schedule recursively first, then do something
* ```ts
* import { queueScheduler } from 'rxjs';
*
* queueScheduler.schedule(() => {
* queueScheduler.schedule(() => console.log('second')); // will not happen now, but will be put on a queue
*
* console.log('first');
* });
*
* // Logs:
* // "first"
* // "second"
* ```
*
* Reschedule itself recursively
* ```ts
* import { queueScheduler } from 'rxjs';
*
* queueScheduler.schedule(function(state) {
* if (state !== 0) {
* console.log('before', state);
* this.schedule(state - 1); // `this` references currently executing Action,
* // which we reschedule with new state
* console.log('after', state);
* }
* }, 0, 3);
*
* // In scheduler that runs recursively, you would expect:
* // "before", 3
* // "before", 2
* // "before", 1
* // "after", 1
* // "after", 2
* // "after", 3
*
* // But with queue it logs:
* // "before", 3
* // "after", 3
* // "before", 2
* // "after", 2
* // "before", 1
* // "after", 1
* ```
*/
export const queueScheduler = new QueueScheduler(QueueAction);
/**
* @deprecated renamed. Use {@link queueScheduler}
*/
export const queue = queueScheduler;