From 3212be565cc50d1a1f31bff189b87802f27398d4 Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Tue, 1 Nov 2016 16:47:47 -0700 Subject: [PATCH] Allow callers to run functions within the scope of a transaction. This generalizes the transactor loop to allow callers to run an arbitrary function within an `in-transaction!` body. Combined with exposing ` report (assoc-in [:db-after] db-after))))) -(defn- ! token-chan (gensym "transactor-token")) (loop [] (when-let [token ( (! (:listener-source conn) report)) - report)))))] + ;; Default: process the transaction. + (do + (when @(:closed? conn) + ;; Drain enqueued transactions. + (raise "Connection is closed" {:error :transact/connection-closed})) + + (let [db (db conn) + in-transaction-fn + (case sentinel + :sentinel-fn + ;; This is a function that we'd like to run + ;; within a database transaction. See + ;; db/in-transaction! for details. + ;; The function is invoked with two arguments: + ;; the db and a function that takes (db, + ;; tx-data) and transacts it to return a + ;; TxReport. + ;; The function must return a TxReport. + ;; The function must not itself call + ;; `in-transaction!` or `! (:listener-source conn) report)) + report)))))] ;; Even when report is nil (transaction not committed), pair is non-nil. (>! result pair)) (>! token-chan token)