Expand description
In-process, topic-based publish/subscribe message bus.
A subscriber is a function called with (topic, data). You register it
against a topic string and get back a Token. A publisher sends a value
to a topic. Every subscriber whose topic matches runs.
Topics are hierarchical and dot-delimited. Publishing a.b.c notifies
subscribers of a.b.c, then a.b, then a, then the wildcard *. A
subscriber on an ancestor topic receives the original leaf topic as its
first argument, not the ancestor it matched.
§Delivery modes
PubSub::publish is deferred. It queues delivery and returns right away.
Subscribers run later, when you call PubSub::process_deferred. This
mirrors a non-blocking publisher that does not wait on subscriber work.
PubSub::publish_sync runs every matching subscriber before it returns.
Both return true if the topic had at least one matching subscriber
(direct, ancestor, or *), else false. The return value is computed
before any subscriber runs.
§Panics during delivery
Default mode catches a panicking subscriber, finishes delivery to the rest,
then re-raises the first panic after the dispatch. Set
PubSub::immediate_exceptions to true to stop at the first panic and
let it propagate, skipping the remaining subscribers.
§Example
use tiny_pubsub::PubSub;
use std::cell::Cell;
use std::rc::Rc;
let bus: PubSub<&str> = PubSub::new();
let hits = Rc::new(Cell::new(0));
let h = hits.clone();
bus.subscribe("car.engine", move |_topic, _data| h.set(h.get() + 1));
assert!(bus.publish_sync("car.engine", "start"));
assert_eq!(hits.get(), 1);Structs§
- PubSub
- An in-process hierarchical-topic pub/sub message bus.
- Token
- A handle to one subscription, returned by
PubSub::subscribe.
Constants§
- WILDCARD
- The wildcard topic. A subscriber here receives every published message.