From 97965ce8b0f43fe68000eb549fc504f3ae8cfd87 Mon Sep 17 00:00:00 2001 From: arne Date: Sat, 22 Nov 2025 00:10:31 +0100 Subject: [PATCH] Separate `iterate-cursor` and `transduce-cursor` --- src/computersandblues/lodestone/database.cljs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/computersandblues/lodestone/database.cljs b/src/computersandblues/lodestone/database.cljs index 7f2beba..fba2868 100644 --- a/src/computersandblues/lodestone/database.cljs +++ b/src/computersandblues/lodestone/database.cljs @@ -131,6 +131,14 @@ (js/console.log :logging args) (apply f args))))) +(defn iterate-cursor [f cursor-req] + (.addEventListener cursor-req "success" + (fn [ev] + (if-let [cursor (-> ev .-target .-result)] + (do (f cursor) + (.continue cursor)) + (f))))) + (defn transduce-cursor "Allows to transduce over all values in a cursor. @@ -148,18 +156,14 @@ xform (xform rf)] (js/Promise. (fn [resolve _] - (.addEventListener cursor-req "success" - (fn [ev] - (if-let [cursor (-> ev .-target .-result)] - ; NOTE: each step will work with the raw js value - ; to avoid unnecessary conversion costs. - (let [step (xform @result (.-value cursor))] - (if (reduced? step) - (resolve @step) - (do - (vreset! result step) - (.continue cursor)))) - (resolve @result))))))))) + (iterate-cursor (fn [cursor] + (if cursor + (let [step (xform @result (.-value cursor))] + (if (reduced? step) + (resolve @step) + (vreset! result step))) + (resolve @result))) + cursor-req)))))) (defn first-result "Given a cursor, will return a promise that resolves to the first result.