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

sampleTime.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Observable } from '../Observable';
  2. import { Operator } from '../Operator';
  3. import { Subscriber } from '../Subscriber';
  4. import { async } from '../scheduler/async';
  5. import { MonoTypeOperatorFunction, SchedulerAction, SchedulerLike, TeardownLogic } from '../types';
  6. /**
  7. * Emits the most recently emitted value from the source Observable within
  8. * periodic time intervals.
  9. *
  10. * <span class="informal">Samples the source Observable at periodic time
  11. * intervals, emitting what it samples.</span>
  12. *
  13. * ![](sampleTime.png)
  14. *
  15. * `sampleTime` periodically looks at the source Observable and emits whichever
  16. * value it has most recently emitted since the previous sampling, unless the
  17. * source has not emitted anything since the previous sampling. The sampling
  18. * happens periodically in time every `period` milliseconds (or the time unit
  19. * defined by the optional `scheduler` argument). The sampling starts as soon as
  20. * the output Observable is subscribed.
  21. *
  22. * ## Example
  23. * Every second, emit the most recent click at most once
  24. * ```ts
  25. * import { fromEvent } from 'rxjs';
  26. * import { sampleTime } from 'rxjs/operators';
  27. *
  28. * const clicks = fromEvent(document, 'click');
  29. * const result = clicks.pipe(sampleTime(1000));
  30. * result.subscribe(x => console.log(x));
  31. * ```
  32. *
  33. * @see {@link auditTime}
  34. * @see {@link debounceTime}
  35. * @see {@link delay}
  36. * @see {@link sample}
  37. * @see {@link throttleTime}
  38. *
  39. * @param {number} period The sampling period expressed in milliseconds or the
  40. * time unit determined internally by the optional `scheduler`.
  41. * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for
  42. * managing the timers that handle the sampling.
  43. * @return {Observable<T>} An Observable that emits the results of sampling the
  44. * values emitted by the source Observable at the specified time interval.
  45. * @method sampleTime
  46. * @owner Observable
  47. */
  48. export function sampleTime<T>(period: number, scheduler: SchedulerLike = async): MonoTypeOperatorFunction<T> {
  49. return (source: Observable<T>) => source.lift(new SampleTimeOperator(period, scheduler));
  50. }
  51. class SampleTimeOperator<T> implements Operator<T, T> {
  52. constructor(private period: number,
  53. private scheduler: SchedulerLike) {
  54. }
  55. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  56. return source.subscribe(new SampleTimeSubscriber(subscriber, this.period, this.scheduler));
  57. }
  58. }
  59. /**
  60. * We need this JSDoc comment for affecting ESDoc.
  61. * @ignore
  62. * @extends {Ignored}
  63. */
  64. class SampleTimeSubscriber<T> extends Subscriber<T> {
  65. lastValue: T;
  66. hasValue: boolean = false;
  67. constructor(destination: Subscriber<T>,
  68. private period: number,
  69. private scheduler: SchedulerLike) {
  70. super(destination);
  71. this.add(scheduler.schedule(dispatchNotification, period, { subscriber: this, period }));
  72. }
  73. protected _next(value: T) {
  74. this.lastValue = value;
  75. this.hasValue = true;
  76. }
  77. notifyNext() {
  78. if (this.hasValue) {
  79. this.hasValue = false;
  80. this.destination.next(this.lastValue);
  81. }
  82. }
  83. }
  84. function dispatchNotification<T>(this: SchedulerAction<any>, state: any) {
  85. let { subscriber, period } = state;
  86. subscriber.notifyNext();
  87. this.schedule(state, period);
  88. }