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

added function that returns an array listing the events #96

Closed
wants to merge 1 commit into from
Closed

added function that returns an array listing the events #96

wants to merge 1 commit into from

Conversation

leirons
Copy link
Contributor

@leirons leirons commented Jan 4, 2022

In node js we can emitter.eventNames() that returns array of events, so i added the function that returns name of events

Copy link
Owner

@jfhbrook jfhbrook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a cool feature and I'd be glad to roll it into the next release! Do you mind writing a test? Adding a really simple one that adds some named events and then asserts the expected result should be plenty. I think I spotted a bug in the current implementation, but a test will tell us for sure.

Thanks!

@@ -80,6 +80,10 @@ def _add_event_handler(self, event, k, v):
def _emit_run(self, f, args, kwargs):
f(*args, **kwargs)

def event_names(self):
# Returns an array listing the events
return list(self._events.keys())[1:]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This slice is dropping the first key:

❯ python
Python 3.8.8 (default, Apr 13 2021, 19:58:26) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> xs = [1, 2, 3]
>>> xs[1:]
[2, 3]
>>> 

I don't think this is what we want. I think you were trying to clone the list:

>>> xs = [1, 2, 3]
>>> xs[:]
[1, 2, 3]

The motivation for doing this is so that self._events may mutate before you've completed iterating over its keys - the list gets that out of the way for us. Good thinking! But I think that's already handled by converting it to a list:

>>> xs is xs
True
>>> xs is xs[:]
False
>>> xs is list(xs)
False

In other words, try the following:

def event_names(self):
    return list(self._events.keys())

Copy link
Contributor Author

@leirons leirons Jan 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to drop the first key because in the first place always new_listener, and it's not a really listener, or am i wrong?

ok, l will do it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the behavior in node.js? do they filter out internal events there? I think the easiest thing will be to copy that interface.

This was referenced Jan 12, 2022
@jfhbrook
Copy link
Owner

I have this some light rework and pushed it as #99. I intend to publish this in release 9. Thanks!

@jfhbrook jfhbrook closed this Jan 12, 2022
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.

None yet

3 participants