Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

interval.ts 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Observable } from '../Observable';
  2. import { async } from '../scheduler/async';
  3. import { SchedulerAction, SchedulerLike } from '../types';
  4. import { isNumeric } from '../util/isNumeric';
  5. import { Subscriber } from '../Subscriber';
  6. /**
  7. * Creates an Observable that emits sequential numbers every specified
  8. * interval of time, on a specified {@link SchedulerLike}.
  9. *
  10. * <span class="informal">Emits incremental numbers periodically in time.
  11. * </span>
  12. *
  13. * ![](interval.png)
  14. *
  15. * `interval` returns an Observable that emits an infinite sequence of
  16. * ascending integers, with a constant interval of time of your choosing
  17. * between those emissions. The first emission is not sent immediately, but
  18. * only after the first period has passed. By default, this operator uses the
  19. * `async` {@link SchedulerLike} to provide a notion of time, but you may pass any
  20. * {@link SchedulerLike} to it.
  21. *
  22. * ## Example
  23. * Emits ascending numbers, one every second (1000ms) up to the number 3
  24. * ```ts
  25. * import { interval } from 'rxjs';
  26. * import { take } from 'rxjs/operators';
  27. *
  28. * const numbers = interval(1000);
  29. *
  30. * const takeFourNumbers = numbers.pipe(take(4));
  31. *
  32. * takeFourNumbers.subscribe(x => console.log('Next: ', x));
  33. *
  34. * // Logs:
  35. * // Next: 0
  36. * // Next: 1
  37. * // Next: 2
  38. * // Next: 3
  39. * ```
  40. *
  41. * @see {@link timer}
  42. * @see {@link delay}
  43. *
  44. * @param {number} [period=0] The interval size in milliseconds (by default)
  45. * or the time unit determined by the scheduler's clock.
  46. * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for scheduling
  47. * the emission of values, and providing a notion of "time".
  48. * @return {Observable} An Observable that emits a sequential number each time
  49. * interval.
  50. * @static true
  51. * @name interval
  52. * @owner Observable
  53. */
  54. export function interval(period = 0,
  55. scheduler: SchedulerLike = async): Observable<number> {
  56. if (!isNumeric(period) || period < 0) {
  57. period = 0;
  58. }
  59. if (!scheduler || typeof scheduler.schedule !== 'function') {
  60. scheduler = async;
  61. }
  62. return new Observable<number>(subscriber => {
  63. subscriber.add(
  64. scheduler.schedule(dispatch, period, { subscriber, counter: 0, period })
  65. );
  66. return subscriber;
  67. });
  68. }
  69. function dispatch(this: SchedulerAction<IntervalState>, state: IntervalState) {
  70. const { subscriber, counter, period } = state;
  71. subscriber.next(counter);
  72. this.schedule({ subscriber, counter: counter + 1, period }, period);
  73. }
  74. interface IntervalState {
  75. subscriber: Subscriber<number>;
  76. counter: number;
  77. period: number;
  78. }