diff --git a/xilem/examples/data_thread.rs b/xilem/examples/data_thread.rs index f9778687e..642e50361 100644 --- a/xilem/examples/data_thread.rs +++ b/xilem/examples/data_thread.rs @@ -10,8 +10,9 @@ use xilem::{ }; struct AppData { - proxy_sender: mpsc::SyncSender>, number: i32, + // Used to send MessageProxy from message_handler view to data_thread + proxy_sender: mpsc::SyncSender>, } fn app_logic(data: &mut AppData) -> impl WidgetView { @@ -19,9 +20,11 @@ fn app_logic(data: &mut AppData) -> impl WidgetView { label(format!("Number: {}", &data.number)), message_handler( |data: &mut AppData, proxy: MessageProxy| { + // Send message proxy to the data_thread data.proxy_sender.send(proxy).unwrap(); }, |data: &mut AppData, msg: i32| { + // Receive data from the data_thread data.number = msg; }, ), @@ -29,7 +32,9 @@ fn app_logic(data: &mut AppData) -> impl WidgetView { } fn data_thread(proxy_receiver: mpsc::Receiver>) { + // Wait for the MessageProxy if let Ok(proxy) = proxy_receiver.recv() { + // Generate data and send it to the message_handler view let mut number = 0; while let Ok(()) = proxy.message(number) { number += 1; diff --git a/xilem/src/view/message_handler.rs b/xilem/src/view/message_handler.rs index 64f30ed23..7299421e6 100644 --- a/xilem/src/view/message_handler.rs +++ b/xilem/src/view/message_handler.rs @@ -9,18 +9,18 @@ use xilem_core::{ use crate::ViewCtx; -/// No-element view which allows to update app-data in response to -/// asynchronous user messages. +/// No-element view which allows to update app state in response to +/// asynchronous user messages, for example from another thread. /// /// `store_proxy` serves as a way to obtain [`MessageProxy`], which can then -/// be used to send messages to self. -/// It is given a mutable reference to the app data and a proxy, so the proxy -/// can be saved to the app data here, or sent to another thread for example. -/// Note, it is always called only once, changes to the app data won't trigger -/// `store_proxy` to rerun. +/// be used to send messages to this view. +/// It is given a mutable reference to the app state and a message proxy, so +/// the proxy can be e.g. saved to the app state here, or sent to another thread. +/// Note, `store_proxy` is called once, shortly after the view is built. +/// Changes to the app state won't it to rerun. /// /// `handle_event` receives messages from the aforementioned `MessageProxy`, -/// along with a mutable reference to the app data. +/// along with a mutable reference to the app state. pub fn message_handler( store_proxy: F, handle_event: H,