Hub#
- class glue.core.hub.Hub(*args)#
Bases:
objectThe hub manages communication between subscribers.
Objects
subscribe()to receive specific message types. When a message is passed tobroadcast(), 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.
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)#
Broadcasts a message to all subscribed objects.
- Parameters:
message (
Message) – The message to broadcast
- 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
- 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>>, priority=10)#
Subscribe an object to a type of message class.
- Parameters:
subscriber (
HubListener) – The subscribing objectmessage_class – A
Messageclass to subscribe tohandler – 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.
priority – An optional integer to set the priority of the handler. Handlers are sorted such that higher priority handlers get called first when broadcasting a message.
- Raises:
InvalidMessage: If the input class isn’t a
MessageclassInvalidSubscriber: If the input subscriber isn’t a HubListener object.
- unsubscribe(subscriber, message)#
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)#
Unsubscribe the object from any subscriptions.