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

delayWhen.ts 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { Subscription } from '../Subscription';
  5. import { OuterSubscriber } from '../OuterSubscriber';
  6. import { InnerSubscriber } from '../InnerSubscriber';
  7. import { subscribeToResult } from '../util/subscribeToResult';
  8. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  9. /* tslint:disable:max-line-length */
  10. /** @deprecated In future versions, empty notifiers will no longer re-emit the source value on the output observable. */
  11. export function delayWhen<T>(delayDurationSelector: (value: T, index: number) => Observable<never>, subscriptionDelay?: Observable<any>): MonoTypeOperatorFunction<T>;
  12. export function delayWhen<T>(delayDurationSelector: (value: T, index: number) => Observable<any>, subscriptionDelay?: Observable<any>): MonoTypeOperatorFunction<T>;
  13. /* tslint:disable:max-line-length */
  14. /**
  15. * Delays the emission of items from the source Observable by a given time span
  16. * determined by the emissions of another Observable.
  17. *
  18. * <span class="informal">It's like {@link delay}, but the time span of the
  19. * delay duration is determined by a second Observable.</span>
  20. *
  21. * ![](delayWhen.png)
  22. *
  23. * `delayWhen` time shifts each emitted value from the source Observable by a
  24. * time span determined by another Observable. When the source emits a value,
  25. * the `delayDurationSelector` function is called with the source value as
  26. * argument, and should return an Observable, called the "duration" Observable.
  27. * The source value is emitted on the output Observable only when the duration
  28. * Observable emits a value or completes.
  29. * The completion of the notifier triggering the emission of the source value
  30. * is deprecated behavior and will be removed in future versions.
  31. *
  32. * Optionally, `delayWhen` takes a second argument, `subscriptionDelay`, which
  33. * is an Observable. When `subscriptionDelay` emits its first value or
  34. * completes, the source Observable is subscribed to and starts behaving like
  35. * described in the previous paragraph. If `subscriptionDelay` is not provided,
  36. * `delayWhen` will subscribe to the source Observable as soon as the output
  37. * Observable is subscribed.
  38. *
  39. * ## Example
  40. * Delay each click by a random amount of time, between 0 and 5 seconds
  41. * ```ts
  42. * import { fromEvent, interval } from 'rxjs';
  43. * import { delayWhen } from 'rxjs/operators';
  44. *
  45. * const clicks = fromEvent(document, 'click');
  46. * const delayedClicks = clicks.pipe(
  47. * delayWhen(event => interval(Math.random() * 5000)),
  48. * );
  49. * delayedClicks.subscribe(x => console.log(x));
  50. * ```
  51. *
  52. * @see {@link delay}
  53. * @see {@link throttle}
  54. * @see {@link throttleTime}
  55. * @see {@link debounce}
  56. * @see {@link debounceTime}
  57. * @see {@link sample}
  58. * @see {@link sampleTime}
  59. * @see {@link audit}
  60. * @see {@link auditTime}
  61. *
  62. * @param {function(value: T, index: number): Observable} delayDurationSelector A function that
  63. * returns an Observable for each value emitted by the source Observable, which
  64. * is then used to delay the emission of that item on the output Observable
  65. * until the Observable returned from this function emits a value.
  66. * @param {Observable} subscriptionDelay An Observable that triggers the
  67. * subscription to the source Observable once it emits any value.
  68. * @return {Observable} An Observable that delays the emissions of the source
  69. * Observable by an amount of time specified by the Observable returned by
  70. * `delayDurationSelector`.
  71. * @method delayWhen
  72. * @owner Observable
  73. */
  74. export function delayWhen<T>(delayDurationSelector: (value: T, index: number) => Observable<any>,
  75. subscriptionDelay?: Observable<any>): MonoTypeOperatorFunction<T> {
  76. if (subscriptionDelay) {
  77. return (source: Observable<T>) =>
  78. new SubscriptionDelayObservable(source, subscriptionDelay)
  79. .lift(new DelayWhenOperator(delayDurationSelector));
  80. }
  81. return (source: Observable<T>) => source.lift(new DelayWhenOperator(delayDurationSelector));
  82. }
  83. class DelayWhenOperator<T> implements Operator<T, T> {
  84. constructor(private delayDurationSelector: (value: T, index: number) => Observable<any>) {
  85. }
  86. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  87. return source.subscribe(new DelayWhenSubscriber(subscriber, this.delayDurationSelector));
  88. }
  89. }
  90. /**
  91. * We need this JSDoc comment for affecting ESDoc.
  92. * @ignore
  93. * @extends {Ignored}
  94. */
  95. class DelayWhenSubscriber<T, R> extends OuterSubscriber<T, R> {
  96. private completed: boolean = false;
  97. private delayNotifierSubscriptions: Array<Subscription> = [];
  98. private index: number = 0;
  99. constructor(destination: Subscriber<T>,
  100. private delayDurationSelector: (value: T, index: number) => Observable<any>) {
  101. super(destination);
  102. }
  103. notifyNext(outerValue: T, _innerValue: any,
  104. _outerIndex: number, _innerIndex: number,
  105. innerSub: InnerSubscriber<T, R>): void {
  106. this.destination.next!(outerValue);
  107. this.removeSubscription(innerSub);
  108. this.tryComplete();
  109. }
  110. notifyError(error: any, innerSub: InnerSubscriber<T, R>): void {
  111. this._error(error);
  112. }
  113. notifyComplete(innerSub: InnerSubscriber<T, R>): void {
  114. const value = this.removeSubscription(innerSub);
  115. if (value) {
  116. this.destination.next!(value);
  117. }
  118. this.tryComplete();
  119. }
  120. protected _next(value: T): void {
  121. const index = this.index++;
  122. try {
  123. const delayNotifier = this.delayDurationSelector(value, index);
  124. if (delayNotifier) {
  125. this.tryDelay(delayNotifier, value);
  126. }
  127. } catch (err) {
  128. this.destination.error!(err);
  129. }
  130. }
  131. protected _complete(): void {
  132. this.completed = true;
  133. this.tryComplete();
  134. this.unsubscribe();
  135. }
  136. private removeSubscription(subscription: InnerSubscriber<T, R>): T {
  137. subscription.unsubscribe();
  138. const subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
  139. if (subscriptionIdx !== -1) {
  140. this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
  141. }
  142. return subscription.outerValue;
  143. }
  144. private tryDelay(delayNotifier: Observable<any>, value: T): void {
  145. const notifierSubscription = subscribeToResult(this, delayNotifier, value);
  146. if (notifierSubscription && !notifierSubscription.closed) {
  147. const destination = this.destination as Subscription;
  148. destination.add(notifierSubscription);
  149. this.delayNotifierSubscriptions.push(notifierSubscription);
  150. }
  151. }
  152. private tryComplete(): void {
  153. if (this.completed && this.delayNotifierSubscriptions.length === 0) {
  154. this.destination.complete!();
  155. }
  156. }
  157. }
  158. /**
  159. * We need this JSDoc comment for affecting ESDoc.
  160. * @ignore
  161. * @extends {Ignored}
  162. */
  163. class SubscriptionDelayObservable<T> extends Observable<T> {
  164. constructor(public source: Observable<T>, private subscriptionDelay: Observable<any>) {
  165. super();
  166. }
  167. /** @deprecated This is an internal implementation detail, do not use. */
  168. _subscribe(subscriber: Subscriber<T>) {
  169. this.subscriptionDelay.subscribe(new SubscriptionDelaySubscriber(subscriber, this.source));
  170. }
  171. }
  172. /**
  173. * We need this JSDoc comment for affecting ESDoc.
  174. * @ignore
  175. * @extends {Ignored}
  176. */
  177. class SubscriptionDelaySubscriber<T> extends Subscriber<T> {
  178. private sourceSubscribed: boolean = false;
  179. constructor(private parent: Subscriber<T>, private source: Observable<T>) {
  180. super();
  181. }
  182. protected _next(unused: any) {
  183. this.subscribeToSource();
  184. }
  185. protected _error(err: any) {
  186. this.unsubscribe();
  187. this.parent.error(err);
  188. }
  189. protected _complete() {
  190. this.unsubscribe();
  191. this.subscribeToSource();
  192. }
  193. private subscribeToSource(): void {
  194. if (!this.sourceSubscribed) {
  195. this.sourceSubscribed = true;
  196. this.unsubscribe();
  197. this.source.subscribe(this.parent);
  198. }
  199. }
  200. }