From aff235b9a9e2be702895d7431ce11ed44e141e98 Mon Sep 17 00:00:00 2001 From: arne Date: Sat, 22 Nov 2025 00:13:21 +0100 Subject: [PATCH] Clean up `fetch-more-posts!` --- src/computersandblues/lodestone/app.cljs | 44 ++++++++++++------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/computersandblues/lodestone/app.cljs b/src/computersandblues/lodestone/app.cljs index b97af00..cffb1f7 100644 --- a/src/computersandblues/lodestone/app.cljs +++ b/src/computersandblues/lodestone/app.cljs @@ -448,7 +448,7 @@ ; this returns a promise, but we don't care if these updates happen in sequence (db/put! ::db/posts (cond-> post ; these IDs are internal server ids and it looks like - ; they are not returned in any response; they are + ; they are not returned in any response body; they are ; required for pagination, so we're storing them to be ; able to abort and continue pagination if we want or ; if outer circumstances decide so (for example if the @@ -461,29 +461,29 @@ (swap! state assoc-in [:section/posts :last-update] (now)))}] (paginate-posts! (merge defaults opts)))) -(defn- internal-post-id - "Returns a promise which resolves to the smallest or largest internal post id. - This is useful to continue interrupted paginated requests." - [max-or-min] - (let [posts-cursor (db/open-cursor ::db/posts ::db/all {:index ::db/post-internal-id - :direction (if (= max-or-min :min) :asc :desc)})] - (db/first-result (keep (j/get :internal_id)) posts-cursor))) +(defn- bound + "Returns a promise which resolves to the smallest or largest value returned by + an index." + [store-id index xform max-or-min] + (let [cursor (db/open-cursor store-id ::db/all {:index index + :direction (if (= max-or-min :min) + :asc + :desc)})] + (db/first-result xform cursor))) (defn- fetch-more-posts! [e] - (.preventDefault e) - (. (promise-all [(fetch-application-settings) (internal-post-id :min) (internal-post-id :max)]) - (then (fn [[application min-id max-id]] - (when max-id - (fetch-posts! {:instance-url (:instance_url application) - :bearer-token (:bearer_token application) - :min-id max-id})) - (when min-id - (fetch-posts! {:instance-url (:instance_url application) - :bearer-token (:bearer_token application) - :max-id min-id})) - (when-not (or min-id max-id) - (fetch-posts! {:instance-url (:instance_url application) - :bearer-token (:bearer_token application)})))))) + (let [internal-id-bound (partial bound ::db/posts ::db/post-internal-id (keep (j/get :internal_id)))] + (.preventDefault e) + (. (promise-all [(fetch-application-settings) (internal-id-bound :min) (internal-id-bound :max)]) + (then (fn [[application min-id max-id]] + (let [opts {:instance-url (:instance_url application) + :bearer-token (:bearer_token application)}] + (when max-id + (fetch-posts! (assoc opts :min-id max-id))) + (when min-id + (fetch-posts! (assoc opts :max-id min-id))) + (when-not (or min-id max-id) + (fetch-posts! opts)))))))) (defn- disconnect-account! [e] (.preventDefault e)