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
(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)