Fix index lookup for minimal and maximal favorite id

This commit is contained in:
arne 2025-11-20 23:21:38 +01:00
commit 7d359c2ed7
2 changed files with 4 additions and 15 deletions

View file

@ -417,20 +417,9 @@
"Returns a promise which resolves to the smallest or largest internal post id.
This is useful to continue interrupted paginated requests."
[max-or-min]
; this may look like a horribly inefficient way to get these numbers, and it is!
; there was an earlier version that simply used an index on :internal_id and
; retrieved the first result in ascending or descending order; however, the
; result that was returned was unexpected and unreliable. this may very well
; have been my fault, because i'm only learning how the indexeddb api works as
; i implement all of this here.
(let [xform (comp (keep (j/get :internal_id)))
rf (if (= max-or-min :min) min max)
init (if (= max-or-min :min) js/Number.POSITIVE_INFINITY js/Number.NEGATIVE_INFINITY)]
(. (->> (db/open-cursor! ::db/posts ::db/all)
(db/transduce-cursor xform rf init))
(then (fn [result]
(when-not (infinite? result)
result))))))
(->> (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)))))
(defn fetch-more-posts! [e]
(.preventDefault e)

View file

@ -114,7 +114,7 @@
([db store-id key-range {:keys [direction index]}]
(let [store (open-store db store-id "readonly")
key-range (if (= key-range ::all)
(js/IDBKeyRange.lowerBound "")
(js/IDBKeyRange.lowerBound js/Number.NEGATIVE_INFINITY)
key-range)
direction ({:asc "next" :desc "prev"} direction direction)]
(if index