yew_agent/reactor/mod.rs
1//! This module contains the reactor agent implementation.
2//!
3//! Reactor agents are agents that receive multiple inputs and send multiple outputs over a single
4//! bridge. A reactor is defined as an async function that takes a [ReactorScope]
5//! as the argument.
6//!
7//! The reactor scope is a stream that produces inputs from the bridge and a
8//! sink that implements an additional send method to send outputs to the connected bridge.
9//! When the bridge disconnects, the output stream and input sink will be closed.
10//!
11//! # Example
12//!
13//! ```
14//! # use serde::{Serialize, Deserialize};
15//! # #[derive(Serialize, Deserialize)]
16//! # pub struct ReactorInput {}
17//! # #[derive(Serialize, Deserialize)]
18//! # pub struct ReactorOutput {}
19//! #
20//! use futures::sink::SinkExt;
21//! use futures::stream::StreamExt;
22//! use yew_agent::reactor::{reactor, ReactorScope};
23//! #[reactor(MyReactor)]
24//! pub async fn my_reactor(mut scope: ReactorScope<ReactorInput, ReactorOutput>) {
25//! while let Some(input) = scope.next().await {
26//! // handles each input.
27//! // ...
28//! # let output = ReactorOutput { /* ... */ };
29//!
30//! // sends output
31//! if scope.send(output).await.is_err() {
32//! // sender closed, the bridge is disconnected
33//! break;
34//! }
35//! }
36//! }
37//! ```
38
39mod hooks;
40mod provider;
41
42#[doc(inline)]
43pub use gloo_worker::reactor::{
44 Reactor, ReactorBridge, ReactorRegistrar, ReactorScope, ReactorScoped, ReactorSpawner,
45};
46pub use hooks::{
47 use_reactor_bridge, use_reactor_subscription, ReactorEvent, UseReactorBridgeHandle,
48 UseReactorSubscriptionHandle,
49};
50pub use provider::ReactorProvider;
51pub(crate) use provider::ReactorProviderState;
52/// A procedural macro to create reactor agents.
53pub use yew_agent_macro::reactor;