Last active
January 1, 2016 08:19
-
-
Save rwat/8117055 to your computer and use it in GitHub Desktop.
Sundry Clojurescript snippets
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
;; 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