Skip to content

Commit

Permalink
Merge pull request #229 from ZIMkaRU/bugfix/fix-max-listeners-exceede…
Browse files Browse the repository at this point in the history
…d-warning

Fix max listeners exceeded warning
  • Loading branch information
prdn authored Jul 25, 2023
2 parents 776baec + 65811a4 commit 39da38d
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 26 deletions.
29 changes: 16 additions & 13 deletions src/auto-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const {
closeAlert
} = require('../modal-dialog-src/utils')
const parseEnvValToBool = require('../helpers/parse-env-val-to-bool')
const {
WINDOW_EVENT_NAMES,
addOnceProcEventHandler
} = require('../window-event-manager')

const isAutoUpdateDisabled = parseEnvValToBool(process.env.IS_AUTO_UPDATE_DISABLED)

Expand Down Expand Up @@ -53,13 +57,11 @@ const script = `<script type="text/javascript">${toastScript}</script>`
const sound = { freq: 'F2', type: 'triange', duration: 1.5 }

const _sendProgress = (progress) => {
if (
!toast ||
!toast.browserWindow ||
!Number.isFinite(progress)
) return
if (!Number.isFinite(progress)) {
return
}

toast.browserWindow.webContents.send(
toast?.browserWindow?.webContents.send(
'progress',
progress
)
Expand Down Expand Up @@ -90,9 +92,10 @@ const _fireToast = (
const alert = new Alert([fonts, style, script])
toast = alert

const _closeAlert = () => closeAlert(alert)

win.once('closed', _closeAlert)
const eventHandlerCtx = addOnceProcEventHandler(
WINDOW_EVENT_NAMES.CLOSED,
() => closeAlert(alert)
)

const bwOptions = {
frame: false,
Expand Down Expand Up @@ -151,7 +154,7 @@ const _fireToast = (
alert.browserWindow.hide()
},
didClose: () => {
win.removeListener('closed', _closeAlert)
eventHandlerCtx.removeListener()

didClose(alert)
}
Expand Down Expand Up @@ -387,14 +390,16 @@ const _autoUpdaterFactory = () => {
})
autoUpdater.on('download-progress', async (progressObj) => {
try {
const { percent } = { ...progressObj }
const { percent } = progressObj ?? {}

if (isProgressToastEnabled) {
_sendProgress(percent)

return
}

isProgressToastEnabled = true

await _fireToast(
{
title: 'Downloading...',
Expand All @@ -404,8 +409,6 @@ const _autoUpdaterFactory = () => {
didOpen: (alert) => {
_sendProgress(percent)
alert.showLoading()

isProgressToastEnabled = true
},
didClose: () => {
isProgressToastEnabled = false
Expand Down
20 changes: 16 additions & 4 deletions src/change-sync-frequency.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const { getConfigsKeeperByName } = require('./configs-keeper')
const getAlertCustomClassObj = require(
'./helpers/get-alert-custom-class-obj'
)
const {
WINDOW_EVENT_NAMES,
addOnceProcEventHandler
} = require('./window-event-manager')

const _getSchedulerRule = (timeFormat, alertRes) => {
if (timeFormat.value === 'days') {
Expand Down Expand Up @@ -219,8 +223,16 @@ module.exports = () => {

return async () => {
const win = electron.BrowserWindow.getFocusedWindow()
win.once('closed', closeTimeFormatAlert)
win.once('closed', closeAlert)
const timeFormatAlertEventHandlerCtx = addOnceProcEventHandler(
WINDOW_EVENT_NAMES.CLOSED,
closeTimeFormatAlert,
win
)
const alertEventHandlerCtx = addOnceProcEventHandler(
WINDOW_EVENT_NAMES.CLOSED,
closeAlert,
win
)

try {
const savedSchedulerRule = await configsKeeper
Expand All @@ -234,7 +246,7 @@ module.exports = () => {
inputValue: timeData.timeFormat
}
)
win.removeListener('closed', closeTimeFormatAlert)
timeFormatAlertEventHandlerCtx.removeListener()

if (timeFormat.dismiss) {
return
Expand All @@ -244,7 +256,7 @@ module.exports = () => {
alert,
getAlertOpts(timeFormat, timeData)
)
win.removeListener('closed', closeAlert)
alertEventHandlerCtx.removeListener()

if (alertRes.dismiss) {
return
Expand Down
12 changes: 9 additions & 3 deletions src/error-manager/show-modal-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const isMainWinAvailable = require(
const {
closeAlert
} = require('../modal-dialog-src/utils')
const {
WINDOW_EVENT_NAMES,
addOnceProcEventHandler
} = require('../window-event-manager')

const mdStyle = fs.readFileSync(path.join(
rootPath, 'node_modules', 'github-markdown-css/github-markdown.css'
Expand Down Expand Up @@ -69,9 +73,11 @@ const _fireAlert = (params) => {
const maxHeight = Math.floor(screenHeight * 0.90)

const alert = new Alert([mdS, fonts, style, script])
const _close = () => closeAlert(alert)

win.once('closed', _close)
const eventHandlerCtx = addOnceProcEventHandler(
WINDOW_EVENT_NAMES.CLOSED,
() => closeAlert(alert)
)

const bwOptions = {
frame: false,
Expand Down Expand Up @@ -138,7 +144,7 @@ const _fireAlert = (params) => {
alert.browserWindow.hide()
},
didClose: () => {
win.removeListener('closed', _close)
eventHandlerCtx.removeListener()
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/restore-db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const {
const {
DbRestoringError
} = require('../errors')
const {
WINDOW_EVENT_NAMES,
addOnceProcEventHandler
} = require('../window-event-manager')

const fontsStyle = fs.readFileSync(path.join(
rootPath, 'bfx-report-ui/build/fonts/roboto.css'
Expand Down Expand Up @@ -79,9 +83,11 @@ const _fireAlert = (params) => {
const maxHeight = Math.floor(screenHeight * 0.90)

const alert = new Alert([fonts, style, script])
const _close = () => closeAlert(alert)

win.once('closed', _close)
const eventHandlerCtx = addOnceProcEventHandler(
WINDOW_EVENT_NAMES.CLOSED,
() => closeAlert(alert)
)

const bwOptions = {
resizable: true,
Expand Down Expand Up @@ -154,7 +160,7 @@ const _fireAlert = (params) => {
alert.browserWindow.hide()
},
didClose: () => {
win.removeListener('closed', _close)
eventHandlerCtx.removeListener()
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/show-docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const mdUserManual = fs.readFileSync(
path.join(rootPath, 'docs/user-manual.md'),
'utf8'
)
const {
WINDOW_EVENT_NAMES,
addOnceProcEventHandler
} = require('../window-event-manager')

const mdStyle = fs.readFileSync(path.join(
rootPath, 'node_modules', 'github-markdown-css/github-markdown.css'
Expand Down Expand Up @@ -75,9 +79,11 @@ const _fireAlert = (params) => {
const maxHeight = Math.floor(screenHeight * 0.90)

const alert = new Alert([mdS, fonts, style, script])
const _close = () => closeAlert(alert)

win.once('closed', _close)
const eventHandlerCtx = addOnceProcEventHandler(
WINDOW_EVENT_NAMES.CLOSED,
() => closeAlert(alert)
)

const bwOptions = {
frame: false,
Expand Down Expand Up @@ -143,7 +149,7 @@ const _fireAlert = (params) => {
alert.browserWindow.hide()
},
didClose: () => {
win.removeListener('closed', _close)
eventHandlerCtx.removeListener()
}
}

Expand Down
93 changes: 93 additions & 0 deletions src/window-event-manager/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict'

const wins = require('../windows')

const WINDOW_EVENT_NAMES = {
CLOSED: 'closed'
}

const windowMap = new Map()

const addOnceProcEventHandler = (eventName, handler, window) => {
const _window = window ?? wins.mainWindow

if (
!_window ||
!eventName ||
typeof eventName !== 'string' ||
typeof handler !== 'function'
) {
return {
isAdded: false,
removeListener: () => {}
}
}

const handlerSet = _setWinEventHandler(eventName, handler, _window)

const ctx = {
isAdded: true,
removeListener: () => handlerSet.delete(handler)
}

return ctx
}

const _setWinEventHandler = (eventName, handler, window) => {
const winEventHandlerMap = _getWinEventHandlerMap(window)
const foundHandlerSet = winEventHandlerMap.get(eventName)?.handlerSet

if (foundHandlerSet instanceof Set) {
foundHandlerSet.add(handler)

return foundHandlerSet
}

const handlerSet = new Set([handler])
const rootHandler = () => {
winEventHandlerMap.delete(eventName)

for (const handler of handlerSet) {
try {
handlerSet.delete(handler)

const res = handler()

if (!(res instanceof Promise)) {
return
}

res.then(() => {}, (err) => { console.error(err) })
} catch (err) {
console.error(err)
}
}
}

window.once(eventName, rootHandler)
winEventHandlerMap.set(eventName, {
rootHandler,
handlerSet
})

return handlerSet
}

const _getWinEventHandlerMap = (window) => {
const foundWinEventHandlerMap = windowMap.get(window)

if (foundWinEventHandlerMap instanceof Map) {
return foundWinEventHandlerMap
}

const winEventHandlerMap = new Map()
windowMap.set(window, winEventHandlerMap)

return winEventHandlerMap
}

module.exports = {
WINDOW_EVENT_NAMES,

addOnceProcEventHandler
}

0 comments on commit 39da38d

Please sign in to comment.