aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorStephen Blott2016-04-02 12:12:19 +0100
committerStephen Blott2016-04-02 12:27:21 +0100
commit57c3cae5abd5c796b093ad02661b040b2c877fc4 (patch)
tree9c80ebc1a1b080d5ed055b36f9b1b25450e60c84 /lib
parent89df06fa6c00c9295ff064308c03f770b01d79be (diff)
downloadvimium-57c3cae5abd5c796b093ad02661b040b2c877fc4.tar.bz2
Refactor DomUtils.documentReady.
We do not need to install separate event listeners for every callback. Just install one listener and keep track of the callbacks ourself. This is clearer, and also determines the order in which callbacks are called. (Although, we don't rely on that currently.) This also adds a tests.
Diffstat (limited to 'lib')
-rw-r--r--lib/dom_utils.coffee18
1 files changed, 9 insertions, 9 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index 3fc08b78..9a6c1b09 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -3,15 +3,15 @@ DomUtils =
# Runs :callback if the DOM has loaded, otherwise runs it on load
#
documentReady: do ->
- isReady = false
- (callback) ->
- if document.readyState == "loading" and not isReady
- window.addEventListener "DOMContentLoaded", handler = ->
- isReady = true
- window.removeEventListener "DOMContentLoaded", handler
- callback()
- else
- callback()
+ [isReady, callbacks] = [document.readyState != "loading", []]
+ unless isReady
+ window.addEventListener "DOMContentLoaded", onDOMContentLoaded = ->
+ window.removeEventListener "DOMContentLoaded", onDOMContentLoaded
+ isReady = true
+ callback() for callback in callbacks
+ callbacks = null
+
+ (callback) -> if isReady then callback() else callbacks.push callback
createElement: (tagName) ->
element = document.createElement tagName