Skip to content

Instantly share code, notes, and snippets.

@mike706574
mike706574 / generators-question.clj
Created October 28, 2017 19:32
generators-question
(require '[clojure.spec.alpha :as s])
(require '[clojure.spec.gen.alpha :as gen])
;; I was trying to write a spec for an 5-10 character alpha string with a
;; custom generator that uses clojure.spec.gen.alpha/char-alpha
(s/def ::test
(s/spec (s/and string? #(<= 5 (count %) 10))
:gen (fn [] (gen/fmap #(apply str %)
(gen/vector gen/char-alpha 5 10)))))
(defn websocket-effect
[{:keys [uri on-message on-error on-success on-failure websocket]}]
(if websocket
(rf/dispatch (conj on-success websocket))
(let [websocket (js/WebSocket. uri)]
(log/info "Starting new websocket.")
(set! (.-onmessage websocket) #(rf/dispatch (conj on-message %)))
(set! (.-onerror websocket) #(rf/dispatch (conj on-failure %)))
(set! (.-onopen websocket) (fn on-open []
(set! (.-onerror websocket) #(rf/dispatch (conj on-error %)))