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

Allow unregistering EventHandlers #3

Open
ruancomelli opened this issue May 13, 2021 · 0 comments
Open

Allow unregistering EventHandlers #3

ruancomelli opened this issue May 13, 2021 · 0 comments

Comments

@ruancomelli
Copy link

First of all, thank you so much for this project!

I'd like to suggest an additional feature: unregistering EventHandlers.

Unregistering would work as follows:

from buslane import Event, EventBus, EventHandler


class MyEvent(Event):
    pass


class MyHandler(EventHandler[MyEvent]):
    def handle(self, event: MyEvent) -> None:
        print('Handling an event.')

bus = EventBus()

handler = MyHandler()

bus.register(handler)
bus.publish(MyEvent()) # prints "Handling an event."

bus.unregister(handler)
bus.publish(MyEvent()) # prints nothing

My motivation is that I am monitoring a system, and sometimes an automated agent is called to observe events and handle them. This agent was trivially implemented as an EventHandler. But, after some conditions are met, this agent becomes unnecessary and can be safely unregistered. In fact, since those agents are constantly created and registered, without unregistering unnecessary agents I ended up having millions of "dead" handlers. So unregistering seems natural at least in this application.

I already have this implemented and would be very glad to submit a PR if you agree that this is an interesting functionality.

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

No branches or pull requests

1 participant