Part of the unification order in the UI sweep issue (step d, first of three). Twenty files hand-rolled the same effect: a cancelled flag, a fetch, the problem body on a bad status, a catch that wraps the error. useResource does it once on top of the poller's request discipline: it aborts the request in flight when the inputs change or the page goes away, keeps the last good answer under a failed read and clears the error on the next good one. The agent details move to it (limits, linked accounts, ssh CA, permission catalogue, allowed and denied domains). Hint drops its HintTone alias for the shared Tone.
204 lines
5.8 KiB
JavaScript
204 lines
5.8 KiB
JavaScript
// The request discipline of a polled page. Run by `npm test`, which Node runs
|
|
// with its built-in test runner and type stripping, so the `.ts` module is
|
|
// imported as it is.
|
|
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import { createPoller, createResource } from "../src/ui/polled/poller.ts";
|
|
|
|
// A fetch whose answers the test settles by hand, in any order.
|
|
function manualFetch() {
|
|
const calls = [];
|
|
const fetch = (signal) =>
|
|
new Promise((resolve, reject) => {
|
|
calls.push({ signal, resolve, reject });
|
|
});
|
|
return { calls, fetch };
|
|
}
|
|
|
|
// Lets the promise callbacks of a settled request run.
|
|
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
|
|
|
function collect() {
|
|
const seen = { data: [], error: [] };
|
|
return {
|
|
seen,
|
|
onData: (d) => seen.data.push(d),
|
|
onError: (e) => seen.error.push(e),
|
|
};
|
|
}
|
|
|
|
test("a tick while a request is in flight asks for nothing", async () => {
|
|
const { calls, fetch } = manualFetch();
|
|
const sink = collect();
|
|
const poller = createPoller({ fetch, ...sink });
|
|
poller.tick();
|
|
poller.tick();
|
|
assert.equal(calls.length, 1);
|
|
calls[0].resolve("one");
|
|
await flush();
|
|
assert.deepEqual(sink.seen.data, ["one"]);
|
|
poller.tick();
|
|
assert.equal(calls.length, 2);
|
|
});
|
|
|
|
test("a refetch drops the request in flight, so an older answer never lands", async () => {
|
|
const { calls, fetch } = manualFetch();
|
|
const sink = collect();
|
|
const poller = createPoller({ fetch, ...sink });
|
|
poller.tick();
|
|
poller.refetch();
|
|
assert.equal(calls.length, 2);
|
|
assert.equal(calls[0].signal.aborted, true);
|
|
calls[1].resolve("new");
|
|
calls[0].resolve("old");
|
|
await flush();
|
|
assert.deepEqual(sink.seen.data, ["new"]);
|
|
});
|
|
|
|
test("a stopped poller aborts the request and ignores its answer", async () => {
|
|
const { calls, fetch } = manualFetch();
|
|
const sink = collect();
|
|
const poller = createPoller({ fetch, ...sink });
|
|
poller.tick();
|
|
poller.stop();
|
|
assert.equal(calls[0].signal.aborted, true);
|
|
calls[0].resolve("late");
|
|
await flush();
|
|
assert.deepEqual(sink.seen.data, []);
|
|
poller.tick();
|
|
assert.equal(calls.length, 2);
|
|
});
|
|
|
|
test("a failed request reports the error and frees the next tick", async () => {
|
|
const { calls, fetch } = manualFetch();
|
|
const sink = collect();
|
|
const poller = createPoller({ fetch, ...sink });
|
|
poller.tick();
|
|
const boom = new Error("boom");
|
|
calls[0].reject(boom);
|
|
await flush();
|
|
assert.deepEqual(sink.seen.error, [boom]);
|
|
poller.tick();
|
|
assert.equal(calls.length, 2);
|
|
});
|
|
|
|
test("the answer of a dropped request that fails is not reported", async () => {
|
|
const { calls, fetch } = manualFetch();
|
|
const sink = collect();
|
|
const poller = createPoller({ fetch, ...sink });
|
|
poller.tick();
|
|
poller.refetch();
|
|
calls[0].reject(new Error("aborted"));
|
|
await flush();
|
|
assert.deepEqual(sink.seen.error, []);
|
|
});
|
|
|
|
test("onBusy follows a request in flight, however it ends", async () => {
|
|
const { calls, fetch } = manualFetch();
|
|
const sink = collect();
|
|
const busy = [];
|
|
const poller = createPoller({ fetch, ...sink, onBusy: (b) => busy.push(b) });
|
|
poller.tick();
|
|
poller.tick();
|
|
assert.deepEqual(busy, [true]);
|
|
calls[0].resolve("one");
|
|
await flush();
|
|
assert.deepEqual(busy, [true, false]);
|
|
poller.tick();
|
|
calls[1].reject(new Error("boom"));
|
|
await flush();
|
|
assert.deepEqual(busy, [true, false, true, false]);
|
|
poller.tick();
|
|
poller.stop();
|
|
assert.deepEqual(busy, [true, false, true, false, true, false]);
|
|
poller.stop();
|
|
assert.equal(busy.length, 6);
|
|
});
|
|
|
|
// The state behind `useResource`.
|
|
function resourceRun() {
|
|
const m = manualFetch();
|
|
const states = [];
|
|
const resource = createResource({
|
|
fetch: m.fetch,
|
|
problemOf: (e) => `problem: ${e}`,
|
|
onState: (s) => states.push(s),
|
|
});
|
|
return { m, states, resource };
|
|
}
|
|
|
|
test("a resource starts loading with nothing, and a good answer fills it", async () => {
|
|
const { m, resource } = resourceRun();
|
|
assert.deepEqual(resource.state(), {
|
|
data: null,
|
|
error: null,
|
|
loading: true,
|
|
});
|
|
resource.load();
|
|
m.calls[0].resolve("one");
|
|
await flush();
|
|
assert.deepEqual(resource.state(), {
|
|
data: "one",
|
|
error: null,
|
|
loading: false,
|
|
});
|
|
});
|
|
|
|
test("a failed read keeps the last good data and the next good answer clears the error", async () => {
|
|
const { m, resource } = resourceRun();
|
|
resource.load();
|
|
m.calls[0].resolve("one");
|
|
await flush();
|
|
resource.load();
|
|
m.calls[1].reject("boom");
|
|
await flush();
|
|
assert.deepEqual(resource.state(), {
|
|
data: "one",
|
|
error: "problem: boom",
|
|
loading: false,
|
|
});
|
|
resource.load();
|
|
m.calls[2].resolve("two");
|
|
await flush();
|
|
assert.deepEqual(resource.state(), {
|
|
data: "two",
|
|
error: null,
|
|
loading: false,
|
|
});
|
|
});
|
|
|
|
test("a read for old inputs never lands once the inputs changed", async () => {
|
|
const { m, resource } = resourceRun();
|
|
resource.load();
|
|
resource.load();
|
|
assert.equal(m.calls[0].signal.aborted, true);
|
|
m.calls[0].resolve("stale");
|
|
m.calls[1].resolve("fresh");
|
|
await flush();
|
|
assert.equal(resource.state().data, "fresh");
|
|
});
|
|
|
|
test("stop drops the request in flight, so an unmounted page hears nothing", async () => {
|
|
const { m, states, resource } = resourceRun();
|
|
resource.load();
|
|
const before = states.length;
|
|
resource.stop();
|
|
m.calls[0].resolve("late");
|
|
await flush();
|
|
assert.equal(m.calls[0].signal.aborted, true);
|
|
assert.equal(resource.state().data, null);
|
|
assert.equal(
|
|
states.slice(before).some((s) => s.data !== null),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("setData patches what is shown without a request", async () => {
|
|
const { m, resource } = resourceRun();
|
|
resource.load();
|
|
m.calls[0].resolve([1]);
|
|
await flush();
|
|
resource.setData((prev) => [...(prev ?? []), 2]);
|
|
assert.deepEqual(resource.state().data, [1, 2]);
|
|
assert.equal(m.calls.length, 1);
|
|
});
|