Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hooks to console.log API to allow 2-way communication between page and dev tools #38

Closed
meta-meta opened this issue Sep 14, 2016 · 1 comment

Comments

@meta-meta
Copy link

relevant conversation here:
https://clojurians.slack.com/archives/clojurescript/p1473893842002423

End goal is to send messages from a reagent webapp to figwheel and back. Specifically, some terminal will be rendered as a texture in an A-frame VR scene.

eval and print at least. intellisense would be a huge bonus.

re: loading dev tools in iframe, this won't work with A-frame unfortunately: aframevr/aframe#399

@darwin
Copy link
Member

darwin commented Sep 17, 2016

I have added request-eval-cljs and request-eval-js into dirac.runtime.repl namespace[1].

This will be available in the next release 0.6.7.

To capture complete output. You will have to hijack methods on console object. For inspiration you can look here[2] - I capture console output for automated testing.

[1]

(defn ^:export request-eval-cljs [code]
(assert (string? code) "Code passed for evaluation must be a string")
(call-dirac "eval-cljs" code))
(defn ^:export request-eval-js [code]
(assert (string? code) "Code passed for evaluation must be a string")
(call-dirac "eval-js" code))

[2]
; -- capturing console output -----------------------------------------------------------------------------------------------
(defn console-handler [orig kind & args]
(let [transcript (str kind args)]
(feedback! transcript (str "scenario out"))
(.apply orig js/console (to-array args))))
(defn store-console-api []
{"log" (oget js/window "console" "log")
"warn" (oget js/window "console" "warn")
"info" (oget js/window "console" "info")
"error" (oget js/window "console" "error")})
(defn captured-console-api [original-api]
{"log" (partial console-handler (get original-api "log") "LOG: ")
"warn" (partial console-handler (get original-api "warn") "WARN: ")
"info" (partial console-handler (get original-api "info") "INFO: ")
"error" (partial console-handler (get original-api "error") "ERROR: ")})
(defn set-console-api! [api]
(oset js/window ["console" "log"] (get api "log"))
(oset js/window ["console" "warn"] (get api "warn"))
(oset js/window ["console" "info"] (get api "info"))
(oset js/window ["console" "error"] (get api "error")))
(defn capture-console-as-feedback! []
{:pre [(nil? @original-console-api)]}
(reset! original-console-api (store-console-api))
(set-console-api! (captured-console-api @original-console-api)))
(defn uncapture-console-as-feedback! []
{:pre [(some? @original-console-api)]}
(set-console-api! @original-console-api)
(reset! original-console-api nil))

@darwin darwin closed this as completed Sep 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants