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
Possible to use @ee.on() decorator on class method? #81
Comments
What you're doing seems like the most straightforward way to do this, but we might be able to make it easier if we collect the events and methods we want during class definition and then apply the events later. Maybe we have a helper class like: class MethodListeners:
def __init__(self):
self._events = []
def on(self, event):
def decorator(method):
self._events.append((event, method.__name__))
return method
return decorator
def apply(self, o, ee):
for event, method in self._events:
ee.on(event, getattr(o, method))
listeners = MethodListeners()
class Downloader:
def __init__(self):
self.ee = EventEmitter()
listeners.apply(self)
@listeners.on("download")
def on_download(self, event):
... No guarantees that this will work out of the box, and I'm sure it's possible to iterate on this into something more helpful. If anyone gives this strategy a shot and they come up with an API they don't hate, go ahead and send it along in a merge request. Otherwise maybe I'll arrive at the right answer someday. |
Another idea is to do a trick similar to attrs and wrap this in a class decorator: from pyee.cls import evented, on
@evented
class Downloader:
@on("download")
def download_handler(self, download):
.... Basically |
I have a PR implementing this: Feel free to take it for a spin. |
I think the answer to this is No, but wanted to check and see:
I have a class with:
Is there any way to decorate the task directly?
In my other module, I'm emitting as follows:
Interestingly, when I use the decorator as above the
self
argument is my other module, and themsg
parameter is missingThe text was updated successfully, but these errors were encountered: