 ;; TODO: xform
 ;; TODO: Terminate loop.
 ;;
+
+;; =============================================================================
+;; Helper functions.
 
 (defn- init-event-bus!
   "Adds support for triggering events and, if the component reified IGotEvent, support for handling events

(ns idealist.event-bus
  (:require [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]
            [cljs.core.async :as async :refer [<! >!]])
  (:require-macros [cljs.core.async.macros :refer [go go-loop]]))

;; =============================================================================
;; Event handling protocol. Use it in your components if you're interested

        (async/tap fork branch)
        (async/tap fork downstream)
        (om/set-state! this ::event-bus upstream)
        (async/go-loop []
                 (let [e (async/<! branch)]
                   (got-event c e))
                 (recur))))))

(defn- before-will-mount!
  "Overrides componentWillMount pure method to call function f before its body."
  [methods f]
  (let [prev-will-mount (:componentWillMount methods)]
    (-> methods
        (assoc :dly_prevWillMount prev-will-mount)
        (assoc :componentWillMount #(this-as this
                                             (do
                                               (f this)
                                               ((.-dly_prevWillMount this))))))))

(defn- make-descriptor
  "Creates a custom descriptor with support for an event bus."
  []
  (let [methods (before-will-mount! om/pure-methods (fn [this]
                                                      (init-event-bus! this)))
        descriptor (om/specify-state-methods! (clj->js methods))]
    descriptor))

