Skip to main content

Crate wasm_actor_bridge

Crate wasm_actor_bridge 

Source
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) 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

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§

CancelGuard
Dropping the guard cancels the associated token.
CancellationToken
A cancellation token that can be checked synchronously or awaited.
WorkerEvent
An event received from a Web Worker.

Enums§

BridgeError
Errors from the worker bridge.

Traits§

ActorMessage
Trait bound for types that can cross the Worker boundary.
WorkerActor
Trait for a worker-side actor that processes typed commands.