transaction: allow registering a post-close callback
authorPierre-Yves David <pierre-yves.david@fb.com>
Tue, 28 Oct 2014 14:24:43 +0100
changeset 23220 3f543f6be500
parent 23219 61cd79ac4b99
child 23221 cadc9a723d60
transaction: allow registering a post-close callback The addchangegroup code considers the transaction done after a 'tr.close()' call and schedules the hook's execution for after lock release. In the nested transaction case, the transaction is not yet committed and we must delay this scheduling. We add an 'addpostclose' method (like the 'addpending' and 'addfinalize' ones) that registers code to be run if the transaction is successfully committed.
mercurial/transaction.py
--- a/mercurial/transaction.py	Fri Oct 24 15:58:46 2014 -0400
+++ b/mercurial/transaction.py	Tue Oct 28 14:24:43 2014 +0100
@@ -107,6 +107,8 @@
         self._anypending = False
         # holds callback to call when writing the transaction
         self._finalizecallback = {}
+        # hold callbalk for post transaction close
+        self._postclosecallback = {}
 
     def __del__(self):
         if self.journal:
@@ -299,6 +301,15 @@
         self._finalizecallback[category] = callback
 
     @active
+    def addpostclose(self, category, callback):
+        """add a callback to be called after the transaction is closed
+
+        Category is a unique identifier to allow overwriting an old callback
+        with a newer callback.
+        """
+        self._postclosecallback[category] = callback
+
+    @active
     def close(self):
         '''commit the transaction'''
         if self.count == 1 and self.onclose is not None:
@@ -324,6 +335,10 @@
                 self.opener.unlink(b)
         self.backupentries = []
         self.journal = None
+        # run post close action
+        categories = sorted(self._postclosecallback)
+        for cat in categories:
+            self._postclosecallback[cat]()
 
     @active
     def abort(self):