Feature suggestion: Global Hooks #736
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes included in this PR
Two new decorators -
before_message
andafter_message
- that can be used to register global hooks.Added a
discover_message_hooks
utility function that creates a mapping of hooks for the ChargePoint subclass.The ChargePoint class stores hook mapping in property
_message_hooks
, it includes a new private method to streamline calling of hooks that handles both synchronous and asynchronous hooks.Added hook execution before and after routing the calls.
Current behavior
If you want to run a function for every call, no matter the type, you currently need to add it to a handler for every type of call (or do some workarounds with overwriting methods).
I find my self having the same code to save calls on all
@after
handlers.New behavior
To add global hooks you can do:
Impact
I believe there is no breaking changes with this implementation.
Global hooks is implemented with the same decorator design as the call handlers.
Question is how useful is it?
I have one relevant case, saving all calls. But is there other use cases?
Here is further questions to consider with this feature:
unpack
andcamel_to_snake_case
is run after the before hook.Checklist