1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! `wasm-actor-bridge` — typed, zero-copy Web Worker bridge for Rust/WASM.
//!
//! # Architecture
//!
//! - Define an actor: `impl WorkerActor for MyWorker { ... }`
//! - Spawn via `SupervisorBuilder::new(url).evt_capacity(32).init(payload).build()`
//! - Send commands: `handle.send(cmd)` (fire-and-forget) or `handle.call(cmd)` (RPC with `CallHandle`)
//! - Receive events: `EventStream<Evt>` (async `Stream`)
//! - Cancel requests: drop the `CallHandle` or check `CancellationToken::is_cancelled()`
//!
//! # Worker Pool
//!
//! For parallel processing, use `WorkerPool::new(handles, RoutingStrategy::RoundRobin)`.
//! Merge event streams via `futures::stream::select_all`.
//!
//! # Main thread
//!
//! ```rust,ignore
//! let (handle, mut events) = SupervisorBuilder::<MyCmd, MyEvt, MyInit>::new("/worker.js")
//! .evt_capacity(32)
//! .init(my_init)
//! .build()?;
//!
//! // Fire-and-forget.
//! handle.send(MyCmd::Ping)?;
//!
//! // RPC (request/response) — cancel-on-drop.
//! let response: MyEvt = handle.call(MyCmd::FetchPage { start: 0 }).await?;
//! ```
//!
//! # Worker thread
//!
//! ```rust,ignore
//! struct MyActor;
//!
//! impl WorkerActor for MyActor {
//! type Init = MyInit;
//! type Cmd = MyCmd;
//! type Evt = MyEvt;
//!
//! async fn handle(&mut self, cmd: MyCmd, ctx: Context<MyEvt>, token: CancellationToken) {
//! if token.is_cancelled() { return; }
//! ctx.respond(MyEvt::Pong);
//! }
//! }
//!
//! #[wasm_bindgen(start)]
//! pub fn main() {
//! run_actor_loop(MyActor);
//! }
//! ```
pub
pub use WorkerActor;
pub use ;
pub use Context;
pub use BridgeError;
pub use ;
pub use SupervisorBuilder;
pub use run_actor_loop;
pub use ;
pub use ;
pub use spawn_worker;
pub use EventStream;