comparison mercurial/transaction.py @ 23204:10beda5bd2b7

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.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Fri, 17 Oct 2014 22:28:09 -0700
parents ea5af863fbff
children 3f543f6be500
comparison
equal deleted inserted replaced
23203:3872d563e01a 23204:10beda5bd2b7
103 self._filegenerators = {} 103 self._filegenerators = {}
104 # hold callbalk to write pending data for hooks 104 # hold callbalk to write pending data for hooks
105 self._pendingcallback = {} 105 self._pendingcallback = {}
106 # True is any pending data have been written ever 106 # True is any pending data have been written ever
107 self._anypending = False 107 self._anypending = False
108 # holds callback to call when writing the transaction
109 self._finalizecallback = {}
108 110
109 def __del__(self): 111 def __del__(self):
110 if self.journal: 112 if self.journal:
111 self._abort() 113 self._abort()
112 114
286 any = self._pendingcallback.pop(cat)() 288 any = self._pendingcallback.pop(cat)()
287 self._anypending = self._anypending or any 289 self._anypending = self._anypending or any
288 return self._anypending 290 return self._anypending
289 291
290 @active 292 @active
293 def addfinalize(self, category, callback):
294 """add a callback to be called when the transaction is closed
295
296 Category is a unique identifier to allow overwriting old callbacks with
297 newer callbacks.
298 """
299 self._finalizecallback[category] = callback
300
301 @active
291 def close(self): 302 def close(self):
292 '''commit the transaction''' 303 '''commit the transaction'''
293 if self.count == 1 and self.onclose is not None: 304 if self.count == 1 and self.onclose is not None:
294 self._generatefiles() 305 self._generatefiles()
306 categories = sorted(self._finalizecallback)
307 for cat in categories:
308 self._finalizecallback[cat]()
295 self.onclose() 309 self.onclose()
296 310
297 self.count -= 1 311 self.count -= 1
298 if self.count != 0: 312 if self.count != 0:
299 return 313 return