This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew_agent/worker/
mod.rs

1//! This module contains the worker agent implementation.
2//!
3//! This is a low-level implementation that uses an actor model.
4//!
5//! # Example
6//!
7//! ```
8//! # mod example {
9//! use serde::{Deserialize, Serialize};
10//! use yew::prelude::*;
11//! use yew_agent::worker::{use_worker_bridge, UseWorkerBridgeHandle};
12//!
13//! // This would usually live in the same file as your worker
14//! #[derive(Serialize, Deserialize)]
15//! pub enum WorkerResponseType {
16//!     IncrementCounter,
17//! }
18//! # mod my_worker_mod {
19//! #   use yew_agent::worker::{HandlerId, WorkerScope};
20//! #   use super::WorkerResponseType;
21//! #   pub struct MyWorker {}
22//! #
23//! #   impl yew_agent::worker::Worker for MyWorker {
24//! #       type Input = ();
25//! #       type Output = WorkerResponseType;
26//! #       type Message = ();
27//! #
28//! #       fn create(scope: &WorkerScope<Self>) -> Self {
29//! #           MyWorker {}
30//! #       }
31//! #
32//! #       fn update(&mut self, scope: &WorkerScope<Self>, _msg: Self::Message) {
33//! #           // do nothing
34//! #       }
35//! #
36//! #       fn received(&mut self, scope: &WorkerScope<Self>, _msg: Self::Input, id: HandlerId) {
37//! #           scope.respond(id, WorkerResponseType::IncrementCounter);
38//! #       }
39//! #   }
40//! # }
41//! use my_worker_mod::MyWorker; // note that <MyWorker as yew_agent::Worker>::Output == WorkerResponseType
42//! #[function_component(UseWorkerBridge)]
43//! fn bridge() -> Html {
44//!     let counter = use_state(|| 0);
45//!
46//!     // a scoped block to clone the state in
47//!     {
48//!         let counter = counter.clone();
49//!         // response will be of type MyWorker::Output, i.e. WorkerResponseType
50//!         let bridge: UseWorkerBridgeHandle<MyWorker> = use_worker_bridge(move |response| match response {
51//!             WorkerResponseType::IncrementCounter => {
52//!                 counter.set(*counter + 1);
53//!             }
54//!         });
55//!     }
56//!
57//!     html! {
58//!         <div>
59//!             {*counter}
60//!         </div>
61//!     }
62//! }
63//! # }
64//! ```
65
66mod hooks;
67mod provider;
68
69#[doc(inline)]
70pub use gloo_worker::{
71    HandlerId, Worker, WorkerBridge, WorkerDestroyHandle, WorkerRegistrar, WorkerScope,
72};
73pub use hooks::{
74    use_worker_bridge, use_worker_subscription, UseWorkerBridgeHandle, UseWorkerSubscriptionHandle,
75};
76pub(crate) use provider::WorkerProviderState;
77pub use provider::{WorkerProvider, WorkerProviderProps};