def discard(cls, callback, sender=None): """Similar to `Message.unsubscribe`, but does not raise an error if the callback has not been registered before. """ if sender: MessageBus().discard_locally(cls, sender, callback) else: MessageBus().discard_globally(cls, callback)
def subscribe(cls, callback, sender=None): """Register a callback to be called whenever a message of this type is send. callback - Callable that receives an instance of a message as only argument. sender - If specified, the callback receives only messages that originated from sender. By default, all messages are received. Example: >>> def cb(msg): ... print 'Received', msg >>> MessageClass.subscribe(cb) # Global >>> MessageClass.subscribe(cb, sender=foo) # Specific sender """ if sender: MessageBus().subscribe_locally(cls, sender, callback) else: MessageBus().subscribe_globally(cls, callback)
def unsubscribe(cls, callback, sender=None): """Stop your subscription of this message type for the specified callback. callback - Callable that receives an instance of a message as only argument. The same you've been using with `Message.subscribe`. sender - If specified, the subscription will only be stopped for messages from this sender. By default, all subscriptions are ended. Note: There has to be a subscription, otherwise an error will be raised. Example: >>> MessageClass.subscribe(cb) >>> MessageClass.broadcast('sender') message received >>> MessageClass.unsubscribe(cb) >>> MessageClass.broadcast('sender') """ if sender: MessageBus().unsubscribe_locally(cls, sender, callback) else: MessageBus().unsubscribe_globally(cls, callback)
def broadcast(cls, *args): """Send a message that is initialized with `args`. The first argument is always a sender, the number of arguments has to be N + 1, with N being the number of arguments defined on the message class. Example: >>> class Foo(Message): ... arguments = ('a', 'b', ) >>> Foo.broadcast('sender', 1, 2) """ MessageBus().broadcast(cls(*args))
def discard(cls, callback): """Similar to `Message.unsubscribe`, but does not raise an error if the callback has not been registered before. """ MessageBus().discard_globally(cls, callback)