diff --git a/src/cljs/airsonic_ui/components/library/subs.cljs b/src/cljs/airsonic_ui/components/library/subs.cljs index 7a0c908..ed5dcbe 100644 --- a/src/cljs/airsonic_ui/components/library/subs.cljs +++ b/src/cljs/airsonic_ui/components/library/subs.cljs @@ -9,10 +9,11 @@ (let [sorted-albums (->> (filter (fn [[[_ params] _]] (= kind (:type params))) responses) (sort-by (fn [[[_ params] _]] (:offset params))) - (map (comp :album val)))] + (keep (comp :album val)))] ;; NOTE: we concatenate this manually to avoid duplication; we have to do ;; this because fetch more than conf/albums-per-page per page, otherwise we ;; can't know whether to show a link to the next page + ;; FIXME: Somehow (last sorted-albums) is nil when (concat (mapcat (partial take conf/albums-per-page) (butlast sorted-albums)) (last sorted-albums)))) diff --git a/src/cljs/airsonic_ui/config.cljs b/src/cljs/airsonic_ui/config.cljs index 8e61c5c..0c86287 100644 --- a/src/cljs/airsonic_ui/config.cljs +++ b/src/cljs/airsonic_ui/config.cljs @@ -5,3 +5,4 @@ ;; how many covers are shown per page when browsing the library (def albums-per-page 20) +(def albums-prefetch-factor 5) diff --git a/src/cljs/airsonic_ui/routes.cljs b/src/cljs/airsonic_ui/routes.cljs index 0bffba9..78f030d 100644 --- a/src/cljs/airsonic_ui/routes.cljs +++ b/src/cljs/airsonic_ui/routes.cljs @@ -49,7 +49,7 @@ ;; we fetch more than just the albums needed for the current page so we can ;; page through it faster [:api/request "getAlbumList2" {:type kind - :size (* 3 conf/albums-per-page) + :size (* conf/albums-prefetch-factor conf/albums-per-page) :offset (* (dec (int page)) conf/albums-per-page)}]] [:routes/do-navigation [route-id {:kind "recent"} {:page 1}]])) diff --git a/test/cljs/airsonic_ui/components/library/subs_test.cljs b/test/cljs/airsonic_ui/components/library/subs_test.cljs index df818f1..01acef5 100644 --- a/test/cljs/airsonic_ui/components/library/subs_test.cljs +++ b/test/cljs/airsonic_ui/components/library/subs_test.cljs @@ -1,12 +1,25 @@ (ns airsonic-ui.components.library.subs-test (:require [cljs.test :refer-macros [deftest testing is]] + [airsonic-ui.config :as conf] [airsonic-ui.components.library.subs :as sub])) -(def responses {["getAlbumList2" {:type "recent" :offset 1}] {:album '(5 6 7 8)} - ["getAlbumList2" {:type "recent" :offset 0}] {:album '(1 2 3 4)} - ["getAlbumList2" {:type "newest" :offset 1}] {:album '(9 8 7 6)}}) +(defn stub-albums [offset] + (let [start (* offset conf/albums-per-page) + end (inc (+ start (* conf/albums-per-page conf/albums-prefetch-factor)))] + (range start end))) + +(def responses {["getAlbumList2" {:type "recent" :offset 1}] {:album (stub-albums 1)} + ["getAlbumList2" {:type "recent" :offset 2}] {:album (stub-albums 2)} + ["getAlbumList2" {:type "recent" :offset 0}] {:album (stub-albums 0)} + ;; vvv this one shouldn't show up in the test vvv + ["getAlbumList2" {:type "newest" :offset 1}] {:album (reverse (stub-albums 1))} + ["getAlbumList2" {:type "recent" :offset 3}] {:album (stub-albums 3)}}) (deftest complete-library - (testing "Should concatenate all album list responses for a given type of list" - (is (= '(1 2 3 4 5 6 7 8) + (testing "Should concatenate and deduplicate all album list responses for a given type of list" + ;; we test from offset 0 to 3, which is where these numbers come from + (println "last number" (last (stub-albums 3))) + + (is (= (range 0 (inc (+ (* 3 conf/albums-per-page) + (* conf/albums-per-page conf/albums-prefetch-factor)))) (sub/complete-library responses [:library/complete "recent"])))))