worker

Attribute Macro worker 

Source
#[worker]
Expand description

The main macro powering worker functions.

This macro accepts the following arguments:

  1. Optional: The name of the worker, typically in PascalCase. This will not be visible if using the worker function directly, but is used internally as well as if you decide to use explicit executors.

Worker functions come in four flavours. Common to all are the following requirements:

The different flavours all have slightly different usage scenarios:

FlavourRequestResponseUsecase
CallbackSingleMultipleIntegrating with synchronous code based on callbacks, for example to report progress
ChannelMultipleMultipleFor a duplex connection to a persistent worker, rather than one-off tasks
FutureSingleSingleRunning a single calculation without the need for progress updates
StreamSingleMultipleRunning an asynchronous calculation with multiple responses

§Callback

See also: CallbackWorker.

Define your worker function as such:

#[worker(MyCallbackWorker)]
/*pub?*/ /*async?*/ fn worker(
    req: MyRequest,
    callback: impl Fn(MyResponse)
)

Which will be transformed to:

/*pub?*/ async fn worker(
    req: MyRequest,
    callback: impl Fn(MyResponse) + 'static
) -> Result<(), leptos_workers::CreateWorkerError>

§Channel

See also: ChannelWorker.

Define your worker function as such:

#[worker(MyChannelWorker)]
/*pub?*/ /*async?*/ fn worker(
    init: MyInit, // Optional parameter: initialization input for the worker, requires Clone + Serialize + DeserializeOwned.
    rx: leptos_workers::Receiver<MyRequest>,
    tx: leptos_workers::Sender<MyResponse>
)

Which will be transformed to:

/*pub?*/ fn worker(init: MyInit) -> Result<
    (
        leptos_workers::Sender<MyRequest>,
        leptos_workers::Receiver<MyResponse>,
    ),
    leptos_workers::CreateWorkerError,
>

§Future

See also: FutureWorker.

Define your worker function as such:

#[worker(MyFutureWorker)]
/*pub?*/ /*async?*/ fn worker(
    req: MyRequest
) -> MyResponse

Which will be transformed to:

/*pub?*/ async fn worker(
    req: MyRequest,
) -> Result<MyResponse, leptos_workers::CreateWorkerError>

§Stream

See also: StreamWorker.

Define your worker function as such:

#[worker(MyStreamWorker)]
/*pub?*/ /*async?*/ fn worker(
    req: MyRequest
) -> impl leptos_workers::Stream<Item = MyResponse>

Which will be transformed to:

/*pub?*/ fn worker(
    req: &MyRequest,
) -> Result<
    impl leptos_workers::Stream<Item = MyResponse>,
    leptos_workers::CreateWorkerError
>

§Usage

After applying the macro to one of the above examples they can be called directly. This will use a default thread pool. Don’t forget to handle the error case if you need compatibility with older browsers.

#[derive(Clone, Serialize, Deserialize)]
pub struct MyRequest;
#[derive(Clone, Serialize, Deserialize)]
pub struct MyResponse;

#[worker(MyFutureWorker)]
pub async fn future_worker(_req: MyRequest) -> MyResponse {
    MyResponse
}
// in your component
let response = create_local_resource(|| (), move |_| {
    future_worker(MyRequest)
});

If using SSR it is important to ensure the worker only executes on the client, as the worker does not exist on the server. A few examples of wrapping functions include:

  • create_local_resource
  • create_effect
  • create_action
  • spawn_local

If using pure CSR, this is not a problem.