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(self, message)

Broadcasts a message to all subscribed objects.

delay_callbacks(self)

get_handler(self, subscriber, message)

ignore_callbacks(self, ignore_type)

is_subscribed(self, subscriber, message)

Test whether the subscriber has subscribed to a given message class

subscribe(self, subscriber, message_class[, …])

Subscribe an object to a type of message class.

unsubscribe(self, subscriber, message)

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

unsubscribe_all(self, subscriber)

Unsubscribe the object from any subscriptions.

Methods Documentation

broadcast(self, message)[source]

Broadcasts a message to all subscribed objects.

Parameters

message (Message) – The message to broadcast

delay_callbacks(self)[source]
get_handler(self, subscriber, message)[source]
ignore_callbacks(self, ignore_type)[source]
is_subscribed(self, 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(self, subscriber, message_class, handler=None, filter=<function Hub.<lambda> at 0x7efd3c4610d0>)[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(self, 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(self, subscriber)[source]

Unsubscribe the object from any subscriptions.