Next: , Previous: , Up: actor-lib: A standard library of sorts   [Contents][Index]


6.11 Pubsub

The (goblins actor-lib pubsub) module provides an actor to publish messages to a set of other, subscribed actors.

Constructor: ^pubsub bcom initial-subscribers …

Construct an actor with references to the other actors initial-subscribers and which has the following methods:

  • subscribe subscriber: Add subscriber to the list of subscribed actors.
  • unsubscribe subscriber: Remove subscriber from the list of subscribed actors.
  • publish args ...: Invoke all subscribers with the arguments args.
  • subscribers: Return a list of all subscribed actors.
> (use-modules (goblins actor-lib cell))
;; enter a vat
> (define a-chest (spawn ^cell 'sword))
> (define b-chest (spawn ^cell 'gold))
> (define c-chest (spawn ^cell 'wand))
> (define pubsub (spawn ^pubsub a-chest b-chest))
> ($ pubsub 'subscribers)
=> ($<local-object ^cell> #<local-object ^cell>)
> ($ pubsub 'publish 'boots)
=> #t
> ($ a-chest)
=> 'boots
> ($ b-chest)
=> 'boots
> ($ pubsub 'subscribe c-chest)
> ($ pubsub 'subscribers)
=> (#<local-object ^cell> #<local-object ^cell> #<local-object ^cell>)
> ($ pubsub 'unsubscribe b-chest)
> ($ pubsub 'publish 'crown)
=> #t
> ($ a-chest)
=> 'crown
> ($ b-chest)
=> 'boots
> ($ c-chest)
=> 'crown
Persistence Environment: pubsub-env