Event Dispatchers¶
A set of classes for dispatching events, they can be imported from fluid.utils.dispatcher
:
fluid.utils.dispatcher.Event
¶
Bases: NamedTuple
tag
class-attribute
instance-attribute
¶
The event tag - for registering multiple handlers for a given event type
from_string_or_event
classmethod
¶
from_string
classmethod
¶
PARAMETER | DESCRIPTION |
---|---|
event
|
The event string has the form {event_type} or {event_type}.{event_tag}
TYPE:
|
Source code in fluid/utils/dispatcher.py
fluid.utils.dispatcher.BaseDispatcher
¶
Bases: Generic[MessageType, MessageHandlerType]
, ABC
Base generic abstract class for dispatchers
Source code in fluid/utils/dispatcher.py
register_handler
¶
Register a handler for the given event
It is possible to register multiple handlers for the same event type by providing a different tag for each handler.
For example, to register two handlers for the event type foo
:
dispatcher.register_handler("foo.first", handler1)
dispatcher.register_handler("foo.second", handler2)
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to register the handler for
TYPE:
|
handler
|
The handler to register
TYPE:
|
Source code in fluid/utils/dispatcher.py
unregister_handler
¶
Unregister a handler for the given event
It returns the handler that was unregistered or None
if no handler was
registered for the given event.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to unregister the handler
TYPE:
|
Source code in fluid/utils/dispatcher.py
get_handlers
¶
Get all event handlers for the given message
This method returns a dictionary of all handlers registered for the given
message type. If no handlers are registered for the message type, it returns
None
.
PARAMETER | DESCRIPTION |
---|---|
message
|
The message to get the handlers for
TYPE:
|
Source code in fluid/utils/dispatcher.py
fluid.utils.dispatcher.Dispatcher
¶
Bases: BaseDispatcher[MessageType, Callable[[MessageType], None]]
Dispatcher for sync handlers
Source code in fluid/utils/dispatcher.py
register_handler
¶
Register a handler for the given event
It is possible to register multiple handlers for the same event type by providing a different tag for each handler.
For example, to register two handlers for the event type foo
:
dispatcher.register_handler("foo.first", handler1)
dispatcher.register_handler("foo.second", handler2)
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to register the handler for
TYPE:
|
handler
|
The handler to register
TYPE:
|
Source code in fluid/utils/dispatcher.py
unregister_handler
¶
Unregister a handler for the given event
It returns the handler that was unregistered or None
if no handler was
registered for the given event.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to unregister the handler
TYPE:
|
Source code in fluid/utils/dispatcher.py
get_handlers
¶
Get all event handlers for the given message
This method returns a dictionary of all handlers registered for the given
message type. If no handlers are registered for the message type, it returns
None
.
PARAMETER | DESCRIPTION |
---|---|
message
|
The message to get the handlers for
TYPE:
|
Source code in fluid/utils/dispatcher.py
event_type
abstractmethod
¶
dispatch
¶
dispatch the message to all handlers
It returns the number of handlers that were called
Source code in fluid/utils/dispatcher.py
fluid.utils.dispatcher.AsyncDispatcher
¶
Bases: BaseDispatcher[MessageType, Callable[[MessageType], Awaitable[None]]]
Dispatcher for async handlers
Source code in fluid/utils/dispatcher.py
register_handler
¶
Register a handler for the given event
It is possible to register multiple handlers for the same event type by providing a different tag for each handler.
For example, to register two handlers for the event type foo
:
dispatcher.register_handler("foo.first", handler1)
dispatcher.register_handler("foo.second", handler2)
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to register the handler for
TYPE:
|
handler
|
The handler to register
TYPE:
|
Source code in fluid/utils/dispatcher.py
unregister_handler
¶
Unregister a handler for the given event
It returns the handler that was unregistered or None
if no handler was
registered for the given event.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to unregister the handler
TYPE:
|
Source code in fluid/utils/dispatcher.py
get_handlers
¶
Get all event handlers for the given message
This method returns a dictionary of all handlers registered for the given
message type. If no handlers are registered for the message type, it returns
None
.
PARAMETER | DESCRIPTION |
---|---|
message
|
The message to get the handlers for
TYPE:
|
Source code in fluid/utils/dispatcher.py
event_type
abstractmethod
¶
dispatch
async
¶
Dispatch the message and wait for all handlers to complete
It returns the number of handlers that were called