 (defonce output (async/chan))
 (defonce exception (async/chan))
 
-(defn js-handle [fn]
-  (println "executing stuff in " (Thread/currentThread))
-  (fn))
+(defn- js-handle [fn] (fn))
 
-(defn js-loop []
+(defn- js-loop []
   (async/thread (while true
                   (let [fn (async/<!! input)]
                     (try

(defonce fonts (atom {}))
;;; ;;; ;;; END OF STATE

(defn set-lang-version [cx]
  (.setLanguageVersion cx Context/VERSION_ES6)
  cx)

(defn load-jvm-npm [cx scope]
  (let [reader (InputStreamReader. (BufferedInputStream. (FileInputStream. "./resources/jvm-npm.js")) StandardCharsets/UTF_8)]
    (.evaluateReader cx scope reader "<cmd>" 1 nil)
    (.close reader)))

(defn eval-str [x]
  (.evaluateString @cx @scope x "<cmd>" 1 nil))

(defn bootstrap-rhino []
  (swap! cx (fn [old-cx] (set-lang-version (Context/enter))))
  (swap! scope (fn [old-scope] (.initStandardObjects @cx)))
  (load-jvm-npm @cx @scope)

                        (println "error in js-loop ..." (.getMessage e))
                        (async/>!! exception e)))))))

(defn- run-js-thread [f]
  (async/>!! input f)
  (let [[result ch] (async/alts!! [output exception])]
    (if (= ch exception)
      (throw result)
      result)))

(defonce js-loop-thread (js-loop)) ;;; bootstrap JS loop

;;; ;;; ;;; STATE
(defonce cx (atom nil))
