Skip to content
Browse files

Fix timers for chrome and write tests.

  • Loading branch information...
1 parent f21cfac commit 788fe2c42fa5797d26ed9b5ba6b99a3b43ee2c2f @Gozala Gozala committed Dec 31, 2012
Showing with 94 additions and 4 deletions.
  1. +35 −4 builtins/timers.js
  2. +9 −0 testling/test.sh
  3. +50 −0 testling/timers.js
View
39 builtins/timers.js
@@ -1,4 +1,35 @@
-exports.setTimeout = setTimeout;
-exports.clearTimeout = clearTimeout;
-exports.setInterval = setInterval;
-exports.clearInterval = clearInterval;
+try {
+ // Old IE browsers that do not curry arguments
+ if (!setTimeout.call) {
+ var slicer = Array.prototype.slice;
+ exports.setTimeout = function(fn) {
+ var args = slicer.call(arguments, 1);
+ return setTimeout(function() {
+ return fn.apply(this, args);
+ })
+ };
+
+ exports.setInterval = function(fn) {
+ var args = slicer.call(arguments, 1);
+ return setInterval(function() {
+ return fn.apply(this, args);
+ });
+ };
+ } else {
+ exports.setTimeout = setTimeout;
+ exports.setInterval = setInterval;
+ }
+ exports.clearTimeout = clearTimeout;
+ exports.clearInterval = clearInterval;
+
+ // Chrome seems to depend on `this` pseudo variable being a `window` and
+ // throws invalid invocation exception otherwise. If that's a case next
+ // line will throw and in `catch` clause window bound timer functions will
+ // be exported instead.
+ exports.setTimeout(function() {});
+} catch (_) {
+ exports.setTimeout = setTimeout.bind(window);
+ exports.clearTimeout = clearTimeout.bind(window);
+ exports.setInterval = setInterval.bind(window);
+ exports.clearInterval = clearInterval.bind(window);
+}
View
9 testling/test.sh
@@ -18,11 +18,20 @@ function util () {
'http://testling.com/?main=util.js&noinstrument=builtins/util.js'
}
+function timers () {
+ tar -cf- timers.js ../builtins/timers.js |
+ curl -sSNT- -u "$user:$pass" \
+ 'http://testling.com/?main=timers.js'
+}
+
if test -z "$1"; then
tick
util
+ timers
elif test "$1" == 'tick'; then
tick
elif test "$1" == 'util'; then
util
+elif test "$1" == 'timers'; then
+ timers
fi
View
50 testling/timers.js
@@ -0,0 +1,50 @@
+var test = require('testling');
+var timers = require('./builtins/timers');
+
+test('setTimeout / clearTimeout', function (t) {
+ t.plan(3);
+ var arg1 = {}
+ var arg2 = {}
+ var async = false
+
+ timers.setTimeout(function(param1, param2) {
+ t.deepEqual(param1, arg1, '1st argument was curried');
+ t.deepEqual(param2, arg2, '2nd argument was curried');
+ t.ok(async, 'setTimeout is async');
+ timers.clearTimeout(id);
+ }, 10, arg1, arg2);
+
+ var id = timers.setTimeout(function() {
+ t.fail('timer had to be cleared');
+ }, 50);
+
+ timers.setTimeout(function() {
+ t.end();
+ }, 100);
+
+ async = true;
+});
+
+test('setInterval / clearInterval', function (t) {
+ t.plan(6);
+ var arg1 = {};
+ var arg2 = {};
+ var called = 0;
+ var async = false;
+
+ var id = timers.setInterval(function(param1, param2) {
+ called = called + 1;
+ t.deepEqual(param1, arg1, '1st argument was curried');
+ t.deepEqual(param2, arg2, '2nd argument was curried');
+ t.ok(async, 'setInterval invokes internal async');
+
+ if (called > 2) t.fail('internal had to be cleared')
+
+ if (called === 2) {
+ timers.clearInterval(id);
+ t.end();
+ }
+ }, 5, arg1, arg2);
+
+ async = true;
+});

0 comments on commit 788fe2c

Please sign in to comment.
Something went wrong with that request. Please try again.