split code up into modules and add pubsub and timers

This commit is contained in:
Travis Shears 2023-08-10 08:11:39 +02:00
parent 05c470197f
commit 001f99db7c
Signed by: travisshears
GPG key ID: D4C2E4DFAB8BABF8
7 changed files with 228 additions and 137 deletions

View file

@ -0,0 +1,16 @@
class PubSub:
def __init__(self):
self.cbs = {}
def _check_msg_type(self, msg_type):
if msg_type not in self.cbs:
self.cbs[msg_type] = []
def subscribe(self, msg_type, cb):
self._check_msg_type(msg_type)
self.cbs[msg_type].append(cb)
def publish(self, msg_type, body):
self._check_msg_type(msg_type)
print(f"publishing msg {msg_type}")
for cb in self.cbs[msg_type]:
cb(body)