browser.cljs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (ns frontend.search.browser
  2. "Browser implementation of search protocol"
  3. (:require [cljs-bean.core :as bean]
  4. [frontend.search.db :as search-db :refer [indices]]
  5. [frontend.search.protocol :as protocol]
  6. [goog.object :as gobj]
  7. [promesa.core :as p]))
  8. ;; fuse.js
  9. (defn search-blocks
  10. [repo q {:keys [limit page]
  11. :or {limit 20}}]
  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]}]
  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. (transact-pages! [_this _data] nil) ;; Page index is not available with fuse.js until sufficient performance benchmarking
  52. (truncate-blocks! [_this]
  53. (swap! indices assoc-in [repo :blocks] nil))
  54. (remove-db! [_this]
  55. nil))