Hub#

class glue.core.hub.Hub(*args)[source]#

Bases: object

The hub manages communication between subscribers.

Objects subscribe() to receive specific message types. When a message is passed to broadcast(), the hub observes the following protocol:

  • For each subscriber, it looks for a message class subscription that is a parent class of the input message type (if several are found, the most-subclassed one is chosen)

  • If one is found, it calls the subscriptions filter(message) class (if provided)

  • If filter(message) == True, it calls handler(message) (or notify(message) if handler wasn’t provided).

Any arguments that are passed to Hub will be registered to the new hub object.

Methods Summary

broadcast(message)

Broadcasts a message to all subscribed objects.

delay_callbacks()

get_handler(subscriber, message)

ignore_callbacks(ignore_type)

is_subscribed(subscriber, message)

Test whether the subscriber has subscribed to a given message class

subscribe(subscriber, message_class[, ...])

Subscribe an object to a type of message class.

unsubscribe(subscriber, message)

Remove a (subscriber,message) pair from subscription list.

unsubscribe_all(subscriber)

Unsubscribe the object from any subscriptions.

Methods Documentation

broadcast(message)[source]#

Broadcasts a message to all subscribed objects.

Parameters:

message (Message) – The message to broadcast

delay_callbacks()[source]#
get_handler(subscriber, message)[source]#
ignore_callbacks(ignore_type)[source]#
is_subscribed(subscriber, message)[source]#

Test whether the subscriber has subscribed to a given message class

Parameters:
  • subscriber – The subscriber to test

  • message – The message class to test

Returns:

True if the subscriber/message pair have been subscribed to the hub

subscribe(subscriber, message_class, handler=None, filter=<function Hub.<lambda>>)[source]#

Subscribe an object to a type of message class.

Parameters:
  • subscriber (HubListener) – The subscribing object

  • message_class – A Message class to subscribe to

  • handler – An optional function of the form handler(message) that will receive the message on behalf of the subscriber. If not provided, this defaults to the HubListener’s notify method

  • filter – An optional function of the form filter(message). Messages are only passed to the subscriber if filter(message) == True. The default is to always pass messages.

Raises:

InvalidMessage: If the input class isn’t a Message class

InvalidSubscriber: If the input subscriber isn’t a HubListener object.

unsubscribe(subscriber, message)[source]#

Remove a (subscriber,message) pair from subscription list. The handler originally attached to the subscription will no longer be called when broadcasting messages of type message

unsubscribe_all(subscriber)[source]#

Unsubscribe the object from any subscriptions.