   (:require-macros [cljs.core.async.macros :refer [go go-loop]])
   (:require [reagent.core :as reagent]
             [cljs.core.async :as async]
+            [clojure.set :refer [union]]
             [cljsjs.react]
             [goog.net.XhrIo]
             [zest.settings]


        @score))))

(defn do-match-chunks [query reset]
  (if (not-empty @nonfts-cursor)
    (let [chunk (first @nonfts-cursor)
          new-results
          (->>
            chunk
            (map (fn [v] [(score-exact (.-name (.-contents v)) query) v]))
            (filter #(> (first %) 0))
            (take 100))]

        ; reset only after something was found
        (reset! nonfts-results []))

      (reset! nonfts-keys
              (union @nonfts-keys (set (map #(key (second %)) new-results))))

      (reset!
        nonfts-results
        (concat @nonfts-results new-results))

      (if (or (and reset (empty? new-results))
              (< (count @nonfts-results) 2000))
        (reset! query-immediate
                (.setImmediate js/window
                               #(do-match-chunks
                                 query
                                 (and reset (= 0 (count new-results)))
                                 (> (count @nonfts-results) 100)))))))

  (if (and (empty? @nonfts-cursor) reset)
    (reset! nonfts-results []))

  (if (and (not just-for-next-queries)
           (or (empty? @nonfts-cursor)
               (>= (count @nonfts-results) 8)))
    ; show only after finished searching or if found at least 8 results,
    ; to avoid flickering of the list
    (reset!
