source: trip-planner-front/node_modules/rxjs/src/internal/operators/sample.ts@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1import { Operator } from '../Operator';
2import { Observable } from '../Observable';
3import { Subscriber } from '../Subscriber';
4
5import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
6import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
7
8/**
9 * Emits the most recently emitted value from the source Observable whenever
10 * another Observable, the `notifier`, emits.
11 *
12 * <span class="informal">It's like {@link sampleTime}, but samples whenever
13 * the `notifier` Observable emits something.</span>
14 *
15 * ![](sample.png)
16 *
17 * Whenever the `notifier` Observable emits a value or completes, `sample`
18 * looks at the source Observable and emits whichever value it has most recently
19 * emitted since the previous sampling, unless the source has not emitted
20 * anything since the previous sampling. The `notifier` is subscribed to as soon
21 * as the output Observable is subscribed.
22 *
23 * ## Example
24 * On every click, sample the most recent "seconds" timer
25 * ```ts
26 * import { fromEvent, interval } from 'rxjs';
27 * import { sample } from 'rxjs/operators';
28 *
29 * const seconds = interval(1000);
30 * const clicks = fromEvent(document, 'click');
31 * const result = seconds.pipe(sample(clicks));
32 * result.subscribe(x => console.log(x));
33 * ```
34 *
35 * @see {@link audit}
36 * @see {@link debounce}
37 * @see {@link sampleTime}
38 * @see {@link throttle}
39 *
40 * @param {Observable<any>} notifier The Observable to use for sampling the
41 * source Observable.
42 * @return {Observable<T>} An Observable that emits the results of sampling the
43 * values emitted by the source Observable whenever the notifier Observable
44 * emits value or completes.
45 * @method sample
46 * @owner Observable
47 */
48export function sample<T>(notifier: Observable<any>): MonoTypeOperatorFunction<T> {
49 return (source: Observable<T>) => source.lift(new SampleOperator(notifier));
50}
51
52class SampleOperator<T> implements Operator<T, T> {
53 constructor(private notifier: Observable<any>) {
54 }
55
56 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
57 const sampleSubscriber = new SampleSubscriber(subscriber);
58 const subscription = source.subscribe(sampleSubscriber);
59 subscription.add(innerSubscribe(this.notifier, new SimpleInnerSubscriber(sampleSubscriber)));
60 return subscription;
61 }
62}
63
64/**
65 * We need this JSDoc comment for affecting ESDoc.
66 * @ignore
67 * @extends {Ignored}
68 */
69class SampleSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
70 private value?: T;
71 private hasValue: boolean = false;
72
73 protected _next(value: T) {
74 this.value = value;
75 this.hasValue = true;
76 }
77
78 notifyNext(): void {
79 this.emitValue();
80 }
81
82 notifyComplete(): void {
83 this.emitValue();
84 }
85
86 emitValue() {
87 if (this.hasValue) {
88 this.hasValue = false;
89 this.destination.next!(this.value!);
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.