use-truapi
Host

useWorkerOperation

Keep a background Worker runtime alive while an operation is pending

useWorkerOperation wraps the Worker domain for products that run as a background worker: the host keeps the runtime alive while at least one operation is open, which is how a worker outlives the surface that started it. begin(label?) opens an operation and resolves its id, end(id) closes it (idempotent, so a retry after an ambiguous failure is safe), and run(work, label?) wraps an async function in one, ending it however the work settles.

Only meaningful inside a worker execution; a plain app surface has nothing to keep alive. Host-only: standalone every call rejects with HostUnavailableError.

Usage

import { useWorkerOperation } from "@use-truapi/react";

function Sync() {
  const { run } = useWorkerOperation();
  async function onSync() {
    await run(() => syncEverything(), "sync");
  }
  return <button type="button" onClick={onSync}>Sync</button>;
}
<script setup lang="ts">
import { useWorkerOperation } from "@use-truapi/vue";

const { run } = useWorkerOperation();
const onSync = () => run(() => syncEverything(), "sync");
</script>

<template>
  <button type="button" @click="onSync">Sync</button>
</template>

API reference

React

function useWorkerOperation(): WorkerOperationApi;

Worker-lifecycle bookkeeping for products that run as a background Worker: wrap long-running work in an operation so the host does not stop the runtime mid-way. Host-only; every call rejects with HostUnavailableError standalone.

Returns

WorkerOperationApi

Vue

function useWorkerOperation(): WorkerOperationApi;

Worker-lifecycle bookkeeping for products that run as a background Worker: wrap long-running work in an operation so the host does not stop the runtime mid-way. Host-only; every call rejects with HostUnavailableError standalone.

Returns

WorkerOperationApi

On this page