 
 (defn add-query-filter!
   [g q-vertex flt]
-  (let [fv (f/add-vertex!
-            g nil ;; FIXME delay
+  (let [tx (comp (mapcat identity) (filter flt))
+        fv (f/add-vertex!
+            g nil
             {::f/collect-fn
              (f/collect-pure
-              (fn [_ incoming]
-                ;; FIXME delay
-                ;;(delay (sequence (comp (mapcat deref) (filter flt)) incoming))
-                (sequence (comp (mapcat identity) (filter flt)) incoming)
-                ))})]
+              (fn [_ incoming] (sequence tx incoming)))})]
     (f/add-edge! g q-vertex fv f/signal-forward nil)
     fv))
 

           (fn [{:syms [?a ?super ?b]}] [[?a ?super ?b]]))
        pq (:qvar-result (add-param-query! g :pq '[?s knows ?o]))
        jq (:qvar-result (add-join-query! g '[[?p author ?prj] [?prj type project] [?p type person]]))
        tq (:qvar-result (add-join-query! g '[[?p author ?prj] [?prj tag ?t]]))
        _ (run!
           #(add-triple! g %)
           '[[toxi author fabric]
             [fabric type project]
             [knows type symmetric-prop]
             [knows domain person]
             [author domain person]
             [author range creative-work]
             [parent sub-prop-of ancestor]
             [ancestor type transitive-prop]
             [ancestor domain person]
             [ancestor range person]])
        ;;_ (prn :next-id (-> g :g :state deref :next-id))
        ctx (f/async-execution-context {:graph g})
        ctx-chan (f/execute! ctx)]
    (go []
        (let [res (<! ctx-chan)]
          (warn :result res)
          (warn (sort @all))
          (run!
           #(add-triple! g %)
           '[[toxi parent noah]
             [ingo parent toxi]
             [geom type project]
             [toxi author geom]
             [toxi knows noah]
             [geom tag clojure]
             [fabric tag clojure]])
          (let [res (<! ctx-chan)]
            (warn :result res)
            (warn :all (sort @all))
            (warn :pq @pq)
            (warn :jq @jq)
            (warn :tq @tq)
            (remove-triple! g '[geom tag clojure])
            (remove-triple! g '[fabric type project])
            (let [res (<! ctx-chan)]
              (warn :jq @jq)
              (warn :tq @tq)
              (f/stop! ctx)
              (do (remove-triple-graph-logger log)
                  (warn :done))))))

    {:g        g
     :ctx      ctx
     :all      all
     :toxi     toxi
     :types    types
     :knows    knows
     :projects projects
     :pq       pq
     :jq       jq
     :tq       tq
     :num-projects num-projects
     :num-types num-types
     }))


(defn ^:export start-sync
  []
  (let [;;log (add-triple-graph-logger "triple-log.edn")
        ;;g (triple-graph (f/logged-compute-graph (f/compute-graph) log))
        g (triple-graph (f/compute-graph))
        log (add-triple-graph-logger
             g #?(:clj
                  #(spit "triple-log.edn" (str (pr-str %) "\n") :append true)
                  :cljs
                  #(.log js/console (pr-str %))))
        toxi (:result (add-query! g :toxi ['toxi nil nil]))
        types (:result (add-query! g :types [nil 'type nil]))
        projects (:result (add-query! g :projects [nil 'type 'project]))
        knows (:result (add-query! g :projects [nil 'knows nil]))
        all (:result (add-query! g :all [nil nil nil]))
        num-projects (add-counter! g projects)
        num-types (add-counter! g types)
        _ (add-rule!
           g :symmetric '[[?a ?prop ?b] [?prop type symmetric-prop]]
           (fn [{:syms [?a ?prop ?b]}] [[?b ?prop ?a]]))
        _ (add-rule!
           g :domain '[[?a ?prop nil] [?prop domain ?d]]
           (fn [{:syms [?a ?prop ?d]}] [[?a 'type ?d]]))
        _ (add-rule!
           g :range '[[nil ?prop ?a] [?prop range ?r]]
           (fn [{:syms [?a ?prop ?r]}] [[?a 'type ?r]]))
        _ (add-rule!
           g :transitive '[[?a ?prop ?b] [?b ?prop ?c] [?prop type transitive-prop]]
           (fn [{:syms [?a ?prop ?c]}] [[?a ?prop ?c]]))
        _ (add-rule!
           g :sub-prop '[[?a ?prop ?b] [?prop sub-prop-of ?super]]
           (fn [{:syms [?a ?super ?b]}] [[?a ?super ?b]]))
        pq (:qvar-result (add-param-query! g :pq '[?s knows ?o]))
        jq (:qvar-result (add-join-query! g '[[?p author ?prj] [?prj type project] [?p type person]]))
        tq (:qvar-result (add-join-query! g '[[?p author ?prj] [?prj tag ?t]]))
        _ (run!
           #(add-triple! g %)
           '[[toxi author fabric]
             [fabric type project]
             [knows type symmetric-prop]
             [knows domain person]
             [author domain person]
             [author range creative-work]
             [parent sub-prop-of ancestor]
             [ancestor type transitive-prop]
             [ancestor domain person]
             [ancestor range person]
             ;;[noah knows toxi]
             ])
        ctx (f/sync-execution-context {:graph g})
        res (f/execute! ctx)]
    (warn :result res)
    (warn (sort @all))
    (run!

  (query-result [_ id]))

(defn signal-triple
  [vertex op] [op (:id vertex) @vertex])

(defn collect-index
  [spo]
  (f/collect-pure
   (fn [val incoming]
     ;;(debug :update-index spo incoming)
     (debug :old-index val)
     (let [val (transduce
                (map (fn [[op id t]] [op id (nth t spo)]))
