747b7a13963099c6f960b050122c79fd097c80a86faa338b18234c4a51668c6f34da72c33c45143c050672dd79a58ddf93b48ea84c323326a05c93a9be9e58 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Subscriber } from '../Subscriber';
  2. import { MonoTypeOperatorFunction, ObservableInput } from '../types';
  3. import { operate } from '../util/lift';
  4. import { innerFrom } from '../observable/innerFrom';
  5. import { createOperatorSubscriber } from './OperatorSubscriber';
  6. /**
  7. * Ignores source values for a duration determined by another Observable, then
  8. * emits the most recent value from the source Observable, then repeats this
  9. * process.
  10. *
  11. * <span class="informal">It's like {@link auditTime}, but the silencing
  12. * duration is determined by a second Observable.</span>
  13. *
  14. * ![](audit.svg)
  15. *
  16. * `audit` is similar to `throttle`, but emits the last value from the silenced
  17. * time window, instead of the first value. `audit` emits the most recent value
  18. * from the source Observable on the output Observable as soon as its internal
  19. * timer becomes disabled, and ignores source values while the timer is enabled.
  20. * Initially, the timer is disabled. As soon as the first source value arrives,
  21. * the timer is enabled by calling the `durationSelector` function with the
  22. * source value, which returns the "duration" Observable. When the duration
  23. * Observable emits a value, the timer is disabled, then the most
  24. * recent source value is emitted on the output Observable, and this process
  25. * repeats for the next source value.
  26. *
  27. * ## Example
  28. *
  29. * Emit clicks at a rate of at most one click per second
  30. *
  31. * ```ts
  32. * import { fromEvent, audit, interval } from 'rxjs';
  33. *
  34. * const clicks = fromEvent(document, 'click');
  35. * const result = clicks.pipe(audit(ev => interval(1000)));
  36. * result.subscribe(x => console.log(x));
  37. * ```
  38. *
  39. * @see {@link auditTime}
  40. * @see {@link debounce}
  41. * @see {@link delayWhen}
  42. * @see {@link sample}
  43. * @see {@link throttle}
  44. *
  45. * @param durationSelector A function
  46. * that receives a value from the source Observable, for computing the silencing
  47. * duration, returned as an Observable or a Promise.
  48. * @return A function that returns an Observable that performs rate-limiting of
  49. * emissions from the source Observable.
  50. */
  51. export function audit<T>(durationSelector: (value: T) => ObservableInput<any>): MonoTypeOperatorFunction<T> {
  52. return operate((source, subscriber) => {
  53. let hasValue = false;
  54. let lastValue: T | null = null;
  55. let durationSubscriber: Subscriber<any> | null = null;
  56. let isComplete = false;
  57. const endDuration = () => {
  58. durationSubscriber?.unsubscribe();
  59. durationSubscriber = null;
  60. if (hasValue) {
  61. hasValue = false;
  62. const value = lastValue!;
  63. lastValue = null;
  64. subscriber.next(value);
  65. }
  66. isComplete && subscriber.complete();
  67. };
  68. const cleanupDuration = () => {
  69. durationSubscriber = null;
  70. isComplete && subscriber.complete();
  71. };
  72. source.subscribe(
  73. createOperatorSubscriber(
  74. subscriber,
  75. (value) => {
  76. hasValue = true;
  77. lastValue = value;
  78. if (!durationSubscriber) {
  79. innerFrom(durationSelector(value)).subscribe(
  80. (durationSubscriber = createOperatorSubscriber(subscriber, endDuration, cleanupDuration))
  81. );
  82. }
  83. },
  84. () => {
  85. isComplete = true;
  86. (!hasValue || !durationSubscriber || durationSubscriber.closed) && subscriber.complete();
  87. }
  88. )
  89. );
  90. });
  91. }