@@ -10,7 +10,7 @@ import type { ConfigStore } from '@/main/config'
1010import type { EventRecorder } from '@/main/observability'
1111import {
1212 createMainWindow ,
13- detachVisibilityListenersOnClose ,
13+ guardVisibilityListenersAfterDestroy ,
1414 resolvePermission ,
1515 setupPermissionHandlers ,
1616} from '@/main/window'
@@ -202,32 +202,43 @@ describe('createMainWindow', () => {
202202 } )
203203} )
204204
205- describe ( 'detachVisibilityListenersOnClose ' , ( ) => {
205+ describe ( 'guardVisibilityListenersAfterDestroy ' , ( ) => {
206206 /** A window wired the way Electron wires every BrowserWindow: its visibility listener reads the native window. */
207207 function electronLikeWindow ( ) {
208208 let destroyed = false
209+ const visibilityChecks = vi . fn ( )
209210 const win = Object . assign ( new EventEmitter ( ) , {
211+ isDestroyed : ( ) => destroyed ,
210212 isVisible : ( ) => {
211213 if ( destroyed ) throw new TypeError ( 'Object has been destroyed' )
214+ visibilityChecks ( )
212215 return true
213216 } ,
214217 destroy : ( ) => {
215218 destroyed = true
216- win . emit ( 'closed' )
217219 } ,
218220 } )
219221 const visibilityChanged = ( ) => win . isVisible ( )
220222 for ( const event of [ 'show' , 'hide' , 'minimize' , 'maximize' , 'restore' ] ) {
221223 win . on ( event , visibilityChanged )
222224 }
223- return win
225+ return { win, visibilityChecks }
224226 }
225227
226- it ( 'a visibility event delivered after the window closed no longer throws ' , ( ) => {
227- const win = electronLikeWindow ( )
228- detachVisibilityListenersOnClose ( win as never )
228+ it ( 'keeps tracking visibility while the window lives ' , ( ) => {
229+ const { win, visibilityChecks } = electronLikeWindow ( )
230+ guardVisibilityListenersAfterDestroy ( win as never )
229231
230232 win . emit ( 'show' )
233+ win . emit ( 'minimize' )
234+
235+ expect ( visibilityChecks ) . toHaveBeenCalledTimes ( 2 )
236+ } )
237+
238+ it ( 'a visibility event delivered after the window was destroyed no longer throws' , ( ) => {
239+ const { win } = electronLikeWindow ( )
240+ guardVisibilityListenersAfterDestroy ( win as never )
241+
231242 win . destroy ( )
232243
233244 expect ( ( ) => win . emit ( 'show' ) ) . not . toThrow ( )
0 commit comments