 (defn match-chunks [entries query]
   (if (not (nil? @query-immediate))
     (.clearImmediate js/window @query-immediate))
-  (if (not (nil? @query-timeout))
-    (.clearTimeout js/window @query-timeout))
-  (reset! nonfts-cursor (partition-all 2000 entries))
-  (reset! query-timeout
-          (.setTimeout js/window #(do-match-chunks query true) 0)))
+  (reset! nonfts-cursor (cons (map #(second %) @nonfts-results)
+                              (partition-all 2000 entries)))
+  (reset! nonfts-keys #{})
+  (do-match-chunks query true false) 0)
 
 (defn set-query [q]
   (reset! query q)


(def query (reagent/atom ""))
(def query-immediate (reagent/atom nil))
(def query-timeout (reagent/atom nil))
(def nonfts-results (reagent/atom []))
(def nonfts-cursor (atom nil))
(def results (reagent/atom []))
(def search-results (reagent/atom []))

        ; 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!
