yew_agent

Module 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§

HandlerId
Identifier to send output to bridges.
UseWorkerBridgeHandle
Hook handle for the use_worker_bridge hook.
UseWorkerSubscriptionHandle
Hook handle for the use_worker_subscription hook.
WorkerBridge
A connection manager for components interaction with workers.
WorkerDestroyHandle
A handle that closes the worker when it is dropped.
WorkerProvider
The Worker Agent Provider.
WorkerProviderProps
Properties for WorkerProvider.
WorkerRegistrar
A Worker Registrar.
WorkerScope
This struct holds a reference to a component and to a global scheduler.

Traits§

Worker
Declares the behaviour of a worker.

Functions§

use_worker_bridge
A hook to bridge to a Worker.
use_worker_subscription
A hook to subscribe to the outputs of a Worker agent.