aboutsummaryrefslogtreecommitdiffstats
path: root/node_modules/rxjs/src/internal/operators/sample.ts
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-06-28 17:26:46 -0700
committerPinapelz <yukais@pinapelz.com>2025-06-28 17:43:56 -0700
commite4fa1e69e7ebfb627c7198fd1a9881e9327ec4d4 (patch)
tree06284a538a6008eca75051399e47db4e5d50301c /node_modules/rxjs/src/internal/operators/sample.ts
initial commit: scaffolding
Diffstat (limited to 'node_modules/rxjs/src/internal/operators/sample.ts')
-rw-r--r--node_modules/rxjs/src/internal/operators/sample.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/node_modules/rxjs/src/internal/operators/sample.ts b/node_modules/rxjs/src/internal/operators/sample.ts
new file mode 100644
index 0000000..9008af2
--- /dev/null
+++ b/node_modules/rxjs/src/internal/operators/sample.ts
@@ -0,0 +1,72 @@
+import { innerFrom } from '../observable/innerFrom';
+import { MonoTypeOperatorFunction, ObservableInput } from '../types';
+import { operate } from '../util/lift';
+import { noop } from '../util/noop';
+import { createOperatorSubscriber } from './OperatorSubscriber';
+
+/**
+ * Emits the most recently emitted value from the source Observable whenever
+ * another Observable, the `notifier`, emits.
+ *
+ * <span class="informal">It's like {@link sampleTime}, but samples whenever
+ * the `notifier` `ObservableInput` emits something.</span>
+ *
+ * ![](sample.png)
+ *
+ * Whenever the `notifier` `ObservableInput` emits a value, `sample`
+ * looks at the source Observable and emits whichever value it has most recently
+ * emitted since the previous sampling, unless the source has not emitted
+ * anything since the previous sampling. The `notifier` is subscribed to as soon
+ * as the output Observable is subscribed.
+ *
+ * ## Example
+ *
+ * On every click, sample the most recent `seconds` timer
+ *
+ * ```ts
+ * import { fromEvent, interval, sample } from 'rxjs';
+ *
+ * const seconds = interval(1000);
+ * const clicks = fromEvent(document, 'click');
+ * const result = seconds.pipe(sample(clicks));
+ *
+ * result.subscribe(x => console.log(x));
+ * ```
+ *
+ * @see {@link audit}
+ * @see {@link debounce}
+ * @see {@link sampleTime}
+ * @see {@link throttle}
+ *
+ * @param notifier The `ObservableInput` to use for sampling the
+ * source Observable.
+ * @return A function that returns an Observable that emits the results of
+ * sampling the values emitted by the source Observable whenever the notifier
+ * Observable emits value or completes.
+ */
+export function sample<T>(notifier: ObservableInput<any>): MonoTypeOperatorFunction<T> {
+ return operate((source, subscriber) => {
+ let hasValue = false;
+ let lastValue: T | null = null;
+ source.subscribe(
+ createOperatorSubscriber(subscriber, (value) => {
+ hasValue = true;
+ lastValue = value;
+ })
+ );
+ innerFrom(notifier).subscribe(
+ createOperatorSubscriber(
+ subscriber,
+ () => {
+ if (hasValue) {
+ hasValue = false;
+ const value = lastValue!;
+ lastValue = null;
+ subscriber.next(value);
+ }
+ },
+ noop
+ )
+ );
+ });
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage