Skip to content

Instantly share code, notes, and snippets.

@walison17
Last active July 19, 2020 18:47
Show Gist options
  • Save walison17/5dfc9b696cda14aad1251727e5b45ffb to your computer and use it in GitHub Desktop.
Save walison17/5dfc9b696cda14aad1251727e5b45ffb to your computer and use it in GitHub Desktop.
from collections import defaultdict
from typing import Callable
REGISTRY: defaultdict = defaultdict(list)
def channel_handler(channel: str) -> Callable:
def decorator(func: Callable) -> Callable:
REGISTRY[channel].append(func)
return func
return decorator
def run_handlers(notify: Notify) -> None:
handlers = REGISTRY.get(notify.channel, [])
if not handlers:
return
for handler in handlers:
handler(payload=notify.payload)
@channel_handler('top_score')
def notify_customers(payload: dict) -> None:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment