ai-chatbot/lib/client/page-refresh.ts

16 lines
577 B
TypeScript

export const BEFORE_PAGE_REFRESH = "app:before-page-refresh";
export type PageRefreshRequest = {
save: (action: () => Promise<void>) => void;
block: (message: string) => void;
};
export async function preparePageRefresh() {
const actions: Array<() => Promise<void>> = [];
let blocked = "";
window.dispatchEvent(new CustomEvent<PageRefreshRequest>(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();
}