Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Eventloop is now property of window, not browser.

  • Loading branch information...
commit 9c61377a7e273542719568ea50fe0a31ee3f57a5 1 parent 2a8aed2
@assaf authored
View
4 CHANGELOG.md
@@ -8,8 +8,10 @@ New isolated contexts for executing JavaScript.
Fixed error handling on timeout/XHR scripts, these now generate an
`onerror` event.
+Eventloop is now associated with window instead of browser.
+
293 Tests
- 3.3 sec to complete
+ 3.4 sec to complete
### Version 0.8.13 2011-02-11
View
8 src/zombie/browser.coffee
@@ -5,6 +5,7 @@ require "./jsdom_patches"
require "./forms"
require "./xpath"
History = require("./history").History
+EventLoop = require("./eventloop").EventLoop
require.paths.push "../../build/default"
WindowContext = require("../../build/default/windowcontext").WindowContext
@@ -17,7 +18,6 @@ class Browser extends require("events").EventEmitter
cache = require("./cache").use(this)
cookies = require("./cookies").use(this)
storage = require("./storage").use(this)
- eventloop = require("./eventloop").use(this)
interact = require("./interact").use(this)
xhr = require("./xhr").use(cache)
resources = require("./resources")
@@ -107,7 +107,7 @@ class Browser extends require("events").EventEmitter
resources.extend newWindow
cookies.extend newWindow
storage.extend newWindow
- eventloop.extend newWindow
+ newWindow._eventloop = new EventLoop(newWindow)
history.extend newWindow
interact.extend newWindow
xhr.extend newWindow
@@ -160,7 +160,7 @@ class Browser extends require("events").EventEmitter
callback null, this
@on "error", onerror
@on "done", ondone
- eventloop.wait window, terminate
+ window._eventloop.wait window, terminate
return
# ### browser.fire(name, target, callback?)
@@ -755,7 +755,7 @@ class Browser extends require("events").EventEmitter
console.log "History:\n#{indent window.history.dump()}"
console.log "Cookies:\n#{indent cookies.dump()}"
console.log "Storage:\n#{indent storage.dump()}"
- console.log "Eventloop:\n#{indent eventloop.dump()}"
+ console.log "Eventloop:\n#{indent window._eventloop.dump()}"
if @document
html = @document.outerHTML
html = html.slice(0, 497) + "..." if html.length > 497
View
53 src/zombie/eventloop.coffee
@@ -3,26 +3,26 @@ URL = require("url")
# Handles the Window event loop, timers and pending requests.
class EventLoop
- constructor: (browser)->
+ constructor: (window)->
timers = {}
lastHandle = 0
# ### window.setTimeout(fn, delay) => Number
#
# Implements window.setTimeout using event queue
- this.setTimeout = (fn, delay)->
+ window.setTimeout = (fn, delay)->
timer =
- when: browser.clock + delay
+ when: window.browser.clock + delay
timeout: true
fire: =>
- browser.log "Firing timeout #{handle}, delay: #{delay}"
+ window.browser.log "Firing timeout #{handle}, delay: #{delay}"
try
- browser.window._evaluate fn
+ window._evaluate fn
catch error
- evt = browser.document.createEvent("HTMLEvents")
+ evt = window.document.createEvent("HTMLEvents")
evt.initEvent "error", true, false
evt.error = error
- browser.window.dispatchEvent evt
+ window.dispatchEvent evt
finally
delete timers[handle]
handle = ++lastHandle
@@ -32,21 +32,21 @@ class EventLoop
# ### window.setInterval(fn, delay) => Number
#
# Implements window.setInterval using event queue
- this.setInterval = (fn, delay)->
+ window.setInterval = (fn, delay)->
timer =
- when: browser.clock + delay
+ when: window.browser.clock + delay
interval: true
fire: =>
- browser.log "Firing interval #{handle}, interval: #{delay}"
+ window.browser.log "Firing interval #{handle}, interval: #{delay}"
try
- browser.window._evaluate fn
+ window._evaluate fn
catch error
- evt = browser.document.createEvent("HTMLEvents")
+ evt = window.document.createEvent("HTMLEvents")
evt.initEvent "error", true, false
evt.error = error
- browser.window.dispatchEvent evt
+ window.dispatchEvent evt
finally
- timer.when = browser.clock + delay
+ timer.when = window.browser.clock + delay
handle = ++lastHandle
timers[handle] = timer
handle
@@ -54,11 +54,11 @@ class EventLoop
# ### window.clearTimeout(timeout)
#
# Implements window.clearTimeout using event queue
- this.clearTimeout = (handle)-> delete timers[handle] if timers[handle]?.timeout
+ window.clearTimeout = (handle)-> delete timers[handle] if timers[handle]?.timeout
# ### window.clearInterval(interval)
#
# Implements window.clearInterval using event queue
- this.clearInterval = (handle)-> delete timers[handle] if timers[handle]?.interval
+ window.clearInterval = (handle)-> delete timers[handle] if timers[handle]?.interval
# Size of processing queue (number of ongoing tasks).
processing = 0
@@ -100,7 +100,7 @@ class EventLoop
if earliest
intervals = false
event = ->
- browser.clock = earliest.when if browser.clock < earliest.when
+ window.browser.clock = earliest.when if window.browser.clock < earliest.when
earliest.fire()
if event
try
@@ -113,31 +113,24 @@ class EventLoop
done = true if terminate.call(window) == false
if done
process.nextTick ->
- browser.emit "done", browser
+ window.browser.emit "done", window.browser
callback null, window if callback
else
@wait window, terminate, callback, intervals
catch err
- browser.emit "error", err
+ window.browser.emit "error", err
callback err, window if callback
else if processing > 0
waiting.push => @wait window, terminate, callback, intervals
else
- browser.emit "done", browser
+ window.browser.emit "done", window.browser
callback null, window if callback
- this.extend = (window)=>
- for fn in ["setTimeout", "setInterval", "clearTimeout", "clearInterval"]
- window[fn] = this[fn]
- window.perform = this.perform
- window.wait = (terminate, callback)=> this.wait(window, terminate, callback)
- window.request = this.request
-
this.dump = ()->
- [ "The time: #{browser.clock}",
+ [ "The time: #{window.browser.clock}",
"Timers: #{Object.keys(timers).length}",
"Processing: #{processing}",
"Waiting: #{waiting.length}" ]
-exports.use = (browser)->
- return new EventLoop(browser)
+
+exports.EventLoop = EventLoop
View
2  src/zombie/jsdom_patches.coffee
@@ -83,7 +83,7 @@ core.Document.prototype._elementBuilders["script"] = (doc, s)->
code = event.target.nodeValue
if code.trim().length > 0
filename = @ownerDocument.URL
- @ownerDocument.parentWindow.perform (done)=>
+ @ownerDocument.parentWindow._eventloop.perform (done)=>
loaded = (code, filename)=>
if core.languageProcessors[@language] && code == @text
core.languageProcessors[@language](this, code, filename)
View
2  src/zombie/resources.coffee
@@ -121,7 +121,7 @@ class Resources extends Array
#
# The callback is called with error and response (see `HTTPResponse`).
this.request = (method, url, data, headers, callback)->
- window.perform (done)->
+ window._eventloop.perform (done)->
makeRequest method, url, data, headers, null, (error, response)->
done()
callback error, response
View
2  src/zombie/xhr.coffee
@@ -15,7 +15,7 @@ XMLHttpRequest = (window)->
@__defineGetter__ "readyState", -> state
if @onreadystatechange
# Since we want to wait on these events, put them in the event loop.
- window.perform (done)=>
+ window._eventloop.perform (done)=>
process.nextTick =>
try
@onreadystatechange.call(@)
Please sign in to comment.
Something went wrong with that request. Please try again.