Last active
August 29, 2015 14:04
Austin Browser Repl Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns bkell.component.browserrepl | |
(:require [com.stuartsierra.component :as component] | |
[taoensso.timbre :as timbre])) | |
;; Creating aa Austin repl-env inside of a component (I call it browserrepl) | |
(defn create-repl-env [host] | |
(reset! cemerick.austin.repls/browser-repl-env (cemerick.austin/repl-env :host host)) ) | |
(defrecord BrowserRepl [env] | |
component/Lifecycle | |
(start [component] | |
(timbre/debug "BrowserRepl.start CALLED / env[" (keys env) "] / component[" (keys component) "]") | |
(if-not (:repl-env component) | |
(assoc component :repl-env (create-repl-env (:host env))) | |
component)) | |
(stop [component] | |
(timbre/trace "BrowserRepl.stop CALLED") | |
(dissoc component :repl-env))) | |
(defn component-browserrepl [env] | |
(map->BrowserRepl {:env env})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns your.namespace | |
(:require [clojure.string :as str] | |
[net.cgrand.enlive-html :as enlive] | |
[taoensso.timbre :as timbre]) | |
;; Use this to deliver an HTML resource, with a 'clojure.browser.repl.connect' back to your repl-env (in your browserrepl) | |
(defn with-browser-repl [filename browserrepl] | |
(timbre/trace "with-browser-repl CALLED / browserrepl[" browserrepl "]") | |
(let [repl-env (:repl-env browserrepl) | |
chopped-url (str/split (:repl-url repl-env) #"\/") | |
host (str "http://" (:host repl-env)) | |
port (second (str/split (nth chopped-url 2) #":")) | |
sessionid (nth chopped-url 3) | |
templ (enlive/html-resource filename)] | |
(apply str (enlive/emit* | |
(enlive/transform templ | |
[:html] | |
(enlive/after | |
[{:tag :script :content (str "")} | |
{:tag :script :content (str "clojure.browser.repl.connect.call(null,\"" host ":" port "/" sessionid "/repl/start\");")}])))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment