-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#267): Introduced better typing to bs_rpc
- Loading branch information
1 parent
7442028
commit 15faa99
Showing
3 changed files
with
18 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
from typing import Generator | ||
from typing import Callable, Generator, Generic, TypeVar | ||
|
||
from . import msg | ||
from .channel import Channel | ||
|
||
CloseFn = Callable[[], None] | ||
T = TypeVar("T") | ||
|
||
class ChannelGenerator: | ||
def __init__(self, ch: Channel, close_fn): | ||
class ChannelGenerator(Generic[T]): | ||
def __init__(self, ch: Channel[T], close_fn: CloseFn | None): | ||
self.__ch: Channel = ch | ||
self.__close_fn = close_fn | ||
|
||
def __call__(self) -> Generator[msg.Body, None, None]: | ||
def __call__(self) -> Generator[T, None, None]: | ||
while True: | ||
ret = self.__ch.listen() | ||
if ret is None: | ||
self.__close_fn() | ||
if self.__close_fn is not None: | ||
self.__close_fn() | ||
return | ||
yield ret |