Created
June 26, 2020 21:23
-
-
Save marc-x-andre/1c55b3fafd1d00cfdaa205ec53a08cf3 to your computer and use it in GitHub Desktop.
A event emitter in Python
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
from typing import Dict | |
class EventEmitter: | |
def __init__(self): | |
self._callbacks: Dict[str, callable] = {} | |
def on(self, event_name, function): | |
self._callbacks[event_name] = self._callbacks.get(event_name, []) + [function] | |
return function | |
def emit(self, event_name, *args, **kwargs): | |
[function(*args, **kwargs) for function in self._callbacks.get(event_name, [])] | |
def off(self, event_name, function): | |
self._callbacks.get(event_name, []).remove(function) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment