To be more precise, if you do a (print "something") in a cljs-script, that gets
compiled and evaluated in the browser's js-vm, then the result is sent back
thru a separate http-post and dispatched to the multimethod "handle-post"
implementation for :print in clojurescript's cljs.repl.browser:
----------
(defmethod handle-post :print [{:keys [content order]} conn _ ]
(do (constrain-order order (fn [] (do (print (read-string content))
(.flush *out*))))
(server/send-and-close conn 200 "ignore__"))))
----------
If you evaluate a cljs-form thru a direct call of cljs.repl/evaluate-form from
a different thread than the cljs-repl, then any output from (print "something")
will be printed in the cljs-repl terminal session and not the terminal where
you invoked the cljs.repl/evaluate-form from.
That the "defmethod handle-post :print" cannot use the terminal's *out* where
cljs.repl/evaluate-form is invoked, is understandable because it runs in a
different thread.
The issue is that it's not just another thread, it seems a completely different
context as the http-post handlers run asynchronous from any sent js-code.
This seems the reason that using any (binding [*out* terminal-out]
(cljs.repl/evaluate-form …)) doesn't work - or I cannot make it work…
Right now I can make it work by saving the terminal's *out* in an atom and
using it's value directly in the handle-post code, like:
----------
(def context-out (atom nil))
(defmethod handle-post :print [{:keys [content order]} conn _ ]
(if @context-out
(do (constrain-order order (fn [] (binding [*out* @context-out]
(do (print (read-string content))
(.flush *out*)))))
(server/send-and-close conn 200 "ignore__"))
(do (constrain-order order (fn [] (do (print (read-string content))
(.flush *out*))))
(server/send-and-close conn 200 "ignore__"))))
----------
If I do (reset! cljs.repl.browser/context-out *out*) in the clj-repl, then the
stdout of the cljs-repl gets redirected to my terminal… as I want.
However, it feels like a hack and I was hoping that I missed a better
alternative… any suggestions or comments are most welcome.
Thanks, FrankS.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en