Line data Source code
1 : import * as React from 'react';
2 :
3 : /// Stops a writer of the app storage before the app is handed over to another tab
4 : export type AppHandOverHook = () => Promise<void>;
5 :
6 : /// Registers a hand over hook and returns its unregistration
7 : export type AppHandOverRegistry = (hook: AppHandOverHook) => () => void;
8 :
9 1 : export const APP_HAND_OVER_CTX = React.createContext<AppHandOverRegistry | null>(null);
10 :
11 : /// Stop a writer of the app storage before another tab takes over the app.
12 : /// A no-op outside of an `AppOwnershipGate`.
13 1 : export function useAppHandOver(hook: AppHandOverHook): void {
14 1 : const register = React.useContext(APP_HAND_OVER_CTX);
15 1 : const latest = React.useRef(hook);
16 1 : latest.current = hook;
17 1 : React.useEffect(() => register?.(() => latest.current()), [register]);
18 1 : }
19 :
20 : /// Restarts the app in this tab, e.g. to start the embedded database with other options
21 : export type AppRestart = () => Promise<void>;
22 :
23 1 : export const APP_RESTART_CTX = React.createContext<AppRestart | null>(null);
24 :
25 : /// Leave the app. The reload stops everything the tab still runs, and the browser releases the lock of the app
26 : /// when the page unloads. After a hand over the tab boots into the "open elsewhere" page.
27 0 : export function reloadTab(): Promise<void> {
28 0 : window.location.reload();
29 0 : return new Promise(() => { });
30 0 : }
31 :
32 : /// Restart the app: stop the writers of the app storage like before a hand over, then reload the tab.
33 : /// Outside of an `AppOwnershipGate` it only reloads the tab.
34 1 : export function useAppRestart(): AppRestart {
35 1 : return React.useContext(APP_RESTART_CTX) ?? reloadTab;
36 1 : }
|