                         connections))]
     (if broadcast connections (take 1 connections))))
 
+(defn trim-last-newline [args]
+  (if-let [args (not-empty (filter string? args))]
+    (conj (vec (butlast args))
+          (string/trim-newline (last args)))
+    args))
+
+(defn print-to-stream [stream args]
+  (condp = stream
+    :out (apply println args)
+    :err (binding [*out* *err*]
+           (apply println args))))
+
+(defn repl-env-print [repl-env stream args]
+  (when-let [args (not-empty (filter string? args))]
+    (when (and (:out-print-fn repl-env) (= :out stream))
+      (apply (:out-print-fn repl-env) args))
+    (when (and (:err-print-fn repl-env) (= :err stream))
+      (apply (:err-print-fn repl-env) args))
+    (let [args (trim-last-newline args)]
+      (when (:print-to-output-streams repl-env)
+        (if-let [bprinter @(:bound-printer repl-env)]
+          (bprinter stream args)
+          (print-to-stream stream args))))))
+
 (let [timeout-val (Object.)]
   (defn evaluate [{:keys [focus-session-name ;; just here for consideration
                           repl-timeout
                           broadcast] :as repl-env} js]
     (reset! (:bound-printer repl-env)
             (bound-fn [stream args]
-              (condp = stream
-                :out (apply println args)
-                :err (binding [*out* *err*]
-                       (apply println args)))))
+              (print-to-stream stream args)))
     (wait-for-connection repl-env)
     (let [ev-connections (eval-connections repl-env)
           result (let [v (deref (send-for-eval ev-connections js)

  (glog/info logger (str "Session ID: "   (session-id)))
  (glog/info logger (str "Session Name: " (session-name))))

(defmethod message "ping" [msg] (respond-to msg true))

(def ^:dynamic *eval-js* js/eval)


      (Thread/sleep 500)
      (recur))))

(defn send-for-eval [connections js]
  (send-for-response connections {:op :eval :code js}))

(defn eval-connections [{:keys [focus-session-name
                                broadcast] :as repl-env}]
