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