mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
* refactored : host_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : network_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : notifee_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : net_connection_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored: raw_connection_interface, secure_conn_interface and stream_muxer abc.py * refactored: addrbook_interface * refactored :peerdata_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :peermetadata_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_client_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_communicator_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_muxer_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :interfaces Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :security_transport_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :listener_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * moved all interfaces and typing files Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * fixed documentation and moved pubsub abc.py Co-authored-by: Khwahish Patel <khwahish.p1@ahduni.edu.in> * added exclude-members in custom_types docs * added : newsfragment for moving all interfaces to libp2p.abc --------- Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> Co-authored-by: Mystical <125946525+mystical-prog@users.noreply.github.com>
60 lines
1.3 KiB
Python
60 lines
1.3 KiB
Python
from collections.abc import (
|
|
AsyncIterator,
|
|
)
|
|
from types import (
|
|
TracebackType,
|
|
)
|
|
from typing import (
|
|
Optional,
|
|
Type,
|
|
)
|
|
|
|
import trio
|
|
|
|
from libp2p.abc import (
|
|
ISubscriptionAPI,
|
|
)
|
|
from libp2p.custom_types import (
|
|
UnsubscribeFn,
|
|
)
|
|
|
|
from .pb import (
|
|
rpc_pb2,
|
|
)
|
|
|
|
|
|
class BaseSubscriptionAPI(ISubscriptionAPI):
|
|
async def __aenter__(self) -> "BaseSubscriptionAPI":
|
|
await trio.lowlevel.checkpoint()
|
|
return self
|
|
|
|
async def __aexit__(
|
|
self,
|
|
exc_type: "Optional[Type[BaseException]]",
|
|
exc_value: "Optional[BaseException]",
|
|
traceback: "Optional[TracebackType]",
|
|
) -> None:
|
|
await self.unsubscribe()
|
|
|
|
|
|
class TrioSubscriptionAPI(BaseSubscriptionAPI):
|
|
receive_channel: "trio.MemoryReceiveChannel[rpc_pb2.Message]"
|
|
unsubscribe_fn: UnsubscribeFn
|
|
|
|
def __init__(
|
|
self,
|
|
receive_channel: "trio.MemoryReceiveChannel[rpc_pb2.Message]",
|
|
unsubscribe_fn: UnsubscribeFn,
|
|
) -> None:
|
|
self.receive_channel = receive_channel
|
|
self.unsubscribe_fn = unsubscribe_fn
|
|
|
|
async def unsubscribe(self) -> None:
|
|
await self.unsubscribe_fn()
|
|
|
|
def __aiter__(self) -> AsyncIterator[rpc_pb2.Message]:
|
|
return self.receive_channel.__aiter__()
|
|
|
|
async def get(self) -> rpc_pb2.Message:
|
|
return await self.receive_channel.receive()
|