                       (fn [db] (get-in db db-path))
                       (fn [_]  @local-store))
 
-        rules  (massage-rules id rules)
-        all-events  (apply set/union (map :events rules))]       ;; all of the events refered to in the rules
+        rules  (massage-rules id rules)]       ;; all of the events refered to in the rules
 
     ;; return an event handler which will manage the flow
     ;; This event handler will receive 3 kinds of events

        (condp = event-v

              ;; Setup the flow coordinator:
              ;;  1. Initialise the state  (seen-events and started-tasks)
              ;;  2. dispatch the first event, to kick start
              ;;  3. arrange for the events to get forwarded to this handler
              :setup {:db (set-state db #{} #{})
                      :dispatch first-dispatch
                      :event-forwarder {:register id
                                        :events   all-events
                                        :to       [id ]}}

              ;; Teardown the flow coordinator:
              ;;  1. remove this event handler
              ;;  2. remove state
              ;;  3. stop the events forwarder
              :halt {:db (dissoc db db-path)
                     :event-forwarder {:unregister id}
                     :deregister-event-handler id})          ;; XXX write this effects handler

              ;; A new event has been forwarded to this handler. What does it mean?
              ;;  1. does this new event mean we need to dispatch another

  "Given the accumulated set of seen events and the set of tasks already started,
  return the list of tasks which should now be started.
  In effect, given the list of events seen has changed, what is the consequence."
  [rules now-seen-events already-started-tasks]
  (->> (remove (comp already-started-tasks :id) rules)
       (filterv (fn [task] ((:when task) (:events task) now-seen-events)))))


(defn massage-rules
  "Massage the supplied rules as follows:
