aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralpadev <[email protected]>2021-06-22 19:19:55 +0200
committerGitHub <[email protected]>2021-06-22 20:19:55 +0300
commit4927388197a3a2b1b041dce1be513c4cc5c39d22 (patch)
tree120b680784a8daf2f5d44f071b35fdb8045f699b
parent56b000474c5b0418bee1361f273f8354b531c720 (diff)
downloadbootstrap-4927388197a3a2b1b041dce1be513c4cc5c39d22.tar.xz
bootstrap-4927388197a3a2b1b041dce1be513c4cc5c39d22.zip
Register only one `DOMContentLoaded` event listener in `onDOMContentLoaded` (#34158)
* refactor: reuse one DOMContentLoaded event listener in onDOMContentLoaded function Instead of adding an event listener everytime the utility function is called, cache the callbacks and execute them all at once. * refactor: drop iife for onDOMContentLoaded Co-authored-by: XhmikosR <[email protected]>
-rw-r--r--js/src/util/index.js11
-rw-r--r--js/tests/unit/util/index.spec.js13
2 files changed, 21 insertions, 3 deletions
diff --git a/js/src/util/index.js b/js/src/util/index.js
index 6edfaa580..064b4e943 100644
--- a/js/src/util/index.js
+++ b/js/src/util/index.js
@@ -201,9 +201,18 @@ const getjQuery = () => {
return null
}
+const DOMContentLoadedCallbacks = []
+
const onDOMContentLoaded = callback => {
if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', callback)
+ // add listener on the first call when the document is in loading state
+ if (!DOMContentLoadedCallbacks.length) {
+ document.addEventListener('DOMContentLoaded', () => {
+ DOMContentLoadedCallbacks.forEach(callback => callback())
+ })
+ }
+
+ DOMContentLoadedCallbacks.push(callback)
} else {
callback()
}
diff --git a/js/tests/unit/util/index.spec.js b/js/tests/unit/util/index.spec.js
index 04ad6bf43..9b5d7b70e 100644
--- a/js/tests/unit/util/index.spec.js
+++ b/js/tests/unit/util/index.spec.js
@@ -582,15 +582,24 @@ describe('Util', () => {
})
describe('onDOMContentLoaded', () => {
- it('should execute callback when DOMContentLoaded is fired', () => {
+ it('should execute callbacks when DOMContentLoaded is fired and should not add more than one listener', () => {
const spy = jasmine.createSpy()
+ const spy2 = jasmine.createSpy()
+
+ spyOn(document, 'addEventListener').and.callThrough()
spyOnProperty(document, 'readyState').and.returnValue('loading')
+
Util.onDOMContentLoaded(spy)
- window.document.dispatchEvent(new Event('DOMContentLoaded', {
+ Util.onDOMContentLoaded(spy2)
+
+ document.dispatchEvent(new Event('DOMContentLoaded', {
bubbles: true,
cancelable: true
}))
+
expect(spy).toHaveBeenCalled()
+ expect(spy2).toHaveBeenCalled()
+ expect(document.addEventListener).toHaveBeenCalledTimes(1)
})
it('should execute callback if readyState is not "loading"', () => {