Skip to content

Commit

Permalink
Make event_names return a set instead of a list
Browse files Browse the repository at this point in the history
  • Loading branch information
jfhbrook committed Jan 12, 2022
1 parent c25cf56 commit 1918f93
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pyee/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
from threading import Lock
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Set, Tuple


class PyeeException(Exception):
Expand Down Expand Up @@ -122,9 +122,9 @@ def _emit_run(
) -> None:
f(*args, **kwargs)

def event_names(self) -> List[str]:
def event_names(self) -> Set[str]:
"""Get a list of events that this emitter is listening to."""
return list(self._events.keys())
return set(self._events.keys())

def _emit_handle_potential_error(self, event: str, error: Any) -> None:
if event == "error":
Expand Down
22 changes: 11 additions & 11 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def event_handler(data, **kwargs):
call_me()
assert data == "emitter is emitted!"

assert ee.event_names() == ["event"]
assert ee.event_names() == {"event"}

# Making sure data is passed propers
ee.emit("event", "emitter is emitted!", error=False)
Expand All @@ -45,7 +45,7 @@ def test_emit_error():
def on_error(exc):
call_me()

assert ee.event_names() == ["error"]
assert ee.event_names() == {"error"}

# No longer raises and error instead return True indicating handled
assert ee.emit("error", test_exception) is True
Expand All @@ -60,7 +60,7 @@ def test_emit_return():
call_me = Mock()
ee = EventEmitter()

assert ee.event_names() == []
assert ee.event_names() == set()

# make sure emitting without a callback returns False
assert not ee.emit("data")
Expand All @@ -85,7 +85,7 @@ def test_new_listener_event():
def event_handler(data):
pass

assert ee.event_names() == ["new_listener", "event"]
assert ee.event_names() == {"new_listener", "event"}

call_me.assert_called_once_with("event", event_handler)

Expand Down Expand Up @@ -114,7 +114,7 @@ def fourth():

ee.on("event", fourth)

assert ee.event_names() == ["event"]
assert ee.event_names() == {"event"}

assert ee._events["event"] == OrderedDict(
[(first, first), (second, second), (third, third), (fourth, fourth)]
Expand Down Expand Up @@ -147,7 +147,7 @@ def should_remove():
ee.on("remove", should_remove)
ee.on("remove", call_me)

assert ee.event_names() == ["remove"]
assert ee.event_names() == {"remove"}

ee.emit("remove")

Expand All @@ -160,7 +160,7 @@ def should_remove():
ee.on("remove", call_me)
ee.on("remove", should_remove)

assert ee.event_names() == ["remove"]
assert ee.event_names() == {"remove"}

ee.emit("remove")

Expand All @@ -183,13 +183,13 @@ def once_handler(data):
# Tests to make sure that after event is emitted that it's gone.
ee.once("event", once_handler)

assert ee.event_names() == ["event"]
assert ee.event_names() == {"event"}

ee.emit("event", "emitter is emitted!")

call_me.assert_called_once()

assert ee.event_names() == []
assert ee.event_names() == set()

assert "event" not in ee._events

Expand All @@ -205,12 +205,12 @@ def once_handler(data):
handle = ee.once("event", once_handler)

assert handle == once_handler
assert ee.event_names() == ["event"]
assert ee.event_names() == {"event"}

ee.remove_listener("event", handle)

assert "event" not in ee._events
assert ee.event_names() == []
assert ee.event_names() == set()


def test_listeners():
Expand Down

0 comments on commit 1918f93

Please sign in to comment.