Clean up fetch-more-posts!

This commit is contained in:
arne 2025-11-22 00:13:21 +01:00
commit aff235b9a9

View file

@ -448,7 +448,7 @@
; this returns a promise, but we don't care if these updates happen in sequence ; this returns a promise, but we don't care if these updates happen in sequence
(db/put! ::db/posts (cond-> post (db/put! ::db/posts (cond-> post
; these IDs are internal server ids and it looks like ; 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 ; required for pagination, so we're storing them to be
; able to abort and continue pagination if we want or ; able to abort and continue pagination if we want or
; if outer circumstances decide so (for example if the ; if outer circumstances decide so (for example if the
@ -461,29 +461,29 @@
(swap! state assoc-in [:section/posts :last-update] (now)))}] (swap! state assoc-in [:section/posts :last-update] (now)))}]
(paginate-posts! (merge defaults opts)))) (paginate-posts! (merge defaults opts))))
(defn- internal-post-id (defn- bound
"Returns a promise which resolves to the smallest or largest internal post id. "Returns a promise which resolves to the smallest or largest value returned by
This is useful to continue interrupted paginated requests." an index."
[max-or-min] [store-id index xform max-or-min]
(let [posts-cursor (db/open-cursor ::db/posts ::db/all {:index ::db/post-internal-id (let [cursor (db/open-cursor store-id ::db/all {:index index
:direction (if (= max-or-min :min) :asc :desc)})] :direction (if (= max-or-min :min)
(db/first-result (keep (j/get :internal_id)) posts-cursor))) :asc
:desc)})]
(db/first-result xform cursor)))
(defn- fetch-more-posts! [e] (defn- fetch-more-posts! [e]
(.preventDefault e) (let [internal-id-bound (partial bound ::db/posts ::db/post-internal-id (keep (j/get :internal_id)))]
(. (promise-all [(fetch-application-settings) (internal-post-id :min) (internal-post-id :max)]) (.preventDefault e)
(then (fn [[application min-id max-id]] (. (promise-all [(fetch-application-settings) (internal-id-bound :min) (internal-id-bound :max)])
(when max-id (then (fn [[application min-id max-id]]
(fetch-posts! {:instance-url (:instance_url application) (let [opts {:instance-url (:instance_url application)
:bearer-token (:bearer_token application) :bearer-token (:bearer_token application)}]
:min-id max-id})) (when max-id
(when min-id (fetch-posts! (assoc opts :min-id max-id)))
(fetch-posts! {:instance-url (:instance_url application) (when min-id
:bearer-token (:bearer_token application) (fetch-posts! (assoc opts :max-id min-id)))
:max-id min-id})) (when-not (or min-id max-id)
(when-not (or min-id max-id) (fetch-posts! opts))))))))
(fetch-posts! {:instance-url (:instance_url application)
:bearer-token (:bearer_token application)}))))))
(defn- disconnect-account! [e] (defn- disconnect-account! [e]
(.preventDefault e) (.preventDefault e)