Add test for timer.
Also fix a minor bug where we might miss the delay the first callback by an extra frame, and allow d3.timer to be called with an optional delay.
This commit is contained in:
Родитель
ae4306eaa6
Коммит
40b98c4d7b
|
@ -1963,7 +1963,7 @@ function d3_transition(groups) {
|
||||||
delay[++k] = delayMin;
|
delay[++k] = delayMin;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
d3_timer(step, delayMin);
|
d3.timer(step, delayMin);
|
||||||
return transition;
|
return transition;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2083,17 +2083,14 @@ var d3_timer_queue = null,
|
||||||
d3_timer_timeout; // is a timeout active?
|
d3_timer_timeout; // is a timeout active?
|
||||||
|
|
||||||
// The timer will continue to fire until callback returns true.
|
// The timer will continue to fire until callback returns true.
|
||||||
d3.timer = function(callback) {
|
d3.timer = function(callback, delay) {
|
||||||
d3_timer(callback, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
function d3_timer(callback, delay) {
|
|
||||||
var now = Date.now(),
|
var now = Date.now(),
|
||||||
found = false,
|
found = false,
|
||||||
t0,
|
t0,
|
||||||
t1 = d3_timer_queue;
|
t1 = d3_timer_queue;
|
||||||
|
|
||||||
if (!isFinite(delay)) return;
|
if (arguments.length < 2) delay = 0;
|
||||||
|
else if (!isFinite(delay)) return;
|
||||||
|
|
||||||
// See if the callback's already in the queue.
|
// See if the callback's already in the queue.
|
||||||
while (t1) {
|
while (t1) {
|
||||||
|
@ -2130,7 +2127,7 @@ function d3_timer_step() {
|
||||||
|
|
||||||
while (t1) {
|
while (t1) {
|
||||||
elapsed = now - t1.then;
|
elapsed = now - t1.then;
|
||||||
if (elapsed > t1.delay) t1.flush = t1.callback(elapsed);
|
if (elapsed >= t1.delay) t1.flush = t1.callback(elapsed);
|
||||||
t1 = t1.next;
|
t1 = t1.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -3,17 +3,14 @@ var d3_timer_queue = null,
|
||||||
d3_timer_timeout; // is a timeout active?
|
d3_timer_timeout; // is a timeout active?
|
||||||
|
|
||||||
// The timer will continue to fire until callback returns true.
|
// The timer will continue to fire until callback returns true.
|
||||||
d3.timer = function(callback) {
|
d3.timer = function(callback, delay) {
|
||||||
d3_timer(callback, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
function d3_timer(callback, delay) {
|
|
||||||
var now = Date.now(),
|
var now = Date.now(),
|
||||||
found = false,
|
found = false,
|
||||||
t0,
|
t0,
|
||||||
t1 = d3_timer_queue;
|
t1 = d3_timer_queue;
|
||||||
|
|
||||||
if (!isFinite(delay)) return;
|
if (arguments.length < 2) delay = 0;
|
||||||
|
else if (!isFinite(delay)) return;
|
||||||
|
|
||||||
// See if the callback's already in the queue.
|
// See if the callback's already in the queue.
|
||||||
while (t1) {
|
while (t1) {
|
||||||
|
@ -50,7 +47,7 @@ function d3_timer_step() {
|
||||||
|
|
||||||
while (t1) {
|
while (t1) {
|
||||||
elapsed = now - t1.then;
|
elapsed = now - t1.then;
|
||||||
if (elapsed > t1.delay) t1.flush = t1.callback(elapsed);
|
if (elapsed >= t1.delay) t1.flush = t1.callback(elapsed);
|
||||||
t1 = t1.next;
|
t1 = t1.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ function d3_transition(groups) {
|
||||||
delay[++k] = delayMin;
|
delay[++k] = delayMin;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
d3_timer(step, delayMin);
|
d3.timer(step, delayMin);
|
||||||
return transition;
|
return transition;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
require("../env");
|
||||||
|
require("../../d3");
|
||||||
|
|
||||||
|
var vows = require("vows"),
|
||||||
|
assert = require("assert");
|
||||||
|
|
||||||
|
var suite = vows.describe("d3.timer");
|
||||||
|
|
||||||
|
suite.addBatch({
|
||||||
|
"timer": {
|
||||||
|
topic: function() {
|
||||||
|
return d3.timer;
|
||||||
|
},
|
||||||
|
"with no delay": {
|
||||||
|
topic: timer(),
|
||||||
|
"first calls after 17 ms or less": function(info) {
|
||||||
|
assert.inDelta(info.start - info.scheduled, 17, 15);
|
||||||
|
},
|
||||||
|
"calls until the function returns true": function(info) {
|
||||||
|
assert.equal(info.count, 4);
|
||||||
|
},
|
||||||
|
"calls every 17 ms": function(info) {
|
||||||
|
assert.inDelta(info.stop - info.start, 17 * 3, 15);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"with a specified delay": {
|
||||||
|
topic: timer(250),
|
||||||
|
"first calls after the delay": function(info) {
|
||||||
|
assert.inDelta(info.start - info.scheduled, 250, 15);
|
||||||
|
},
|
||||||
|
"calls until the function returns true": function(info) {
|
||||||
|
assert.equal(info.count, 4);
|
||||||
|
},
|
||||||
|
"calls every 17 ms": function(info) {
|
||||||
|
assert.inDelta(info.stop - info.start, 17 * 3, 15);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function timer(delay) {
|
||||||
|
var args = Array.prototype.slice.call(arguments);
|
||||||
|
return function() {
|
||||||
|
var cb = this.callback,
|
||||||
|
info = {scheduled: Date.now(), count: 0};
|
||||||
|
|
||||||
|
args.unshift(function() {
|
||||||
|
var count = ++info.count;
|
||||||
|
if (count === 1) {
|
||||||
|
info.start = Date.now();
|
||||||
|
} else if (count === 4) {
|
||||||
|
info.stop = Date.now();
|
||||||
|
cb(null, info);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
d3.timer.apply(this, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
suite.export(module);
|
Загрузка…
Ссылка в новой задаче