From e3adcedcf7b6b921b8abeff4c4bc3ff47d9acd72 Mon Sep 17 00:00:00 2001 From: Chris Blom Date: Thu, 30 Nov 2017 14:43:11 +0100 Subject: [PATCH] Use agent instead of atom to implement mock connection see https://github.com/vvvvalvalval/datomock/issues/2 --- src/datomock/impl.clj | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/datomock/impl.clj b/src/datomock/impl.clj index 7bc049d..bee275a 100644 --- a/src/datomock/impl.clj +++ b/src/datomock/impl.clj @@ -40,25 +40,20 @@ Connection (db [_] (:db @a_state)) - (transact [_ tx-data] (doto (datomic.promise/settable-future) - (deliver (let [tx-res - (loop [] - (let [old-val @a_state - db (:db old-val) - tx-res (try (d/with db tx-data) - (catch Throwable err - (throw (ExecutionException. err)))) - new-val (->MockConnState - (:db-after tx-res) - (conj (:logVec old-val) (log-item tx-res)))] - (if (compare-and-set! a_state old-val new-val) - tx-res - (recur)) - ))] - (when-let [^BlockingQueue txq @a_txq] - (.add ^BlockingQueue txq tx-res)) - tx-res)) - )) + (transact [_ tx-data] + (let [fut (datomic.promise/settable-future)] + (send a_state + (fn [old-val] + (if-let [tx-res (try (d/with (:db old-val) tx-data) + (catch Throwable err + (deliver fut err) + nil))] + (do (when-let [^BlockingQueue txq @a_txq] + (.add ^BlockingQueue txq tx-res)) + (deliver fut tx-res) + (->MockConnState (:db-after tx-res) (conj (:logVec old-val) (log-item tx-res)))) + old-val))) + fut)) (transactAsync [this tx-data] (.transact this tx-data)) (requestIndex [_] true) @@ -83,4 +78,4 @@ (defn mock-conn* [^Database db, ^Log parent-log] - (->MockConnection (atom (->MockConnState db [])) (d/next-t db) parent-log (atom nil))) + (->MockConnection (agent (->MockConnState db [])) (d/next-t db) parent-log (atom nil)))