aboutsummaryrefslogtreecommitdiffstats
path: root/node_modules/rxjs/src/internal/operators/defaultIfEmpty.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/defaultIfEmpty.ts
initial commit: scaffolding
Diffstat (limited to 'node_modules/rxjs/src/internal/operators/defaultIfEmpty.ts')
-rw-r--r--node_modules/rxjs/src/internal/operators/defaultIfEmpty.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/node_modules/rxjs/src/internal/operators/defaultIfEmpty.ts b/node_modules/rxjs/src/internal/operators/defaultIfEmpty.ts
new file mode 100644
index 0000000..9e0d277
--- /dev/null
+++ b/node_modules/rxjs/src/internal/operators/defaultIfEmpty.ts
@@ -0,0 +1,59 @@
+import { OperatorFunction } from '../types';
+import { operate } from '../util/lift';
+import { createOperatorSubscriber } from './OperatorSubscriber';
+
+/**
+ * Emits a given value if the source Observable completes without emitting any
+ * `next` value, otherwise mirrors the source Observable.
+ *
+ * <span class="informal">If the source Observable turns out to be empty, then
+ * this operator will emit a default value.</span>
+ *
+ * ![](defaultIfEmpty.png)
+ *
+ * `defaultIfEmpty` emits the values emitted by the source Observable or a
+ * specified default value if the source Observable is empty (completes without
+ * having emitted any `next` value).
+ *
+ * ## Example
+ *
+ * If no clicks happen in 5 seconds, then emit 'no clicks'
+ *
+ * ```ts
+ * import { fromEvent, takeUntil, interval, defaultIfEmpty } from 'rxjs';
+ *
+ * const clicks = fromEvent(document, 'click');
+ * const clicksBeforeFive = clicks.pipe(takeUntil(interval(5000)));
+ * const result = clicksBeforeFive.pipe(defaultIfEmpty('no clicks'));
+ * result.subscribe(x => console.log(x));
+ * ```
+ *
+ * @see {@link empty}
+ * @see {@link last}
+ *
+ * @param defaultValue The default value used if the source
+ * Observable is empty.
+ * @return A function that returns an Observable that emits either the
+ * specified `defaultValue` if the source Observable emits no items, or the
+ * values emitted by the source Observable.
+ */
+export function defaultIfEmpty<T, R>(defaultValue: R): OperatorFunction<T, T | R> {
+ return operate((source, subscriber) => {
+ let hasValue = false;
+ source.subscribe(
+ createOperatorSubscriber(
+ subscriber,
+ (value) => {
+ hasValue = true;
+ subscriber.next(value);
+ },
+ () => {
+ if (!hasValue) {
+ subscriber.next(defaultValue!);
+ }
+ subscriber.complete();
+ }
+ )
+ );
+ });
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage