transaction: allow registering a finalization callback
The new 'addfinalize' method allows people to register a callback to
be triggered when the transaction is closed. This aims to get rid of
explicit calls to 'changelog.finalize'. This also obsoletes the
'onclose' function but removing it is not in the scope of this series.
--- a/mercurial/transaction.py Fri Oct 17 21:55:31 2014 -0700
+++ b/mercurial/transaction.py Fri Oct 17 22:28:09 2014 -0700
@@ -105,6 +105,8 @@
self._pendingcallback = {}
# True is any pending data have been written ever
self._anypending = False
+ # holds callback to call when writing the transaction
+ self._finalizecallback = {}
def __del__(self):
if self.journal:
@@ -288,10 +290,22 @@
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 callbacks with
+ newer callbacks.
+ """
+ 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