Vize

use-scheduler-post-task

Post prioritized tasks with the Prioritized Task Scheduling API.

Package @vizejs/composable/use-scheduler-post-task
Own the source vize lib pull composable:use-scheduler-post-task
Runtime exports useSchedulerPostTask
Gzip budget 2816 B

Minimal setup

Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).

vp install @vizejs/composable

Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.

import { useSchedulerPostTask } from "@vizejs/composable/use-scheduler-post-task";

Try it

Run tasks by priority

Queue work with user-blocking, user-visible and background priorities.

Open the interactive example

What to observe: Queue three tasks runs respond to the click, then update the list, then prefetch next page, regardless of the order they were queued.

abort() cancels pending tasks and yield() lets the browser handle input between chunks of work.

Compare the initial and interacted states
Run tasks by priority: initial state
Initial state
Run tasks by priority: state after the verified interactions
After the verified interactions

Copy the example

Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.

<script setup lang="ts">
import { ref } from "vue";
import { useMounted } from "@vizejs/composable/use-mounted";
import { useSchedulerPostTask } from "@vizejs/composable/use-scheduler-post-task";

const { postTask, supported } = useSchedulerPostTask();
const order = ref<string[]>([]);
const mounted = useMounted();

async function runTasks() {
  order.value = [];
  await Promise.all([
    postTask(() => order.value.push("prefetch next page"), { priority: "background" }),
    postTask(() => order.value.push("update the list"), { priority: "user-visible" }),
    postTask(() => order.value.push("respond to the click"), { priority: "user-blocking" }),
  ]);
}
</script>

<template>
  <div class="composable-example">
    <p>Schedule work by priority so the most urgent task runs first.</p>
    <button type="button" @click="runTasks">Queue three tasks</button>
    <ol aria-label="Run order">
      <li v-for="task in order" :key="task">{{ task }}</li>
    </ol>
    <output aria-live="polite">{{
      !mounted
        ? "Checking the scheduler…"
        : supported
          ? "Using the browser's task scheduler."
          : "Using a timer fallback."
    }}</output>
  </div>
</template>

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
useSchedulerPostTask timing experimental deterministic-fallback caller-managed caller, reactive-scope web, desktop AbortController, window tryOnScopeDispose

API

useSchedulerPostTask

Post prioritized tasks with the Prioritized Task Scheduling API. Every task gets its own controller (a TaskController when available), so setPriority re-prioritizes pending tasks and abort rejects them; pending tasks are aborted when the owning reactive scope stops (their promises reject with an AbortError; outside a scope the caller owns abort()). Fallback without scheduler: tasks run from setTimeout(delay) in timer (FIFO) order. Priorities are ignored, so a later "user-blocking" task does not overtake an earlier "background" one, and tasks do not yield to rendering or input the way native tasks do. Server rendering: nothing is scheduled, supported is false and postTask rejects.

function useSchedulerPostTask( options: UseSchedulerPostTaskOptions = {}, ): SchedulerPostTaskControls
const { postTask } = useSchedulerPostTask();
const total = await postTask(() => expensiveSum(rows), { priority: "background" });

Types

SchedulerPostTaskInit

Options passed to SchedulerHost.postTask.

Member Type Description
priority? SchedulerTaskPriority Fixed task priority; overrides the signal's priority.
delay? number Delay before the task is queued, in milliseconds.
signal? AbortSignal Aborts the task (a TaskSignal also carries a mutable priority).

SchedulerHost

Minimal scheduler global.

Member Type Description
postTask (callback: () => Result | PromiseLike<Result>, options?: SchedulerPostTaskInit) => Promise<Result> Queue callback; resolves with its result, rejects with the abort reason.
yield? () => Promise<void> Yield to the event loop, continuing with inherited priority.

TaskControllerLike

Minimal TaskController instance.

Member Type Description
signal AbortSignal Signal handed to postTask.
abort (reason?: unknown) => void Abort every task using this controller's signal.
setPriority (priority: SchedulerTaskPriority) => void Change the priority of every task using this controller's signal.

UseSchedulerPostTaskOptions

Options for useSchedulerPostTask.

Member Type Description
scheduler? MaybeRefOrGetter<SchedulerHost | null | undefined> Prioritized Task Scheduling capability for alternate runtimes and tests.
TaskController? MaybeRef<TaskControllerHost | null | undefined> TaskController constructor. A ref (not a getter) because the host is a constructor function. Without it tasks use a plain AbortController and receive the priority explicitly, so setPriority cannot re-prioritize them.
timers? MaybeRefOrGetter<TimeoutScheduler | null | undefined> Timers used when scheduler is missing. Without timers (server rendering) postTask rejects.
priority? SchedulerTaskPriority Initial priority of tasks posted without an explicit priority.

PostTaskCallOptions

Per-call options of SchedulerPostTaskControls.postTask.

Member Type Description
priority? SchedulerTaskPriority Fixed priority for this task; setPriority no longer affects it.
delay? number Delay before the task is queued, in milliseconds.
signal? AbortSignal Aborts this task only.

SchedulerPostTaskControls

Reactive state and actions returned by useSchedulerPostTask.

Member Type Description
supported ComputedRef<boolean> Whether the native scheduler.postTask API is available.
priority Readonly<ShallowRef<SchedulerTaskPriority>> Priority applied to tasks posted without an explicit priority.
pending Readonly<ShallowRef<number>> Number of posted tasks that have not settled.
postTask <Result>( callback: () => Result | PromiseLike<Result>, options?: PostTaskCallOptions, ) => Promise<Result> Post a task.
setPriority (priority: SchedulerTaskPriority) => void Re-prioritize the composable and every pending task without a fixed priority.
abort (reason?: unknown) => void Abort every pending task of this composable.
yield () => Promise<void> Yield to the event loop: scheduler.yield(), else a zero-delay timer, else a resolved promise.