From b80154c9cf89d217423fa556db1e33b2d3cab21c Mon Sep 17 00:00:00 2001 From: Steven Parkes Date: Fri, 6 Nov 2009 16:39:26 -0800 Subject: [PATCH] extend timer to support html5 wait call --- src/dom/document.js | 2 +- src/window/timer.js | 19 +++++++++++++++---- test/call-load-test.js | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/dom/document.js b/src/dom/document.js index f89c333f..c65419e2 100644 --- a/src/dom/document.js +++ b/src/dom/document.js @@ -77,7 +77,7 @@ __extend__(DOMDocument.prototype, { parseHtmlDocument(xmlString, this, null, null); - $env.wait(); + $env.wait(-1); } catch (e) { $error(e); } diff --git a/src/window/timer.js b/src/window/timer.js index 9fbcd871..fc5d7217 100644 --- a/src/window/timer.js +++ b/src/window/timer.js @@ -24,8 +24,11 @@ var convert_time = function(time) { if ( isNaN(time) || time < 0 ) { time = 0; } - if ( $event_loop_running && time < 4 ) { - time = 4; + // html5 says this should be at least 4, but the parser is using a setTimeout for the SAX stuff + // which messes up the world + var min = /* 4 */ 0; + if ( $event_loop_running && time < min ) { + time = min; } return time; } @@ -99,10 +102,16 @@ window.clearInterval = window.clearTimeout = function(num){ // wait(n) (n > 0): execute any timers as they fire until there are none left waiting at least n ms // but no more, even if there are future events/current threads // wait(0): execute any immediately runnable timers and return +// wait(-n): keep sleeping until the next event is more than n ms in the future // FIX: make a priority queue ... window.$wait = $env.wait = $env.wait || function(wait) { + var delta_wait; + if (wait < 0) { + delta_wait = -wait; + wait = 0; + } var start = Date.now(); var old_loop_running = $event_loop_running; $event_loop_running = true; @@ -154,14 +163,16 @@ window.$wait = $env.wait = $env.wait || function(wait) { // no events, but a wait requested: fall through to sleep } else { // there are events in the queue, but they aren't firable now - if ( wait === 0 || ( wait > 0 && wait < Date.now () ) ) { + if ( delta_wait && sleep <= delta_wait ) { + // if they will happen within the next delta, fall through to sleep + } else if ( wait === 0 || ( wait > 0 && wait < Date.now () ) ) { // loop ends even if there are events but the user specifcally asked not to wait too long break; } // there are events and the user wants to wait: fall through to sleep } - // Releated to ajax threads ... hopefully can go away .. + // Related to ajax threads ... hopefully can go away .. var interval = $wait.interval || 100; if ( !sleep || sleep > interval ) { sleep = interval; diff --git a/test/call-load-test.js b/test/call-load-test.js index 2cfb51d1..9de2ccd4 100644 --- a/test/call-load-test.js +++ b/test/call-load-test.js @@ -14,4 +14,4 @@ test("window.location= following Envjs() initialization flagged as error", expect(0); }); -//Envjs.wait(); \ No newline at end of file +Envjs.wait(); \ No newline at end of file