Patchwork [06,of,10] transaction: allow to register finalization callback

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 28, 2014, 3:41 p.m.
Message ID <81fed2b4a62ebd04121d.1414510880@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/6484/
State Accepted
Headers show

Comments

Pierre-Yves David - Oct. 28, 2014, 3:41 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1413610089 25200
#      Fri Oct 17 22:28:09 2014 -0700
# Branch stable
# Node ID 81fed2b4a62ebd04121d4e072bce6b26666e5f64
# Parent  8b1027bf74178ac96a8ba09a0a68d9a6c9a4f88d
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.

Patch

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