Add live account appeal updates and restricted status settings
This commit is contained in:
34
lib/realtime-refresh.ts
Normal file
34
lib/realtime-refresh.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/** Serialize refreshes, retaining one follow-up if a change arrives mid-request. */
|
||||
export function realtimeRefresh(
|
||||
refresh: () => Promise<void>,
|
||||
onError: (error: unknown) => void,
|
||||
) {
|
||||
let running = false;
|
||||
let pending = false;
|
||||
let closed = false;
|
||||
async function request() {
|
||||
if (closed) return;
|
||||
pending = true;
|
||||
if (running) return;
|
||||
running = true;
|
||||
try {
|
||||
while (pending && !closed) {
|
||||
pending = false;
|
||||
try {
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
if (!closed) onError(error);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
return {
|
||||
request,
|
||||
close: () => {
|
||||
closed = true;
|
||||
pending = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user