Skip to content

Commit

Permalink
extend timer to support html5 wait call
Browse files Browse the repository at this point in the history
  • Loading branch information
smparkes committed Nov 7, 2009
1 parent 3a80c70 commit b80154c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/dom/document.js
Expand Up @@ -77,7 +77,7 @@ __extend__(DOMDocument.prototype, {

parseHtmlDocument(xmlString, this, null, null);

$env.wait();
$env.wait(-1);
} catch (e) {
$error(e);
}
Expand Down
19 changes: 15 additions & 4 deletions src/window/timer.js
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/call-load-test.js
Expand Up @@ -14,4 +14,4 @@ test("window.location= following Envjs() initialization flagged as error",
expect(0);
});

//Envjs.wait();
Envjs.wait();

0 comments on commit b80154c

Please sign in to comment.