Module yew_agent::worker

source ·
Expand description

This module contains the worker agent implementation.

This is a low-level implementation that uses an actor model.

Example

use serde::{Deserialize, Serialize};
use yew::prelude::*;
use yew_agent::worker::{use_worker_bridge, UseWorkerBridgeHandle};

// This would usually live in the same file as your worker
#[derive(Serialize, Deserialize)]
pub enum WorkerResponseType {
    IncrementCounter,
}
use my_worker_mod::MyWorker; // note that <MyWorker as yew_agent::Worker>::Output == WorkerResponseType
#[function_component(UseWorkerBridge)]
fn bridge() -> Html {
    let counter = use_state(|| 0);

    // a scoped block to clone the state in
    {
        let counter = counter.clone();
        // response will be of type MyWorker::Output, i.e. WorkerResponseType
        let bridge: UseWorkerBridgeHandle<MyWorker> = use_worker_bridge(move |response| match response {
            WorkerResponseType::IncrementCounter => {
                counter.set(*counter + 1);
            }
        });
    }

    html! {
        <div>
            {*counter}
        </div>
    }
}

Structs

Traits

  • Declares the behaviour of a worker.

Functions