fix: clean up notification display listeners (#3760)

This commit is contained in:
Neil
2026-05-30 09:10:44 -07:00
committed by GitHub
parent e070c6f261
commit d4f00e3748
2 changed files with 31 additions and 7 deletions
+9 -1
View File
@@ -992,10 +992,14 @@ describe('registerNotificationHandlers', () => {
const handler = getDispatchHandler()
const result = handler({}, { source: 'test', requireDisplayConfirmation: true })
getNotificationOnceEventHandler('show')()
const showHandler = getNotificationOnceEventHandler('show')
const failedHandler = getNotificationOnceEventHandler('failed')
showHandler()
await expect(result).resolves.toEqual({ delivered: true })
expect(notificationShowMock).toHaveBeenCalledTimes(1)
expect(notificationRemoveListenerMock).toHaveBeenCalledWith('show', showHandler)
expect(notificationRemoveListenerMock).toHaveBeenCalledWith('failed', failedHandler)
})
it('reports not-displayed when explicit test notifications never show', async () => {
@@ -1013,10 +1017,14 @@ describe('registerNotificationHandlers', () => {
const handler = getDispatchHandler()
const result = handler({}, { source: 'test', requireDisplayConfirmation: true })
const showHandler = getNotificationOnceEventHandler('show')
const failedHandler = getNotificationOnceEventHandler('failed')
await vi.advanceTimersByTimeAsync(2501)
await expect(result).resolves.toEqual({ delivered: false, reason: 'not-displayed' })
expect(notificationShowMock).toHaveBeenCalledTimes(1)
expect(notificationRemoveListenerMock).toHaveBeenCalledWith('show', showHandler)
expect(notificationRemoveListenerMock).toHaveBeenCalledWith('failed', failedHandler)
})
it('loads allowed custom sound files for preload playback', async () => {
+22 -6
View File
@@ -136,19 +136,35 @@ function waitForNotificationDisplay(notification: Notification): Promise<boolean
return new Promise((resolve) => {
let settled = false
let timer: ReturnType<typeof setTimeout> | null = null
const settle = (displayed: boolean): void => {
function cleanup(): void {
notification.removeListener('show', onShow)
notification.removeListener('failed', onFailed)
if (timer) {
clearTimeout(timer)
timer = null
}
}
function settle(displayed: boolean): void {
if (settled) {
return
}
settled = true
if (timer) {
clearTimeout(timer)
}
cleanup()
resolve(displayed)
}
notification.once('show', () => settle(true))
notification.once('failed', () => settle(false))
function onShow(): void {
settle(true)
}
function onFailed(): void {
settle(false)
}
notification.once('show', onShow)
notification.once('failed', onFailed)
timer = setTimeout(() => settle(false), NOTIFICATION_DISPLAY_CONFIRMATION_TIMEOUT_MS)
})
}