Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apichange beforeafter #74

Merged
merged 2 commits into from
Jul 22, 2021
Merged

Apichange beforeafter #74

merged 2 commits into from
Jul 22, 2021

Conversation

da-h
Copy link
Owner

@da-h da-h commented Jul 22, 2021

Api-Change for: Before/After Events

This PR changes the way before_eventname/after_eventname events communicate with their caller (namely the event with the according name eventname.

Attention: This is a breaking change.

Reason for this change is that these kinds of events could not switch position (change execution from before to after or vice versa) and could not be used as a standalone without changing the function signatures. This PR solves this issue by improving the API of these events.

API-Proposal:

  • Any before_/after_ event gets called with a unique event object, that behaves just like the normal event object.
  • This new event-object has a dict-attribute .hook
  • before_-events can read and manipulate the function call arguments before the actual event will be called using event.hook["args"] and event.hook["kwargs"]
  • after_-events can read the function call arguments after the actual event has been called with (including the changes of potential before_-hooks using event.hook["args"] and event.hook["kwargs"]
    (Note: Any modification will only change the arguments for future after_-events but not for the function call itself.)
  • after_-events can additionally alternate the return value of the event by modifying event.hook["result"].
  • both event types can access the name of the actual event-name using event.hook["name"]

Example Module:

def dosomething(val):
    print("event called with value: %i" % val)
    return val

def main(event):
    print("event returned value: %i" % event.dosomething(42))

def before_dosomething(event):
    print("before_-event called")
    event.hook["args"][0] *= 2

def after_dosomethingl(event):
    print("after_-event called")
    event.hook["result"] += 1

def register(mf):
    mf.register_event('dosomething', dosomething)
    mf.register_event('main', main)
    mf.register_event('before_dosomething', before_dosomething, unique=False)
    mf.register_event('after_dosomething', after_dosomething, unique=False)

Running this module should result in the following output:

before_-event called
event called with value: 84
after_-event called
event returned value: 85

@da-h da-h force-pushed the apichange_beforeafter branch from 52def4d to 8c3be60 Compare July 22, 2021 15:09
@da-h da-h force-pushed the apichange_beforeafter branch from 8c3be60 to ac37623 Compare July 22, 2021 15:25
@da-h da-h merged commit d66e56b into v2-dev Jul 22, 2021
@da-h da-h deleted the apichange_beforeafter branch July 22, 2021 15:26
This was referenced Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant