refactor clear-and-get-evalqueue into the reusable reset-returning-old!

This commit is contained in:
Jeremy Penner 2013-03-12 00:17:10 -05:00
parent f3f5d007f9
commit e3d4129d64
2 changed files with 9 additions and 8 deletions

View file

@ -1,4 +1,5 @@
(ns hottub.repl (ns hottub.repl
(:use hottub.util)
(:require (clojure.tools.nrepl [server :as server] (:require (clojure.tools.nrepl [server :as server]
[middleware :as middleware]) [middleware :as middleware])
(clojure.tools.nrepl.middleware pr-values (clojure.tools.nrepl.middleware pr-values
@ -34,15 +35,8 @@
(server/stop-server @(:server repl)) (server/stop-server @(:server repl))
(reset! (:server repl) nil)) (reset! (:server repl) nil))
(defn clear-and-get-evalqueue [repl]
(let [evalqueueatom (:evalqueue repl)]
(loop [evalqueue @evalqueueatom]
(if (compare-and-set! evalqueueatom evalqueue [])
evalqueue
(recur @evalqueueatom)))))
(defn repl-update [repl] (defn repl-update [repl]
(doseq [f (clear-and-get-evalqueue repl)] (doseq [f (reset-returning-old! (:evalqueue repl) [])]
(try (try
(f) (f)
(catch Exception e (println "couldn't eval:" (.getMessage e)))))) (catch Exception e (println "couldn't eval:" (.getMessage e))))))

7
src/hottub/util.clj Normal file
View file

@ -0,0 +1,7 @@
(ns hottub.util)
(defn reset-returning-old! [a newval]
(loop [oldval @a]
(if (compare-and-set! a oldval newval)
oldval
(recur @a))))