question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feat: add a global finalizer callback

See original GitHub issue

After #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:closed
  • Created 7 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
ankostiscommented, Feb 24, 2017

Yes, that would be the a perfect solution. Thanks.

0reactions
aleneumcommented, Mar 7, 2017

resolved by #175 I hope

Read more comments on GitHub >

github_iconTop Results From Across the Web

FinalizationRegistry - JavaScript - MDN Web Docs
A FinalizationRegistry object lets you request a callback when an object is garbage-collected.
Read more >
Finalizer class - dart:core library - Flutter - Dart API docs
A finalizer can create attachments between the finalizer and any number of Dart values, by calling attach with the value, along with a...
Read more >
feat(npm): implement Node API #13633 - denoland/deno
This PR implements the NAPI for loading native modules into Deno. Closes #15717 Feature tracking Environment life cycle APIs napi_set_instance_data ...
Read more >
What are WeakRef and Finalizers in ES2021 (ES12)
A FinalizationRegistry object lets you request a callback when an object is garbage-collected. You first define the registry with the callback ...
Read more >
cbl | Dart Package - Pub.dev
FEAT : add support for Linux + Flutter. FEAT: CblWorker isolate debug name. FEAT: add toJson methods to containers (#167). FEAT: allocate global...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found