From 38d07f7111903012bbdbe7cab0195584db618a6d Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 9 Mar 2022 12:33:28 -0500 Subject: [PATCH] docs: add some more to async --- docs/guide/src/async/coroutines.md | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/guide/src/async/coroutines.md b/docs/guide/src/async/coroutines.md index 0805604d1..f59e9102f 100644 --- a/docs/guide/src/async/coroutines.md +++ b/docs/guide/src/async/coroutines.md @@ -130,17 +130,17 @@ fn Banner(cx: Scope) -> Element { Now, in our sync service, we can structure our state however we want. We only need to update the view values when ready. ```rust -enum SyncMsg { +enum SyncAction { SetUsername(String), } -async fn sync_service(mut rx: UnboundedReceiver, atoms: AtomRoot) { +async fn sync_service(mut rx: UnboundedReceiver, atoms: AtomRoot) { let username = atoms.write(USERNAME); let errors = atoms.write(ERRORS); while let Ok(msg) = rx.next().await { match msg { - SyncMsg::SetUsername(name) => { + SyncAction::SetUsername(name) => { if set_name_on_server(&name).await.is_ok() { username.set(name); } else { @@ -156,6 +156,28 @@ async fn sync_service(mut rx: UnboundedReceiver, atoms: AtomRoot) { To yield values from a coroutine, simply bring in a `UseState` handle and set the value whenever your coroutine completes its work. + +```rust +let sync_status = use_state(&cx, || Status::Launching); +let sync_task = use_coroutine(&cx, |rx: UnboundedReceiver| { + to_owned![sync_status]; + async move { + loop { + delay_ms(1000).await; + sync_status.set(Status::Working); + } + } +}) +``` + ## Automatic injection into the Context API Coroutine handles are automatically injected through the context API. `use_coroutine_handle` with the message type as a generic can be used to fetch a handle. + +```rust +fn Child(cx: Scope) -> Element { + let sync_task = use_coroutine_handle::(&cx); + + sync_task.send(SyncAction::SetUsername); +} +```