A basic pubsub module for communications within an app.
This project is an excercise to help me develop skills relating to:
You would probably be better off using PyPubSub
from xpubsub import PubSub
pub = PubSub()
HashOrList = Union[Hashable, list[Hashable]]
pub.add(topic_list: HashOrList, callback: Callable):
pub.remove(topic_list: HashOrList, callback: Callable):
pub.send(topic_list: HashOrList, message: Any):
from xpubsub import PubSub
pub = PubSub()
def hello(topic, message):
print(topic, message)
def goodbye(topic, msg):
print("😭", topic, msg)
pub.add("hi", hello)
pub.add(["SHTF!", "go away"], goodbye)
pub.send("hi", "👋😍")
pub.send("SHTF!", "Head For The Hills!")
pub.send("go away", "its over")
pub.remove("go away", goodbye)
pub.send("go away", "nothing happens") # this does nothing!
Output
hi 👋😍
😭 SHTF! Head For The Hills!
😭 go away its over