                                                js)
                                 (or repl-timeout 8000)
                                 timeout-val)]
-                   (some->> v
-                            meta
-                            ::message
-                            :session-name
-                            (reset! last-session-name))
                    (if (= timeout-val v)
                      {:status :exception
                       :value "Eval timed out!"

(ns figwheel.repl
  (:require
   #?@(:cljs [[goog.object :as gobj]
              [goog.storage.mechanism.mechanismfactory :as storage-factory]]
       :clj [[clojure.data.json :as json]
             [clojure.set :as set]
             [clojure.edn :as edn]


           (def state (atom {}))
           (def storage (storage-factory/createHTML5SessionStorage "figwheel.repl"))
           (defn session-name []
             (.get storage (str ::session-name)))

           (defn response-for [{:keys [uuid]} response-body]
             {:uuid uuid
              :session-name (session-name)
              :response response-body})

           (defn respond-to [{:keys [websocket] :as old-msg} response-body]
             (let [response (response-for old-msg response-body)]
               (.send websocket (pr-str response))))

           (defmulti message :op)
           (defmethod message "naming" [msg]
             (.set storage (str ::session-name) (:session-name msg)))

           (def ^:dynamic *eval-js* (fn [code] (js* "eval(~{code})")))

           (let [ua-product-fn
                 #(cond
                    (not (nil? goog/nodeGlobalRequire)) :chrome
                    product/SAFARI    :safari
                    product/CHROME    :chrome
                    product/FIREFOX   :firefox
                    product/IE        :ie)]
             (defn eval-javascript** [code]
               (let [ua-product (ua-product-fn)]
                 (try
                   (let [sb (js/goog.string.StringBuffer.)]
                     (binding [cljs.core/*print-newline* false
                               cljs.core/*print-fn* (fn [x] (.append sb x))]
                       (let [result-value (*eval-js* code)]
                         {:status :success
                          :out (str sb)
                          :ua-product ua-product
                          :value result-value})))
                   (catch js/Error e
                     {:status :exception
                      :value (pr-str e)
                      :ua-product ua-product
                      :stacktrace (.-stack e)})
                   (catch :default e
                     {:status :exception
                      :ua-product ua-product
                      :value (pr-str e)
                      :stacktrace "No stacktrace available."})))))

           (defmethod message "eval" [{:keys [code] :as msg}]
             (let [result (eval-javascript** code)]
               (respond-to msg result)))

           (defn connect [websocket-url]
             (let [websocket (goog.net.WebSocket.)]
               (doto websocket
                 (.addEventListener goog.net.WebSocket.EventType.MESSAGE
                                    (fn [e]
                                      (when-let [msg (gobj/get e "message")]
                                        (try
                                          (prn msg)
                                          (js/console.log msg)
                                          (message (assoc
                                                    (js->clj (js/JSON.parse msg) :keywordize-keys true)
                                                    :websocket websocket))
                                          (catch js/Error e
                                            (js/console.error e))))))
                 (.addEventListener goog.net.WebSocket.EventType.OPENED
                                    (fn [e]
                                      (js/console.log "OPENED")
                                      (js/console.log e)))
                 (.open websocket-url)
                 )))
           (js/console.log (.get storage (str ::session-name)))
           (js/console.log (nil? (.get storage (str ::session-name))))
           (connect (str "ws://localhost:9500/fargot" (when-let [n (.get storage (str ::session-name))]
