superseded-task-write
A run of a useTask (Vue or React) or of std's latest that changes state itself after an await, without asking its signal whether a newer run started meanwhile. On by default, as an error.
Why: a task aborts the previous run's signal when a new run starts, and drops what a superseded run returns: only the newest run's answer reaches task.state. What the callback writes itself isn't dropped. Pick a second file while the first is still being read, and the first read finishes later and opens its file over the second: the screen shows the file you didn't pick last. The callback calls nothing that takes the signal, so nothing stops it.
❌ Rejected: an older run finishes later and overwrites the newer one's
✕ Rejectedsuperseded-task-write
ts
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { ref } from "vue";
declare function readFile(name: string): Promise<string>;
export function useViewer() {
const open = ref("");
const pick = useTask(async (_signal, name: string): Promise<void> => {
open.value = await readFile(name);
});
return { open, pick };
}text
This `useTask` run changes `open.value` after the `await` on line 9 without asking its signal:
when a newer run starts meanwhile, this one still finishes, and its change lands after the newer
one's
= help: Return the value and let the task hold it (a superseded run's answer is dropped); or
stop when superseded: `if (signal.aborted) return;` after the `await`, or hand `signal` to the
awaited call✅ Accepted: return the value; the task holds the newest run's (pick.state, shown with match)
✓ Accepted0 diagnostics
ts
import { useTask } from "@evobug_com/safe-ts-vue/task";
declare function readFile(name: string, init?: { signal?: AbortSignal }): Promise<string>;
export function useViewer() {
return useTask((signal, name: string) => readFile(name, { signal }));
}✅ Also accepted: write it yourself, after asking the signal
✓ Accepted0 diagnostics
ts
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { ref } from "vue";
declare function readFile(name: string): Promise<string>;
export function useViewer() {
const open = ref("");
const pick = useTask(async (signal, name: string): Promise<void> => {
const text = await readFile(name);
if (signal.aborted) return;
open.value = text;
});
return { open, pick };
}A call given the signal (readFile(name, { signal })) rejects when a newer run aborts it, so nothing after its await runs in a superseded run: a write after it is fine too.
The same mistake one call down: the callback lends state to a function that changes it after an await of its own, and doesn't give it the signal.
❌ Rejected: open changes the viewer after it waits, and can't know the run was superseded
✕ Rejectedsuperseded-task-write
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";
declare function read(name: string): Promise<string>;
interface Viewer {
open: string;
}
async function open(viewer: Mut<Viewer>, name: string): Promise<void> {
viewer.open = await read(name);
}
export function usePicker(viewer: Mut<Viewer>) {
return useTask((_signal, name: string) => open(mut(viewer), name));
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";
declare function read(name: string): Promise<string>;
interface Viewer {
open: string;
}
async function open(viewer: Mut<Viewer>, name: string): Promise<void> {
viewer.open = await read(name);
}
export function usePicker(viewer: Mut<Viewer>) {
return useTask((_signal, name: string) => open(viewer, name));
}✅ Accepted: pass the signal on, and stop on it after the await
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";
declare function read(name: string): Promise<string>;
interface Viewer {
open: string;
}
async function open(viewer: Mut<Viewer>, name: string, signal: AbortSignal): Promise<void> {
const text = await read(name);
if (signal.aborted) return;
viewer.open = text;
}
export function usePicker(viewer: Mut<Viewer>) {
return useTask((signal, name: string) => open(mut(viewer), name, signal));
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";
declare function read(name: string): Promise<string>;
interface Viewer {
open: string;
}
async function open(viewer: Mut<Viewer>, name: string, signal: AbortSignal): Promise<void> {
const text = await read(name);
if (signal.aborted) return;
viewer.open = text;
}
export function usePicker(viewer: Mut<Viewer>) {
return useTask((signal, name: string) => open(viewer, name, signal));
}What counts
- The callback is the one written in place as the first argument of
useTask(@evobug_com/safe-ts-vue,@evobug_com/safe-ts-react) or oflatest(@evobug_com/safe-ts-std/async). Both packages andlatestdrop a superseded run's answer (they test the run's signal after the callback returns), so returning the value is always fine. - A change is an assignment,
++,deleteor a changing method (push,set,splice, ...) on state that outlives the run: something the callback captured (aref's.value, a component's state, aMutparameter of the enclosing function), module state,this, a part of one of the callback's own arguments, a local naming one of those (const list = viewer.items); andmut(x)of such state handed to a call. - After an untested
await: on some path, the run waited at anawaitthat wasn't handed the signal, and didn't test the signal since. Handed means the awaited expression mentions it (fetch(url, { signal }),load(id, signal),AbortSignal.any([signal, timeout])); the rule takes such a call to reject when the signal aborts, whichfetchand std's helpers do, and a project function given the signal is trusted to. A method on what such anawaitgave (await res.json()of a fetch given the signal) is handed it too: the body is aborted with the request. A test is readingsignal.aborted(orsignal.reason),signal.throwIfAborted()(also insideunsafe(() => ...)), or passing the signal to any call. - Lent to a function that changes it later: a call the run waits for (awaited, returned, or the arrow's body) that isn't given the signal, of a function by name (in the file, or imported) that is
asyncand uses a parameter declaredMut<T>after its firstawait; or a call after an untestedawaitto a function that declares the parameterMut<T>at all.
How it relates to the other async rules
unused-task-signal(idiomatic) asks a callback that never reads its signal to pass it to a call that could take it: a request without it keeps running. A callback whose calls take no signal isn't its business; when such a callback changes state after anawait, it is this rule's: the run has nothing to pass the signal to, and has to ask it.latest-wins-counteris the hand-written form of the same question (++runsbefore theawait, compared after) and asks forlatest(...)oruseTask.stale-across-awaitneeds a read before theawaitthat the write is made from: a different mistake (a lost update), reported on different writes.
What the rule can't see
- A callback passed by name (
useTask(load)): only one written in place is followed. - A change two calls down: a function the callback lends state to, which hands it to another
asyncfunction that changes it after waiting. Only the function the callback calls is read. - State changed through a method of an object (
store.setOpen(text),viewer.open(text)): a method call is a change only when it is one of the known changing methods (push,set, ...) on the state itself. - A signal tested through a local (
const stop = signal.abortedbefore theawait), or a callback whose signal parameter is destructured (({ aborted }) => ...): not followed. - A
for awaitloop over something given the signal: its body is taken as handed the signal for every round, whatever else it awaits.