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

Context manager API for EndpointAPI.subscribe #149

Open
pipermerriam opened this issue Jul 31, 2019 · 0 comments
Open

Context manager API for EndpointAPI.subscribe #149

pipermerriam opened this issue Jul 31, 2019 · 0 comments
Labels
good first issue Good for newcomers

Comments

@pipermerriam
Copy link
Member

What was wrong?

The EndpointAPI.subscribe API returns a handle on the subscription that the user can call to remove this subscription. This is easy to forget or in some contexts, not always easy have a clear place where this would be called.

How can it be fixed?

We can fix this by making lahja.common.Subscription a context manager and having it automatically call Subscription.unsubscribe in the __exit__. This allows for the subscription API to be used like this:

with EndpointAPI.subscribe(MyEvent, handler_fn):
    # subscription active here:
# automatically unsubscribed here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant