# HG changeset patch # User Pierre-Yves David # Date 2014-10-18 05:28:09 # Node ID 10beda5bd2b725e2ad03431b24bc88f60050e9f4 # Parent 3872d563e01a109ed11e0dd2e100e40052a642b3 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. diff --git a/mercurial/transaction.py b/mercurial/transaction.py --- a/mercurial/transaction.py +++ b/mercurial/transaction.py @@ -105,6 +105,8 @@ class transaction(object): 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 @@ class transaction(object): 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