Skip to content

Commit

Permalink
Replace removeEventListener with {once: true}
Browse files Browse the repository at this point in the history
Also hoisted the event handler functions into local variables to make
the addEventListener() calls with `{once: true}` cleaner (as opposed to
defining the handlers inline).
  • Loading branch information
mbland committed Nov 17, 2023
1 parent e6b658e commit ea3d48f
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions strcalc/src/main/frontend/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ class JsdomPageLoader {

#importModulesPromise(window, document) {
return new Promise(resolve => {
let importListener = async () => {
document.removeEventListener('DOMContentLoaded', importListener)

let importModules = async () => {
// The JSDOM docs advise against setting global properties, but we don't
// have another option given the module may access window and/or
// document.
Expand Down Expand Up @@ -272,20 +270,18 @@ class JsdomPageLoader {
// For the same reason, we fire the 'load' event again as well. When
// that listener executes, we can finally reset the global.window and
// global.document variables.
let resetGlobals = () => {
window.removeEventListener('load', resetGlobals)
resolve(global.window = global.document = undefined)
}
window.addEventListener('load', resetGlobals)

let resetGlobals = () => resolve(
global.window = global.document = undefined
)
window.addEventListener('load', resetGlobals, {once: true})
document.dispatchEvent(new window.Event(
'DOMContentLoaded', {bubbles: true, cancelable: false}
))
window.dispatchEvent(new window.Event(
'load', {bubbles: false, cancelable: false}
))
}
document.addEventListener('DOMContentLoaded', importListener)
document.addEventListener('DOMContentLoaded', importModules, {once: true})
})
}
}
Expand Down

0 comments on commit ea3d48f

Please sign in to comment.