Feat: add a global finalizer callback
See original GitHub issueAfter #184 has been merged, this library would have a global preparation function but not a corresponding global finalizer that would fire once, both for successful/failed transitions, something necessary when implementing protocols like 2-phase commits. I believe that there is no way to workaround such functionality with code external to this library in a single place (i.e. without proliferating try...except/finally
in all trigger-calls).
Therefore I suggest to add a finalize_after_all_transitions
callback in a finally block inside Event._trigger()
. The code for such a feature, without error handling would be something like this:
for func in self.machine.prepare_conditions_check:
self.machine._callback(func, event_data)
logger.debug("Executed machine preparation callback '%s' before conditions." % func)
ok = False
try:
for t in self.transitions[state.name]:
event_data.transition = t
if t.execute(event_data):
return True
return False
finally:
event_data.kwargs['ok'] = ok # Provisional name for `ok` flag, or an attr on machine?
for func in self.machine.finalize_after_all_transitions:
self.machine._callback(func, event_data)
logger.debug("Executed machine finalize callback '%s' after all transitions." % func)
Of course if finalize cb is to receive the error raised, an except
case is need additionally in the try
block to store the captured exception. Like the ok
flag above, I’m thinking of 2 possibilities where to store it:
a) on the machine as an extra attribute, or
b) on the event-data
, as another keyword item e.g. error
.
Case (a) needs clearing this attribute before every trigger and kind of violate separation-of-concerns law; case (b) might clash with names already used by existing clients of the library.
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (12 by maintainers)
Top GitHub Comments
Yes, that would be the a perfect solution. Thanks.
resolved by #175 I hope