From 6e1e600ec8b3f23d1a78b44093e0b20f9c6c78c8 Mon Sep 17 00:00:00 2001 From: Miguel de Benito Delgado Date: Sat, 21 Jun 2025 21:11:39 +0200 Subject: [PATCH] Add operators start-with and end-with --- src/beicon/v2/core.cljs | 16 ++++++++++++++++ src/beicon/v2/operators.cljs | 12 ++++++++++++ test/beicon/tests/v2_test.cljs | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/beicon/v2/core.cljs b/src/beicon/v2/core.cljs index 2c84f4f..c186fa0 100644 --- a/src/beicon/v2/core.cljs +++ b/src/beicon/v2/core.cljs @@ -421,6 +421,22 @@ [ob] (ops/pipe (ops/merge-all 1) ob)) +(defn start-with + "Emits the provided value(s) before any other emissions + from the source Observable." + [& args] + (let [values (butlast args) + ob (c/last args)] + (ops/pipe (apply ops/start-with values) ob))) + +(defn end-with + "Emits the provided value(s) after all other emissions + from the source Observable." + [& args] + (let [values (butlast args) + ob (c/last args)] + (ops/pipe (apply ops/end-with values) ob))) + (defn skip "Bypasses a specified number of elements in an observable sequence and then returns the remaining diff --git a/src/beicon/v2/operators.cljs b/src/beicon/v2/operators.cljs index 7d3b6d5..0645399 100644 --- a/src/beicon/v2/operators.cljs +++ b/src/beicon/v2/operators.cljs @@ -78,6 +78,18 @@ [f] (rx/concatMap #(f %2 %1))) +(def ^function start-with + "Returns an observable sequence that upon subscription emits the + specified values before it begins to emit the elements of the + source observable sequence." + rx/startWith) + +(def ^function end-with + "Returns an observable sequence that emits the elements of the + source observable and then emits the specified values after the + source completes." + rx/endWith) + (def ^function skip "Bypasses a specified number of elements in an observable sequence and then returns the remaining diff --git a/test/beicon/tests/v2_test.cljs b/test/beicon/tests/v2_test.cljs index d639f1f..204f26c 100644 --- a/test/beicon/tests/v2_test.cljs +++ b/test/beicon/tests/v2_test.cljs @@ -216,6 +216,24 @@ (drain! sample #(t/is (= % [2 3 4 5]))) (rx/on-end sample done)))) +(t/deftest observable-start-with + (t/async done + (let [nums (rx/from [1 2 3]) + s (rx/start-with 0 nums) + u (rx/start-with -2 -1 s)] + (t/is (rx/observable? u)) + (drain! u #(t/is (= % [-2 -1 0 1 2 3]))) + (rx/on-end u done)))) + +(t/deftest observable-end-with + (t/async done + (let [nums (rx/from [1 2 3]) + s (rx/end-with 4 (rx/from nums)) + u (rx/end-with 5 6 (rx/from s))] + (t/is (rx/observable? u)) + (drain! u #(t/is (= % [1 2 3 4 5 6]))) + (rx/on-end u done)))) + (t/deftest subject-as-ideref (t/async done (let [nums (rx/from [1 1 1 2 3 4 5])