swarm-ui: the refresh picker shows a spinner in place of its clock while its page is fetching
usePolled reports when a request is in flight (through a new onBusy of the poller) and holds it on for at least 0.4 seconds, so a quick answer still reads as a refresh. The picker swaps its clock for the spinner, in the same box so its width stays, and the spinner stands still where motion is off. Every page with a picker passes it; a page with several polled requests shows it while any of them runs. The Jobs page fetches through shared components and has none yet.
This commit is contained in:
parent
89bbeb9757
commit
47c447409b
1 changed files with 296 additions and 23 deletions
|
|
@ -4,7 +4,9 @@
|
|||
// request in flight and asks again, for a refresh that follows a change.
|
||||
// `stop` drops the request in flight. An answer from a dropped request never
|
||||
// reaches `onData` or `onError`, so an older response cannot overwrite a
|
||||
// newer one or land on a page that is gone.
|
||||
// newer one or land on a page that is gone. `onBusy` tells when a request goes
|
||||
// in flight and when none is any more, however it ended, so a page can show
|
||||
// that it is refreshing.
|
||||
export interface Poller {
|
||||
tick(): void;
|
||||
refetch(): void;
|
||||
|
|
@ -15,29 +17,35 @@ export function createPoller<T>(opts: {
|
|||
fetch: (signal: AbortSignal) => Promise<T>;
|
||||
onData: (data: T) => void;
|
||||
onError: (error: unknown) => void;
|
||||
onBusy?: (busy: boolean) => void;
|
||||
}): Poller {
|
||||
let current: { controller: AbortController } | null = null;
|
||||
|
||||
function start() {
|
||||
const run = { controller: new AbortController() };
|
||||
current = run;
|
||||
opts.onBusy?.(true);
|
||||
opts.fetch(run.controller.signal).then(
|
||||
(data) => {
|
||||
if (current !== run) return;
|
||||
current = null;
|
||||
opts.onBusy?.(false);
|
||||
opts.onData(data);
|
||||
},
|
||||
(error: unknown) => {
|
||||
if (current !== run) return;
|
||||
current = null;
|
||||
opts.onBusy?.(false);
|
||||
opts.onError(error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function stop() {
|
||||
current?.controller.abort();
|
||||
if (current === null) return;
|
||||
current.controller.abort();
|
||||
current = null;
|
||||
opts.onBusy?.(false);
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Reference in a new issue