Add add/remove topic validator functions

This commit is contained in:
NIC619 2019-08-04 10:44:12 +08:00
parent 3973f1d13c
commit b1f4813195
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -24,6 +24,9 @@ def get_msg_id(msg: rpc_pb2.Message) -> Tuple[bytes, bytes]:
TopicValidator = namedtuple("TopicValidator", ["validator", "is_async"])
ValidatorFn = Union[Callable[[ID, rpc_pb2.Message], bool], Awaitable[None]]
class Pubsub:
host: IHost
@ -158,6 +161,15 @@ class Pubsub:
# Force context switch
await asyncio.sleep(0)
def add_topic_validator(
self, topic: str, validator: ValidatorFn, is_async_validator: bool
) -> None:
self.topic_validators[topic] = TopicValidator(validator, is_async_validator)
def remove_topic_validator(self, topic: str) -> None:
if topic in self.topic_validators:
del self.topic_validators[topic]
async def stream_handler(self, stream: INetStream) -> None:
"""
Stream handler for pubsub. Gets invoked whenever a new stream is created