-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(pre-req): several fixes to the current hidden window launching process - INS-3319 #7174
Changes from all commits
7d77d60
ad39091
50b27b2
a299dff
8010fcf
6e81684
ad4b84c
cb8b4b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -48,7 +48,10 @@ test('handle hidden browser window getting closed', async ({ app, page }) => { | |||||
await page.getByTestId('settings-button').click(); | ||||||
await page.getByLabel('Request timeout (ms)').fill('1'); | ||||||
await page.getByRole('button', { name: '' }).click(); | ||||||
await page.getByRole('button', { name: 'Send' }).click(); | ||||||
|
||||||
await page.getByText('Pre-request Scripts').click(); | ||||||
await page.getByLabel('Request Collection').getByTestId('Long running task').press('Enter'); | ||||||
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click(); | ||||||
|
||||||
await page.getByText('Timeout: Pre-request script took too long').click(); | ||||||
await page.getByRole('tab', { name: 'Timeline' }).click(); | ||||||
|
@@ -57,5 +60,6 @@ test('handle hidden browser window getting closed', async ({ app, page }) => { | |||||
const hiddenWindow = windows[1]; | ||||||
hiddenWindow.close(); | ||||||
await page.getByRole('button', { name: 'Send' }).click(); | ||||||
await page.getByText('Timeout: Hidden browser window is not responding').click(); | ||||||
// as the hidden window is restarted, it should not show "Timeout: Hidden browser window is not responding" | ||||||
await page.getByText('Timeout: Pre-request script took too long').click(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { | |
type BrowserWindow as ElectronBrowserWindow, | ||
clipboard, | ||
dialog, | ||
ipcMain, | ||
Menu, | ||
type MenuItemConstructorOptions, | ||
MessageChannelMain, | ||
|
@@ -37,6 +38,7 @@ const MINIMUM_HEIGHT = 400; | |
|
||
const browserWindows = new Map<'Insomnia' | 'HiddenBrowserWindow', ElectronBrowserWindow>(); | ||
let localStorage: LocalStorage | null = null; | ||
let hiddenWindowIsBusy = false; | ||
|
||
interface Bounds { | ||
height?: number; | ||
|
@@ -49,64 +51,109 @@ export function init() { | |
initLocalStorage(); | ||
} | ||
|
||
export async function createHiddenBrowserWindow(): Promise<ElectronBrowserWindow> { | ||
// if open, close it | ||
if (browserWindows.get('HiddenBrowserWindow')) { | ||
await new Promise<void>(resolve => { | ||
const hiddenBrowserWindow = browserWindows.get('HiddenBrowserWindow'); | ||
invariant(hiddenBrowserWindow, 'hiddenBrowserWindow is not defined'); | ||
|
||
// overwrite the closed handler | ||
hiddenBrowserWindow.on('closed', () => { | ||
if (hiddenBrowserWindow) { | ||
console.log('[main] restarting hidden browser window'); | ||
browserWindows.delete('HiddenBrowserWindow'); | ||
} | ||
resolve(); | ||
export async function createHiddenBrowserWindow() { | ||
const mainWindow = browserWindows.get('Insomnia'); | ||
invariant(mainWindow, 'MainWindow is not defined, please restart the app.'); | ||
|
||
console.log('[main] Registering the hidden window restarting handler'); | ||
ipcMain.on('set-hidden-window-busy-status', (_, busyStatus) => { | ||
hiddenWindowIsBusy = busyStatus; | ||
}); | ||
// when the main window runs a script | ||
// if the hidden window is down, start it | ||
ipcMain.handle('open-channel-to-hidden-browser-window', async event => { | ||
// sync the hidden window status | ||
const runningHiddenWindow = browserWindows.get('HiddenBrowserWindow'); | ||
const isAvailable = !hiddenWindowIsBusy && runningHiddenWindow; | ||
if (isAvailable) { | ||
return; | ||
} | ||
const isOccupied = hiddenWindowIsBusy && runningHiddenWindow; | ||
if (isOccupied) { | ||
// stop and sync the map | ||
await new Promise<void>(resolve => { | ||
invariant(runningHiddenWindow, 'hiddenBrowserWindow is running'); | ||
// overwrite the closed handler | ||
runningHiddenWindow.on('closed', () => { | ||
if (runningHiddenWindow) { | ||
console.log('[main] restarting hidden browser window:', runningHiddenWindow.id); | ||
browserWindows.delete('HiddenBrowserWindow'); | ||
} | ||
resolve(); | ||
}); | ||
stopHiddenBrowserWindow(); | ||
hiddenWindowIsBusy = false; | ||
}); | ||
} | ||
|
||
const windowWasClosedUnexpectedly = hiddenWindowIsBusy && !runningHiddenWindow; | ||
if (windowWasClosedUnexpectedly) { | ||
hiddenWindowIsBusy = false; | ||
} | ||
|
||
stopHiddenBrowserWindow(); | ||
console.log('[main] hidden window is down, restarting'); | ||
const hiddenBrowserWindow = new BrowserWindow({ | ||
show: false, | ||
title: 'HiddenBrowserWindow', | ||
width: DEFAULT_WIDTH, | ||
height: DEFAULT_HEIGHT, | ||
minHeight: MINIMUM_HEIGHT, | ||
minWidth: MINIMUM_WIDTH, | ||
webPreferences: { | ||
contextIsolation: false, | ||
nodeIntegration: true, | ||
preload: path.join(__dirname, 'hidden-window-preload.js'), | ||
spellcheck: false, | ||
devTools: process.env.NODE_ENV === 'development', | ||
}, | ||
}); | ||
} | ||
const hiddenBrowserWindow = new BrowserWindow({ | ||
show: false, | ||
title: 'HiddenBrowserWindow', | ||
width: DEFAULT_WIDTH, | ||
height: DEFAULT_HEIGHT, | ||
minHeight: MINIMUM_HEIGHT, | ||
minWidth: MINIMUM_WIDTH, | ||
webPreferences: { | ||
contextIsolation: false, | ||
nodeIntegration: true, | ||
preload: path.join(__dirname, 'hidden-window-preload.js'), | ||
spellcheck: false, | ||
devTools: process.env.NODE_ENV === 'development', | ||
}, | ||
}); | ||
browserWindows.set('HiddenBrowserWindow', hiddenBrowserWindow); | ||
|
||
const hiddenBrowserWindowPath = path.resolve(__dirname, 'hidden-window.html'); | ||
const hiddenBrowserWindowUrl = process.env.HIDDEN_BROWSER_WINDOW_URL || pathToFileURL(hiddenBrowserWindowPath).href; | ||
hiddenBrowserWindow.loadURL(hiddenBrowserWindowUrl); | ||
console.log(`[main] Loading ${hiddenBrowserWindowUrl}`); | ||
hiddenBrowserWindow.on('closed', () => { | ||
if (browserWindows.get('HiddenBrowserWindow')) { | ||
console.log('[main] closing hidden browser window'); | ||
browserWindows.delete('HiddenBrowserWindow'); | ||
} | ||
}); | ||
|
||
hiddenBrowserWindow.on('closed', () => { | ||
if (browserWindows.get('HiddenBrowserWindow')) { | ||
console.log('[main] closing hidden browser window'); | ||
browserWindows.delete('HiddenBrowserWindow'); | ||
} | ||
}); | ||
const mainWindow = browserWindows.get('Insomnia'); | ||
const hiddenBrowserWindowPath = path.resolve(__dirname, 'hidden-window.html'); | ||
const hiddenBrowserWindowUrl = process.env.HIDDEN_BROWSER_WINDOW_URL || pathToFileURL(hiddenBrowserWindowPath).href; | ||
hiddenBrowserWindow.loadURL(hiddenBrowserWindowUrl); | ||
console.log(`[main] Loading ${hiddenBrowserWindowUrl}`); | ||
|
||
ipcMain.removeHandler('renderer-listener-ready'); | ||
const hiddenWinListenerReady = new Promise<void>(resolve => { | ||
ipcMain.handleOnce('renderer-listener-ready', () => { | ||
console.log('[main] hidden window listener is ready'); | ||
resolve(); | ||
}); | ||
}); | ||
await hiddenWinListenerReady; | ||
|
||
ipcMain.removeHandler('hidden-window-received-port'); | ||
const hiddenWinPortReady = new Promise<void>(resolve => { | ||
ipcMain.handleOnce('hidden-window-received-port', () => { | ||
console.log('[main] hidden window has received port'); | ||
resolve(); | ||
}); | ||
}); | ||
|
||
invariant(mainWindow, 'mainWindow is not defined'); | ||
mainWindow.webContents.mainFrame.ipc.on('open-channel-to-hidden-browser-window', event => { | ||
const { port1, port2 } = new MessageChannelMain(); | ||
hiddenBrowserWindow.webContents.postMessage('renderer-listener', null, [port1]); | ||
await hiddenWinPortReady; | ||
|
||
ipcMain.removeHandler('main-window-script-port-ready'); | ||
const mainWinPortReady = new Promise<void>(resolve => { | ||
ipcMain.handleOnce('main-window-script-port-ready', () => { | ||
console.log('[main] main window has received hidden window port'); | ||
resolve(); | ||
}); | ||
}); | ||
|
||
event.senderFrame.postMessage('hidden-browser-window-response-listener', null, [port2]); | ||
port1.close(); | ||
port2.close(); | ||
await mainWinPortReady; | ||
|
||
browserWindows.set('HiddenBrowserWindow', hiddenBrowserWindow); | ||
}); | ||
return hiddenBrowserWindow; | ||
} | ||
|
||
export function stopHiddenBrowserWindow() { | ||
|
@@ -737,6 +784,10 @@ function initLocalStorage() { | |
localStorage = new LocalStorage(localStoragePath); | ||
} | ||
|
||
export function getOrCreateWindow() { | ||
return browserWindows.get('Insomnia') ?? createWindow(); | ||
export function createWindowsAndReturnMain() { | ||
const mainWindow = browserWindows.get('Insomnia') ?? createWindow(); | ||
if (!browserWindows.get('HiddenBrowserWindow')) { | ||
createHiddenBrowserWindow(); | ||
Comment on lines
+788
to
+790
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it helps to keep createWindow and createHiddenWindow signatures and behaviour aligned. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool |
||
} | ||
return mainWindow; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔