mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-11-05 16:16:49 +00:00
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
/**
|
|
* Where a feature with data of its own on this device signs up for wipes:
|
|
* edit-in-place's opened files, for one. Free of the stores, so anything can
|
|
* import it. Registering is all a feature has to do.
|
|
*
|
|
* A cleaner runs on every remote wipe, policy wipe, wipe from settings and
|
|
* refused login, before the account signs out. `account` is an account id, or
|
|
* `null` when everything on this device goes. A cleaner should finish quickly
|
|
* and may not ask the user anything. Bridge calls are fine, network isn't: a
|
|
* wipe often runs because the server said no.
|
|
*/
|
|
|
|
export type WipeCleaner = (account: string | null) => void | Promise<void>;
|
|
|
|
const cleaners = new Set<WipeCleaner>();
|
|
|
|
export function onDeviceWipe(cleaner: WipeCleaner): () => void {
|
|
cleaners.add(cleaner);
|
|
return () => cleaners.delete(cleaner);
|
|
}
|
|
|
|
/** How long a wipe waits for one cleaner. What's on screen waits with it. */
|
|
const CLEANER_BUDGET_MS = 10_000;
|
|
|
|
export async function runWipeCleaners(account: string | null): Promise<void> {
|
|
await Promise.all([...cleaners].map(async (cleaner) => {
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
try {
|
|
await Promise.race([
|
|
cleaner(account),
|
|
new Promise((resolve) => { timer = setTimeout(resolve, CLEANER_BUDGET_MS); }),
|
|
]);
|
|
} catch {
|
|
// One feature failing must not keep the others' data.
|
|
} finally {
|
|
if (timer) clearTimeout(timer);
|
|
}
|
|
}));
|
|
}
|