Build a Pub/Sub Event Bus in Larzscript
Decouple producers from consumers with dot-segmented topics and wildcard subscriptions.
A single `*` matches exactly one topic segment, `**` matches any number of remaining segments, and anything published with no matching subscriber lands in a dead-letter list instead of vanishing silently.
larzscript pkg install bus
Code
import "bus" as bus
let b = bus.new()
bus.subscribe(b, "orders.*", fn(topic, payload) { print("handler1:", topic, payload) })
bus.subscribe(b, "orders.**", fn(topic, payload) { print("handler2 (catch-all):", topic) })
print("delivered:", bus.publish(b, "orders.created", {"id": 1}))
bus.publish(b, "unrelated.thing", {})
print("dead letters:", b["dead_letters"])Output
handler1: orders.created {id: 1}
handler2 (catch-all): orders.created
delivered: 2
dead letters: [{topic: unrelated.thing, payload: {}}]Related
larzbusAn in-process pub/sub message bus with dot-segmented, wildcard topics→Retry with Exponential Backoff in LarzscriptRetry a flaky call a few times, waiting longer between each attempt, instead of failing on the first hiccup or hammering a struggling service.→Memoize a Function in LarzscriptCache a function's results so calling it twice with the same input only does the real work once.→Implement a State Machine in LarzscriptDeclare states and transitions, fire events, and let the machine reject anything that isn't a legal move - a document workflow (draft -> review -> published) in a few lines.→