620c6be7dc0863a3cd158c71620d76e5cac99b0807c45bdac93df6742f581e1909e0ea24e6f43a6bbbcebbe07b014b19bccebfa8d24f97ebd5b2a5db6ceee4 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Observable } from '../Observable';
  2. import { SchedulerLike } from '../types';
  3. /**
  4. * Creates an observable that will wait for a specified time period, or exact date, before
  5. * emitting the number 0.
  6. *
  7. * <span class="informal">Used to emit a notification after a delay.</span>
  8. *
  9. * This observable is useful for creating delays in code, or racing against other values
  10. * for ad-hoc timeouts.
  11. *
  12. * The `delay` is specified by default in milliseconds, however providing a custom scheduler could
  13. * create a different behavior.
  14. *
  15. * ## Examples
  16. *
  17. * Wait 3 seconds and start another observable
  18. *
  19. * You might want to use `timer` to delay subscription to an
  20. * observable by a set amount of time. Here we use a timer with
  21. * {@link concatMapTo} or {@link concatMap} in order to wait
  22. * a few seconds and start a subscription to a source.
  23. *
  24. * ```ts
  25. * import { of, timer, concatMap } from 'rxjs';
  26. *
  27. * // This could be any observable
  28. * const source = of(1, 2, 3);
  29. *
  30. * timer(3000)
  31. * .pipe(concatMap(() => source))
  32. * .subscribe(console.log);
  33. * ```
  34. *
  35. * Take all values until the start of the next minute
  36. *
  37. * Using a `Date` as the trigger for the first emission, you can
  38. * do things like wait until midnight to fire an event, or in this case,
  39. * wait until a new minute starts (chosen so the example wouldn't take
  40. * too long to run) in order to stop watching a stream. Leveraging
  41. * {@link takeUntil}.
  42. *
  43. * ```ts
  44. * import { interval, takeUntil, timer } from 'rxjs';
  45. *
  46. * // Build a Date object that marks the
  47. * // next minute.
  48. * const currentDate = new Date();
  49. * const startOfNextMinute = new Date(
  50. * currentDate.getFullYear(),
  51. * currentDate.getMonth(),
  52. * currentDate.getDate(),
  53. * currentDate.getHours(),
  54. * currentDate.getMinutes() + 1
  55. * );
  56. *
  57. * // This could be any observable stream
  58. * const source = interval(1000);
  59. *
  60. * const result = source.pipe(
  61. * takeUntil(timer(startOfNextMinute))
  62. * );
  63. *
  64. * result.subscribe(console.log);
  65. * ```
  66. *
  67. * ### Known Limitations
  68. *
  69. * - The {@link asyncScheduler} uses `setTimeout` which has limitations for how far in the future it can be scheduled.
  70. *
  71. * - If a `scheduler` is provided that returns a timestamp other than an epoch from `now()`, and
  72. * a `Date` object is passed to the `dueTime` argument, the calculation for when the first emission
  73. * should occur will be incorrect. In this case, it would be best to do your own calculations
  74. * ahead of time, and pass a `number` in as the `dueTime`.
  75. *
  76. * @param due If a `number`, the amount of time in milliseconds to wait before emitting.
  77. * If a `Date`, the exact time at which to emit.
  78. * @param scheduler The scheduler to use to schedule the delay. Defaults to {@link asyncScheduler}.
  79. */
  80. export declare function timer(due: number | Date, scheduler?: SchedulerLike): Observable<0>;
  81. /**
  82. * Creates an observable that starts an interval after a specified delay, emitting incrementing numbers -- starting at `0` --
  83. * on each interval after words.
  84. *
  85. * The `delay` and `intervalDuration` are specified by default in milliseconds, however providing a custom scheduler could
  86. * create a different behavior.
  87. *
  88. * ## Example
  89. *
  90. * ### Start an interval that starts right away
  91. *
  92. * Since {@link interval} waits for the passed delay before starting,
  93. * sometimes that's not ideal. You may want to start an interval immediately.
  94. * `timer` works well for this. Here we have both side-by-side so you can
  95. * see them in comparison.
  96. *
  97. * Note that this observable will never complete.
  98. *
  99. * ```ts
  100. * import { timer, interval } from 'rxjs';
  101. *
  102. * timer(0, 1000).subscribe(n => console.log('timer', n));
  103. * interval(1000).subscribe(n => console.log('interval', n));
  104. * ```
  105. *
  106. * ### Known Limitations
  107. *
  108. * - The {@link asyncScheduler} uses `setTimeout` which has limitations for how far in the future it can be scheduled.
  109. *
  110. * - If a `scheduler` is provided that returns a timestamp other than an epoch from `now()`, and
  111. * a `Date` object is passed to the `dueTime` argument, the calculation for when the first emission
  112. * should occur will be incorrect. In this case, it would be best to do your own calculations
  113. * ahead of time, and pass a `number` in as the `startDue`.
  114. * @param startDue If a `number`, is the time to wait before starting the interval.
  115. * If a `Date`, is the exact time at which to start the interval.
  116. * @param intervalDuration The delay between each value emitted in the interval. Passing a
  117. * negative number here will result in immediate completion after the first value is emitted, as though
  118. * no `intervalDuration` was passed at all.
  119. * @param scheduler The scheduler to use to schedule the delay. Defaults to {@link asyncScheduler}.
  120. */
  121. export declare function timer(startDue: number | Date, intervalDuration: number, scheduler?: SchedulerLike): Observable<number>;
  122. /**
  123. * @deprecated The signature allowing `undefined` to be passed for `intervalDuration` will be removed in v8. Use the `timer(dueTime, scheduler?)` signature instead.
  124. */
  125. export declare function timer(dueTime: number | Date, unused: undefined, scheduler?: SchedulerLike): Observable<0>;
  126. //# sourceMappingURL=timer.d.ts.map