Expand description
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) orhandle.call(cmd)(RPC withCallHandle) - Receive events:
EventStream<Evt>(asyncStream) - Cancel requests: drop the
CallHandleor checkCancellationToken::is_cancelled()
§Worker Pool
For parallel processing, use WorkerPool::new(handles, RoutingStrategy::RoundRobin).
Merge event streams via futures::stream::select_all.
§Main thread
ⓘ
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
ⓘ
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);
}Re-exports§
pub use context::Context;
Modules§
- context
- Response context for worker actors.
Structs§
- Cancel
Guard - Dropping the guard cancels the associated token.
- Cancellation
Token - A cancellation token that can be checked synchronously or awaited.
- Worker
Event - An event received from a Web Worker.
Enums§
- Bridge
Error - Errors from the worker bridge.
Traits§
- Actor
Message - Trait bound for types that can cross the Worker boundary.
- Worker
Actor - Trait for a worker-side actor that processes typed commands.