dbab29780da75f3d5d91c98ec73bf8afa2933c3e3d5eb8071e6cb035ae33587f3ff02c98787c8ac3d26b6ee80614a90576757c68e879c678836c1030582bb3 2.0 KB

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