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

rtc.EventEmitter contravariance #304

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions livekit-api/livekit/api/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,13 @@ def to_jwt(self) -> str:
{
"sub": self.identity,
"iss": self.api_key,
"nbf": calendar.timegm(datetime.datetime.now(datetime.timezone.utc).utctimetuple()),
"nbf": calendar.timegm(
datetime.datetime.now(datetime.timezone.utc).utctimetuple()
),
"exp": calendar.timegm(
(datetime.datetime.now(datetime.timezone.utc) + self.ttl).utctimetuple()
(
datetime.datetime.now(datetime.timezone.utc) + self.ttl
).utctimetuple()
),
}
)
Expand Down
2 changes: 1 addition & 1 deletion livekit-rtc/livekit/rtc/_proto/ffi_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions livekit-rtc/livekit/rtc/_proto/handle_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions livekit-rtc/livekit/rtc/event_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

from .log import logger

T = TypeVar("T")
T_contra = TypeVar("T_contra", contravariant=True)


class EventEmitter(Generic[T]):
class EventEmitter(Generic[T_contra]):
def __init__(self) -> None:
"""
Initialize a new instance of EventEmitter.
"""
self._events: Dict[T, Set[Callable]] = dict()
self._events: Dict[T_contra, Set[Callable]] = dict()

def emit(self, event: T, *args) -> None:
def emit(self, event: T_contra, *args) -> None:
"""
Trigger all callbacks associated with the given event.

Expand Down Expand Up @@ -60,7 +60,7 @@ def greet(name):
except Exception:
logger.exception(f"failed to emit event {event}")

def once(self, event: T, callback: Optional[Callable] = None) -> Callable:
def once(self, event: T_contra, callback: Optional[Callable] = None) -> Callable:
"""
Register a callback to be called only once when the event is emitted.

Expand Down Expand Up @@ -116,7 +116,7 @@ def decorator(callback: Callable) -> Callable:

return decorator

def on(self, event: T, callback: Optional[Callable] = None) -> Callable:
def on(self, event: T_contra, callback: Optional[Callable] = None) -> Callable:
"""
Register a callback to be called whenever the event is emitted.

Expand Down Expand Up @@ -168,7 +168,7 @@ def decorator(callback: Callable) -> Callable:

return decorator

def off(self, event: T, callback: Callable) -> None:
def off(self, event: T_contra, callback: Callable) -> None:
"""
Unregister a callback from an event.

Expand Down
Loading