-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
119 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { | ||
timer, | ||
timerOnce, | ||
timerFlush | ||
} from "./src/timer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Some tests need a trailing frame after timers are stopped to cleanup the | ||
// queue and clear the alarm. Let that finish before starting a new test. | ||
module.exports = function(test) { | ||
setTimeout(function() { | ||
test.end(); | ||
}, 24); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var tape = require("tape"), | ||
timer = require("../"), | ||
end = require("./end"); | ||
|
||
tape("timerFlush() immediately invokes any eligible timers", function(test) { | ||
var count = 0; | ||
var t = timer.timer(function() { ++count; t.stop(); }); | ||
timer.timerFlush(); | ||
timer.timerFlush(); | ||
test.equal(count, 1); | ||
end(test); | ||
}); | ||
|
||
tape("timerFlush() within timerFlush() still executes all eligible timers", function(test) { | ||
var count = 0; | ||
var t = timer.timer(function() { if (++count >= 3) t.stop(); timer.timerFlush(); }); | ||
timer.timerFlush(); | ||
test.equal(count, 3); | ||
end(test); | ||
}); | ||
|
||
tape("timerFlush(time) observes the specified time", function(test) { | ||
var start = Date.now(), count = 0; | ||
var t = timer.timer(function() { if (++count >= 2) t.stop(); }, 0, start); | ||
timer.timerFlush(start - 1); | ||
test.equal(count, 0); | ||
timer.timerFlush(start); | ||
test.equal(count, 1); | ||
timer.timerFlush(start + 1); | ||
test.equal(count, 2); | ||
end(test); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var tape = require("tape"), | ||
timer = require("../"), | ||
end = require("./end"); | ||
|
||
require("./inRange"); | ||
|
||
tape("timerOnce(callback) invokes the callback once", function(test) { | ||
var count = 0; | ||
timer.timerOnce(function() { | ||
test.equal(++count, 1); | ||
end(test); | ||
}); | ||
}); | ||
|
||
tape("timerOnce(callback, delay) invokes the callback once after the specified delay", function(test) { | ||
var time = Date.now(), delay = 50; | ||
timer.timerOnce(function(elapsed, now) { | ||
test.inRange(now - time, delay - 10, delay + 10); | ||
end(test); | ||
}, delay); | ||
}); | ||
|
||
tape("timerOnce(callback, delay, time) invokes the callback once after the specified delay relative to the given time", function(test) { | ||
var time = Date.now() + 50, delay = 50; | ||
timer.timerOnce(function(elapsed, now) { | ||
test.inRange(now - time, delay - 10, delay + 10); | ||
end(test); | ||
}, delay, time); | ||
}); | ||
|
||
tape("timerOnce(callback) uses the global context for the callback", function(test) { | ||
timer.timerOnce(function() { | ||
test.equal(this, global); | ||
end(test); | ||
}); | ||
}); | ||
|
||
tape("timerOnce(callback) passes the callback the elapsed and current time", function(test) { | ||
var time = Date.now(), count = 0; | ||
timer.timerOnce(function(elapsed, now) { | ||
test.equal(elapsed, now - time); | ||
test.inRange(now, Date.now() - 10, Date.now()); | ||
end(test); | ||
}, 0, time); | ||
}); | ||
|
||
tape("timerOnce(callback) returns a timer", function(test) { | ||
var count = 0; | ||
var t = timer.timerOnce(function() { ++count; }); | ||
test.equal(t instanceof timer.timer, true); | ||
t.stop(); | ||
setTimeout(function() { | ||
test.equal(count, 0); | ||
end(test); | ||
}, 100); | ||
}); |