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)