Skip to content

Instantly share code, notes, and snippets.

@rwat
Last active January 1, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwat/8117055 to your computer and use it in GitHub Desktop.
Save rwat/8117055 to your computer and use it in GitHub Desktop.
Sundry Clojurescript snippets
;; milliseconds since the epoch
(.now js/Date)
;; read clojure data received over the network
(ns foo.core (:require [cljs.reader :as reader]))
(reader/read-string x)
;; write clojure data to send over the network
(pr-str {:hi "there"})
;; write something to the browser javascript console
(js/console.log x)
(.log js/console x)
;; working with websockets
(def websocket (atom (new js/WebSocket "ws://localhost:1234")))
(doall
(map #(aset @websocket (first %) (second %))
[["onopen" (fn [] (js/console.log "OPEN"))]
["onclose" (fn [] (js/console.log "CLOSE"))]
["onerror" (fn [e] (js/console.log (str "ERROR: " e)))]
["onmessage" (fn [m] (js/console.log (str "MSG: " (.-data m))))]]))
;; clojure data structure to js data structure
(let [mymap {:a "a" :b "b"}]
(.log js/console (clj->js mymap)))
;; delay execution (w/out using core.async)
(js/setTimeout #(swap! an-atom assoc :a "a") 2000)
;; select dom element by id
(defn by-id [id]
(.getElementById js/document id))
;; prevent the google closure compiler from munging a function name
(defn ^:export myfunc [] ... )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment