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

timeInterval.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Observable } from '../Observable';
  2. import { async } from '../scheduler/async';
  3. import { SchedulerLike, OperatorFunction } from '../types';
  4. import { scan } from './scan';
  5. import { defer } from '../observable/defer';
  6. import { map } from './map';
  7. /**
  8. *
  9. * Emits an object containing the current value, and the time that has
  10. * passed between emitting the current value and the previous value, which is
  11. * calculated by using the provided `scheduler`'s `now()` method to retrieve
  12. * the current time at each emission, then calculating the difference. The `scheduler`
  13. * defaults to {@link asyncScheduler}, so by default, the `interval` will be in
  14. * milliseconds.
  15. *
  16. * <span class="informal">Convert an Observable that emits items into one that
  17. * emits indications of the amount of time elapsed between those emissions.</span>
  18. *
  19. * ![](timeinterval.png)
  20. *
  21. * ## Examples
  22. * Emit inteval between current value with the last value
  23. *
  24. * ```ts
  25. * const seconds = interval(1000);
  26. *
  27. * seconds.pipe(timeInterval())
  28. * .subscribe(
  29. * value => console.log(value),
  30. * err => console.log(err),
  31. * );
  32. *
  33. * seconds.pipe(timeout(900))
  34. * .subscribe(
  35. * value => console.log(value),
  36. * err => console.log(err),
  37. * );
  38. *
  39. * // NOTE: The values will never be this precise,
  40. * // intervals created with `interval` or `setInterval`
  41. * // are non-deterministic.
  42. *
  43. * // {value: 0, interval: 1000}
  44. * // {value: 1, interval: 1000}
  45. * // {value: 2, interval: 1000}
  46. * ```
  47. *
  48. * @param {SchedulerLike} [scheduler] Scheduler used to get the current time.
  49. * @return {Observable<{ interval: number, value: T }>} Observable that emit infomation about value and interval
  50. * @method timeInterval
  51. */
  52. export function timeInterval<T>(scheduler: SchedulerLike = async): OperatorFunction<T, TimeInterval<T>> {
  53. return (source: Observable<T>) => defer(() => {
  54. return source.pipe(
  55. // TODO(benlesh): correct these typings.
  56. scan(
  57. ({ current }, value) => ({ value, current: scheduler.now(), last: current }),
  58. { current: scheduler.now(), value: undefined, last: undefined }
  59. ) as any,
  60. map<any, TimeInterval<T>>(({ current, last, value }) => new TimeInterval(value, current - last)),
  61. );
  62. });
  63. }
  64. // TODO(benlesh): make this an interface, export the interface, but not the implemented class,
  65. // there's no reason users should be manually creating this type.
  66. /**
  67. * @deprecated exposed API, use as interface only.
  68. */
  69. export class TimeInterval<T> {
  70. constructor(public value: T, public interval: number) {}
  71. }