[PATCH 4 of 5] transaction: allow to register finalization callback

Pierre-Yves David pierre-yves.david at ens-lyon.org
Sun Nov 2 08:20:42 CST 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1413610089 25200
#      Fri Oct 17 22:28:09 2014 -0700
# Node ID d434fffe10dac3cb137b3695426a69a4cef9ef35
# Parent  95a8260e07f6f99092b19424405ef4ef20401953
transaction: allow to register finalization callback

The new `addfinalize` method allow people to register callback to be triggered
when the transaction is closed. This aims get rid of explicit call to
`changelog.finalize`. This also obsolete the `onclose` function but removing it
is not in the scope of this series.

diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -103,10 +103,12 @@ class transaction(object):
         self._filegenerators = {}
         # hold callbalk to write pending data for hooks
         self._pendingcallback = {}
         # True is any pending data have been written ever
         self._anypending = False
+        # hold callbalk to write when writing the transaction
+        self._finalizecallback = {}
 
     def __del__(self):
         if self.journal:
             self._abort()
 
@@ -286,14 +288,26 @@ class transaction(object):
             any = self._pendingcallback.pop(cat)()
             self._anypending = self._anypending or any
         return self._anypending
 
     @active
+    def addfinalize(self, category, callback):
+        """add a callback to be called when the transaction is closed
+
+        Category is a unique identifier to allow overwriting old callback with
+        newer callback.
+        """
+        self._finalizecallback[category] = callback
+
+    @active
     def close(self):
         '''commit the transaction'''
         if self.count == 1 and self.onclose is not None:
             self._generatefiles()
+            categories = sorted(self._finalizecallback)
+            for cat in categories:
+                self._finalizecallback[cat]()
             self.onclose()
 
         self.count -= 1
         if self.count != 0:
             return


More information about the Mercurial-devel mailing list