export const BEFORE_PAGE_REFRESH = "app:before-page-refresh"; export type PageRefreshRequest = { save: (action: () => Promise) => void; block: (message: string) => void; }; export async function preparePageRefresh() { const actions: Array<() => Promise> = []; let blocked = ""; window.dispatchEvent(new CustomEvent(BEFORE_PAGE_REFRESH, { detail: { save: action => actions.push(action), block: message => { blocked ||= message; } }, })); if (blocked) throw new Error(blocked); for (const action of actions) await action(); }