Skip to content

Commit

Permalink
Delived result after state is updated using delay + watch
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Blom committed Dec 7, 2017
1 parent 3643147 commit 0a8f206
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/datomock/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(d/delete-database uri)
db))

(defrecord MockConnState [db logVec])
(defrecord MockConnState [db logVec deliver-tx-res])

(defn log-item
[tx-res]
Expand Down Expand Up @@ -54,8 +54,13 @@
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))))
(->MockConnState (:db-after tx-res)
(conj (:logVec old-val) (log-item tx-res))
;; pass a delay that delivers tx-res
;; to be forced by a watch on the agent
;; to ensure the future is deliverd after
;; the agent state is updated
(delay (deliver fut tx-res))))
old-val)))
fut))

Expand All @@ -81,4 +86,8 @@

(defn mock-conn*
[^Database db, ^Log parent-log]
(->MockConnection (agent (->MockConnState db [])) (d/next-t db) parent-log (atom nil)))
(let [state-agent (-> (agent (->MockConnState db [] nil nil))
(add-watch ::force-deliver-promise (fn [_ _ old new]
(when-not (= old new)
(force (:deliver-tx-res new))))))]
(->MockConnection state-agent (d/next-t db) parent-log (atom nil))))

0 comments on commit 0a8f206

Please sign in to comment.