2 * A handle representing a JavaScript {@link Window}, useful for dealing with
3 * new windows in async contexts and avoiding popup blockers.
5 * @see {@link getNewWindow}
7 export type WindowHandle = {
8 /** Actual JS Window object */
10 /** Closes the window in case of failure */
15 * Creates a {@link WindowHandle} for the current window, used as a fallback when opening new tabs.
19 * @see {@link getNewWindow}
21 export const getCurrentTab = (): WindowHandle => ({
27 * Creates a {@link WindowHandle} for a new tab. Falls back to the current tab.
31 * const w = getNewWindow();
34 * const result = await somethingAsynchronous();
35 * w.handle.location = new URL(result);
38 * // Close the window if the call fails (edge case)
42 export const getNewWindow = (): WindowHandle => {
43 const handle = window.open('', '_blank');
46 // In case we weren't able to open a new window,
47 // let's fall back to opening in the current tab.
49 // eslint-disable-next-line no-console
50 console.warn('Failed to open new window, using current tab');
52 return getCurrentTab();
57 close: () => handle.close(),