Last active
December 14, 2015 03:29
-
-
Save jaycfields/5021245 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; server | |
(defonce server-list (atom [])) | |
(defonce appender (atom nil)) | |
(defonce appending-fiber (atom nil)) | |
(defonce subscriber (atom identity)) | |
(defn subscribe [f] | |
(reset! subscriber f)) | |
(defn publish-to-client [m] | |
(@subscriber m)) | |
(defn get-snapshot [] | |
(publish-to-client {:type :snapshot :val @server-list})) | |
(defn append-to-list [] | |
(when (< 9 (count @server-list)) | |
(swap! server-list butlast)) | |
(let [incremental (rand-nth (range 100))] | |
(swap! server-list conj incremental) | |
(publish-to-client {:type :incremental :val incremental}))) | |
(defn server-start [] | |
(reset! appending-fiber (doto (org.jetlang.fibers.ThreadFiber.) .start)) | |
(reset! appender (.scheduleAtFixedRate @appending-fiber | |
append-to-list 500 500 | |
java.util.concurrent.TimeUnit/MILLISECONDS))) | |
(defn server-stop [] | |
(.dispose @appender) | |
(.dispose @appending-fiber)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment