diff --git a/src/computersandblues/lodestone/app.cljs b/src/computersandblues/lodestone/app.cljs index 945705c..15ba487 100644 --- a/src/computersandblues/lodestone/app.cljs +++ b/src/computersandblues/lodestone/app.cljs @@ -156,10 +156,8 @@ (declare refresh-displayed-posts!) (defn- fetch-application-settings [] - (-> (db/open-cursor! ::db/application db/all) - (db/transduce-cursor (comp (take 1) - (map #(js->clj % :keywordize-keys true))) - (fn [_ x] x)))) + (->> (db/open-cursor! ::db/application db/all) + (db/first-result (map #(js->clj % :keywordize-keys true))))) (defn setup-application! "Handles Mastodon application setup on the client side" @@ -377,8 +375,8 @@ refresh-id (js/Date.now)] (swap! state update-in [:section/posts :loading] conj refresh-id) (. (promise-all [(db/count! ::db/posts) - (-> (db/open-cursor! ::db/posts ::db/post-created-at db/all "prev") - (db/transduce-cursor xform))]) + (->> (db/open-cursor! ::db/posts ::db/post-created-at db/all "prev") + (db/transduce-cursor xform))]) (then (fn [[total displayed-posts]] (swap! state update :section/posts #(-> (assoc % :total total) (assoc :displayed-posts displayed-posts) @@ -407,12 +405,10 @@ (paginate-posts! (merge defaults opts)))) (defn- internal-post-id [max-or-min] - (-> (db/open-cursor! ::db/posts ::db/post-created-at db/all (if (= max-or-min :min) - "next" - "prev")) - (db/transduce-cursor (comp (keep (j/get :internal_id)) - (take 1)) - (fn [_ x] x)))) + (->> (db/open-cursor! ::db/posts ::db/post-created-at db/all (if (= max-or-min :min) + "next" + "prev")) + (db/first-result (keep (j/get :internal_id))))) (defn fetch-more-posts! [e] (.preventDefault e) diff --git a/src/computersandblues/lodestone/database.cljs b/src/computersandblues/lodestone/database.cljs index 0ec3905..119bc6a 100644 --- a/src/computersandblues/lodestone/database.cljs +++ b/src/computersandblues/lodestone/database.cljs @@ -103,13 +103,13 @@ Takes a transducer `xform`, a reducing function `rf` and an initial `init`. If no `init` is given, it will default to `(rf)`. If no `rf` is given, the resulting value will be a persistent vector containing the result of all steps." - ([cursor-req xform] + ([xform cursor-req] ; optimization: work with a transient vector before returning the final result - (-> (transduce-cursor cursor-req xform conj! (transient [])) + (-> (transduce-cursor xform conj! (transient []) cursor-req) (.then persistent!))) - ([cursor-req xform rf] - (transduce-cursor cursor-req xform rf (rf))) - ([cursor-req xform rf init] + ([xform rf cursor-req] + (transduce-cursor xform rf (rf) cursor-req)) + ([xform rf init cursor-req] (let [result (volatile! init) xform (xform rf)] (js/Promise. @@ -121,14 +121,15 @@ ; to avoid unnecessary conversion costs. (let [step (xform @result (.-value cursor))] (if (reduced? step) - (do - (vreset! result @step) - (resolve @result)) + (resolve @step) (do (vreset! result step) (.continue cursor)))) (resolve @result))))))))) +(defn first-result [xform cursor-req] + (transduce-cursor (comp xform (take 1)) (fn [_ x] x) cursor-req)) + (def all (js/IDBKeyRange.lowerBound "")) (comment