From e3d4129d64d87a360448a191e484ed7e0fa2c169 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Tue, 12 Mar 2013 00:17:10 -0500 Subject: [PATCH] refactor clear-and-get-evalqueue into the reusable reset-returning-old! --- src/hottub/repl.clj | 10 ++-------- src/hottub/util.clj | 7 +++++++ 2 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 src/hottub/util.clj diff --git a/src/hottub/repl.clj b/src/hottub/repl.clj index b793358..f0b9813 100644 --- a/src/hottub/repl.clj +++ b/src/hottub/repl.clj @@ -1,4 +1,5 @@ (ns hottub.repl + (:use hottub.util) (:require (clojure.tools.nrepl [server :as server] [middleware :as middleware]) (clojure.tools.nrepl.middleware pr-values @@ -34,15 +35,8 @@ (server/stop-server @(:server repl)) (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] - (doseq [f (clear-and-get-evalqueue repl)] + (doseq [f (reset-returning-old! (:evalqueue repl) [])] (try (f) (catch Exception e (println "couldn't eval:" (.getMessage e)))))) \ No newline at end of file diff --git a/src/hottub/util.clj b/src/hottub/util.clj new file mode 100644 index 0000000..ee8afdb --- /dev/null +++ b/src/hottub/util.clj @@ -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))))