swarm-ui: test the rain arithmetic and the swarm name loader
The rain's column maths and fade colour move from MatrixRain into shell/rain.ts, and the request-once swarm name loader from useSwarmName into shell/swarm-name.ts. Both are plain functions with their input passed in, so tests cover them; the components keep the DOM and canvas work.
This commit is contained in:
parent
01e58219cf
commit
e32b4172e4
6 changed files with 237 additions and 42 deletions
28
frontend/packages/swarm-ui/src/shell/swarm-name.ts
Normal file
28
frontend/packages/swarm-ui/src/shell/swarm-name.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Loading the swarm's display name from `GET /api/swarm`, once. The header and
|
||||
// the start page both ask for it and it does not change within a page visit,
|
||||
// so the loader remembers the first request and every caller shares it. The
|
||||
// label is cosmetic, so a failed request, an error response or an unconfigured
|
||||
// name all answer `null` instead of throwing: callers fall back to their own
|
||||
// default, as an operator who never set a name sees.
|
||||
|
||||
type Fetch = (url: string) => Promise<{
|
||||
ok: boolean;
|
||||
json: () => Promise<unknown>;
|
||||
}>;
|
||||
|
||||
export function createSwarmNameLoader(
|
||||
fetchImpl: Fetch,
|
||||
): () => Promise<string | null> {
|
||||
let pending: Promise<string | null> | null = null;
|
||||
return () => {
|
||||
if (pending === null) {
|
||||
pending = (async () => {
|
||||
const r = await fetchImpl("/api/swarm");
|
||||
if (!r.ok) return null;
|
||||
const data = (await r.json()) as { name?: string | null };
|
||||
return data.name || null;
|
||||
})().catch(() => null);
|
||||
}
|
||||
return pending;
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue