Struct relm4::component::MessageBroker
source · pub struct MessageBroker<C: Component> { /* private fields */ }
Expand description
A type that can be used in static variables to pass messages to components.
The primary use-case for this type is to communicate between components on different levels.
Imagine you have three components: A, B and C.
A and B are children of the main application, but C is a child of B.
If C wants to pass a message to A, it relies on B to forward that message to A.
This is not great because B has nothing to do this message but has to implement additional
logic only to pass the message through.
MessageBroker
allows you to use statics to remove this limitation.
Note
MessageBroker
will not forward any messages until you initialize them with
ComponentBuilder::launch_with_broker()
.
Only initialize the message broker once!
use relm4::{MessageBroker, Component};
static MY_COMPONENT: MessageBroker<MyComponent> = MessageBroker::new();
// Initialize the component and the message broker with `launch_with_broker`.
let controller = MyComponent::builder().launch_with_broker((), &MY_COMPONENT).detach();
Implementations§
source§impl<C: Component> MessageBroker<C>
impl<C: Component> MessageBroker<C>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new MessageBroker
.
The returned message broker will not forward messages until it’s initialized.