fix: remove cyclic references of BrowserWindow (#22006)

* fix: remove cyclic references in BrowserWindow

* fix: prevent TopLevelWindow from garbage collection

* test: garbage collection of BrowserWindow

* chore: createIDWeakMap is used in tests
This commit is contained in:
Cheng Zhao 2020-02-11 09:37:46 +09:00 committed by GitHub
parent 9942149f3c
commit 9ad6f06831
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 76 additions and 32 deletions

View file

@ -9,7 +9,7 @@ import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDe
import { emittedOnce } from './events-helpers'
import { ifit, ifdescribe } from './spec-helpers'
import { closeWindow } from './window-helpers'
import { closeWindow, closeAllWindows } from './window-helpers'
const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures')
@ -37,12 +37,6 @@ const expectBoundsEqual = (actual: any, expected: any) => {
}
}
const closeAllWindows = async () => {
for (const w of BrowserWindow.getAllWindows()) {
await closeWindow(w, { assertNotWindows: false })
}
}
describe('BrowserWindow module', () => {
describe('BrowserWindow constructor', () => {
it('allows passing void 0 as the webContents', async () => {
@ -58,6 +52,28 @@ describe('BrowserWindow module', () => {
})
})
describe('garbage collection', () => {
const v8Util = process.electronBinding('v8_util')
afterEach(closeAllWindows)
it('window does not get garbage collected when opened', (done) => {
const w = new BrowserWindow({ show: false })
// Keep a weak reference to the window.
const map = v8Util.createIDWeakMap<Electron.BrowserWindow>()
map.set(0, w)
setTimeout(() => {
// Do garbage collection, since |w| is not referenced in this closure
// it would be gone after next call if there is no other reference.
v8Util.requestGarbageCollectionForTesting()
setTimeout(() => {
expect(map.has(0)).to.equal(true)
done()
})
})
})
})
describe('BrowserWindow.close()', () => {
let w = null as unknown as BrowserWindow
beforeEach(() => {