browser.cljs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. (ns frontend.search.browser
  2. (:require [cljs-bean.core :as bean]
  3. [frontend.search.db :as search-db :refer [indices]]
  4. [frontend.search.protocol :as protocol]
  5. [goog.object :as gobj]
  6. [promesa.core :as p]))
  7. ;; fuse.js
  8. (defn search-blocks
  9. [repo q {:keys [limit page]
  10. :or {limit 20}
  11. :as option}]
  12. (let [indice (or (get-in @indices [repo :blocks])
  13. (search-db/make-blocks-indice! repo))
  14. result
  15. (if page
  16. (.search indice
  17. (clj->js {:$and [{"page" page} {"content" q}]})
  18. (clj->js {:limit limit}))
  19. (.search indice q (clj->js {:limit limit})))
  20. result (bean/->clj result)]
  21. (->>
  22. (map
  23. (fn [{:keys [item matches] :as block}]
  24. (let [{:keys [content uuid page]} item]
  25. {:block/uuid uuid
  26. :block/content content
  27. :block/page page
  28. :search/matches matches}))
  29. result)
  30. (remove nil?))))
  31. (defrecord Browser [repo]
  32. protocol/Engine
  33. (query [this q option]
  34. (p/promise (search-blocks repo q option)))
  35. (rebuild-blocks-indice! [this]
  36. (let [indice (search-db/make-blocks-indice! repo)]
  37. (p/promise indice)))
  38. (transact-blocks! [this {:keys [blocks-to-remove-set
  39. blocks-to-add]}]
  40. (swap! search-db/indices update-in [repo :blocks]
  41. (fn [indice]
  42. (when indice
  43. (doseq [block-id blocks-to-remove-set]
  44. (.remove indice
  45. (fn [block]
  46. (= block-id (gobj/get block "id")))))
  47. (when (seq blocks-to-add)
  48. (doseq [block blocks-to-add]
  49. (.add indice (bean/->js block)))))
  50. indice)))
  51. (truncate-blocks! [this]
  52. (swap! indices assoc-in [repo :blocks] nil))
  53. (remove-db! [this]
  54. nil))