Skip to content

Commit

Permalink
Improve message_handler documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
azymohliad committed Jul 28, 2024
1 parent 05e7e75 commit 78eba71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 6 additions & 1 deletion xilem/examples/data_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ use xilem::{
};

struct AppData {
proxy_sender: mpsc::SyncSender<MessageProxy<i32>>,
number: i32,
// Used to send MessageProxy from message_handler view to data_thread
proxy_sender: mpsc::SyncSender<MessageProxy<i32>>,
}

fn app_logic(data: &mut AppData) -> impl WidgetView<AppData> {
fork(
label(format!("Number: {}", &data.number)),
message_handler(
|data: &mut AppData, proxy: MessageProxy<i32>| {
// 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;
},
),
)
}

fn data_thread(proxy_receiver: mpsc::Receiver<MessageProxy<i32>>) {
// 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;
Expand Down
16 changes: 8 additions & 8 deletions xilem/src/view/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M, F, H, State, Action>(
store_proxy: F,
handle_event: H,
Expand Down

0 comments on commit 78eba71

Please sign in to comment.