diff options
author | David Barksdale <amatus.amongus@gmail.com> | 2012-05-12 11:54:44 -0500 |
---|---|---|
committer | David Barksdale <amatus.amongus@gmail.com> | 2012-05-12 11:54:44 -0500 |
commit | 4d7e0788cb4e3194ae53a7713e7010fcde6b4312 (patch) | |
tree | 3c034e116d6d8a138bc6490e129c46ea53f7d9ed /src | |
parent | 2c5904a59b1bc3a7fca0fd75bc5770b4d1f7da63 (diff) |
Created macro agent-do for a common pattern.
This is useful when you want a function with side-effects to be
called after an agent's state has been updated.
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure/foofs/filesystem.clj | 15 | ||||
-rw-r--r-- | src/clojure/foofs/memorybackend.clj | 21 | ||||
-rw-r--r-- | src/clojure/foofs/util.clj | 4 |
3 files changed, 14 insertions, 26 deletions
diff --git a/src/clojure/foofs/filesystem.clj b/src/clojure/foofs/filesystem.clj index 7f21c3b..d4f85a9 100644 --- a/src/clojure/foofs/filesystem.clj +++ b/src/clojure/foofs/filesystem.clj @@ -208,12 +208,8 @@ (continuation! errno-nomem) state) (do - (send - readdir-agent - (fn [state] - (continuation! {:handle handle - :flags 0}) - state)) + (agent-do readdir-agent + (continuation! {:handle handle :flags 0})) {:opendirs (assoc opendirs handle []) :next-handle (inc handle)}))))))))) (readdir [this request continuation!] @@ -229,11 +225,8 @@ (send readdir-agent (fn [state] - (send - readdir-agent - (fn [state] - (continuation! (take size encoded-dirents)) - state)) + (agent-do readdir-agent + (continuation! (take size encoded-dirents))) {:opendirs (assoc (:opendirs state) handle encoded-dirents) :next-handle (:next-handle state)}))))) (let [dirents ((:opendirs (deref readdir-agent)) handle)] diff --git a/src/clojure/foofs/memorybackend.clj b/src/clojure/foofs/memorybackend.clj index 47dab4a..fa2a23d 100644 --- a/src/clojure/foofs/memorybackend.clj +++ b/src/clojure/foofs/memorybackend.clj @@ -43,10 +43,7 @@ (if (nil? attr) (do (continuation! errno-noent) state) (let [new-attr (f attr)] - (send state-agent - (fn [state] - (continuation! new-attr) - state)) + (agent-do state-agent (continuation! new-attr)) (assoc-deep state new-attr :attr-table inode))))))) ;; TODO rename s/attr/attrs/ almost everywhere @@ -114,11 +111,8 @@ attr (conj new-attr {:mode mode :nlink 1})] - (send - state-agent - (fn [state] - (continuation! (assoc attr :inode child-inode)) - state)) + (agent-do state-agent + (continuation! (assoc attr :inode child-inode))) (assoc state :attr-table (assoc attr-table child-inode attr) :lookup-table (assoc lookup-table inode @@ -141,11 +135,8 @@ (if (nil? attr) (do (continuation! errno-noent) state) (do - (send - state-agent - (fn [state] - (continuation! (assoc attr :inode target-inode)) - state)) + (agent-do state-agent + (continuation! (assoc attr :inode target-inode))) (assoc state :attr-table (assoc attr-table target-inode (assoc attr :nlink @@ -204,7 +195,7 @@ (if (not (empty? (dissoc child-children "." ".."))) (do (continuation! errno-notempty) state) (let [nlink (- (:nlink child-attr) 3)] - (continuation! 0) + (agent-do state-agent (continuation! 0)) (assoc state :lookup-table (dissoc (assoc lookup-table inode diff --git a/src/clojure/foofs/util.clj b/src/clojure/foofs/util.clj index 03a0bfc..8119df1 100644 --- a/src/clojure/foofs/util.clj +++ b/src/clojure/foofs/util.clj @@ -34,3 +34,7 @@ ([] nil) ([f] (f)) ([f & fs] (f (partial apply chain fs)))) + +(defmacro agent-do + [_agent f] + `(send ~_agent #(do ~f %))) |