Comparing version 0.0.1 to 0.0.2
@@ -20,15 +20,22 @@ if (typeof requestAnimationFrame === "undefined") { | ||
frame, // is an animation frame pending? | ||
timeout; // is a timeout pending? | ||
timeout, // is a timeout pending? | ||
timeoutTime = Infinity; // the time the timeout will fire | ||
function step() { | ||
frame = 0; | ||
var delay = timerFlush(); | ||
if (frame) return; // Return if timer(…) was called during flush. | ||
function wake() { | ||
frame = timeout = 0, timeoutTime = Infinity; | ||
wakeAt(timerFlush()); | ||
} | ||
function wakeAt(time) { | ||
if (frame) return; // Fastest wake already set. | ||
var delay = time - Date.now(); | ||
if (delay > 24) { | ||
if (isFinite(delay)) { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(step, delay); | ||
if (timeoutTime > time) { // Note: false if time is infinite. | ||
if (timeout) clearTimeout(timeout); | ||
timeout = setTimeout(wake, delay); | ||
timeoutTime = time; | ||
} | ||
} else { | ||
frame = requestAnimationFrame(step); | ||
if (timeout) timeout = clearTimeout(timeout), timeoutTime = Infinity; | ||
frame = requestAnimationFrame(wake); | ||
} | ||
@@ -46,8 +53,8 @@ } | ||
// The timer will continue to fire until callback returns true. | ||
function timer(callback, delay, then) { | ||
if (delay == null) delay = 0; | ||
if (then == null) then = Date.now(); | ||
function timer(callback, delay, time) { | ||
if (time == null) time = Date.now(); else time = +time; | ||
if (delay != null) time += +delay; | ||
// Add the callback to the tail of the queue. | ||
var timer = new Timer(callback, then + delay); | ||
var timer = new Timer(callback, time); | ||
if (queueTail) queueTail.next = timer; | ||
@@ -57,7 +64,3 @@ else queueHead = timer; | ||
// Start animatin’! | ||
if (!frame) { | ||
timeout = clearTimeout(timeout); | ||
frame = requestAnimationFrame(step); | ||
} | ||
wakeAt(time); | ||
} | ||
@@ -67,7 +70,7 @@ | ||
// Replace the current timer. Only allowed within a timer callback. | ||
function timerReplace(callback, delay, then) { | ||
if (delay == null) delay = 0; | ||
if (then == null) then = Date.now(); | ||
function timerReplace(callback, delay, time) { | ||
if (time == null) time = Date.now(); else time = +time; | ||
if (delay != null) time += +delay; | ||
active.callback = callback; | ||
active.time = then + delay; | ||
active.time = time; | ||
} | ||
@@ -78,6 +81,6 @@ | ||
// then flush completed timers to avoid concurrent queue modification. | ||
// Returns the delay until the nextmost active timer. | ||
function timerFlush() { | ||
var now = Date.now(), | ||
active0 = active; | ||
// Returns the time of the earliest active timer. | ||
function timerFlush(time) { | ||
if (time == null) time = Date.now(); else time = +time; | ||
var active0 = active; | ||
@@ -87,6 +90,7 @@ // Note: timerFlush can be re-entrant, so we must preserve the old active. | ||
while (active) { | ||
if (now >= active.time) active.flush = active.callback(now - active.time, now); | ||
if (time >= active.time) active.flush = active.callback(time - active.time, time); | ||
active = active.next; | ||
} | ||
active = active0; | ||
time = Infinity; | ||
@@ -97,4 +101,3 @@ // Note: invoking a timer callback can change the timer queue (due to a re- | ||
var t0, | ||
t1 = queueHead, | ||
time = Infinity; | ||
t1 = queueHead; | ||
while (t1) { | ||
@@ -109,3 +112,3 @@ if (t1.flush) { | ||
queueTail = t0; | ||
return time - now; | ||
return time; | ||
} | ||
@@ -112,0 +115,0 @@ |
@@ -1,1 +0,1 @@ | ||
"undefined"==typeof requestAnimationFrame&&(requestAnimationFrame="undefined"!=typeof window&&(window.msRequestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.oRequestAnimationFrame)||function(e){return setTimeout(e,17)}),function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.timer={})}(this,function(e){"use strict";function t(){s=0;var e=u();s||(e>24?isFinite(e)&&(clearTimeout(f),f=setTimeout(t,e)):s=requestAnimationFrame(t))}function n(e,t){this.callback=e,this.time=t,this.flush=!1,this.next=null}function i(e,i,o){null==i&&(i=0),null==o&&(o=Date.now());var u=new n(e,o+i);m?m.next=u:a=u,m=u,s||(f=clearTimeout(f),s=requestAnimationFrame(t))}function o(e,t,n){null==t&&(t=0),null==n&&(n=Date.now()),r.callback=e,r.time=n+t}function u(){var e=Date.now(),t=r;for(r=a;r;)e>=r.time&&(r.flush=r.callback(e-r.time,e)),r=r.next;r=t;for(var n,i=a,o=1/0;i;)i.flush?i=n?n.next=i.next:a=i.next:(i.time<o&&(o=i.time),i=(n=i).next);return m=n,o-e}var a,m,r,s,f;e.timer=i,e.timerReplace=o,e.timerFlush=u}); | ||
"undefined"==typeof requestAnimationFrame&&(requestAnimationFrame="undefined"!=typeof window&&(window.msRequestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.oRequestAnimationFrame)||function(e){return setTimeout(e,17)}),function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.timer={})}(this,function(e){"use strict";function t(){l=s=0,c=1/0,n(a())}function n(e){if(!l){var n=e-Date.now();n>24?c>e&&(s&&clearTimeout(s),s=setTimeout(t,n),c=e):(s&&(s=clearTimeout(s),c=1/0),l=requestAnimationFrame(t))}}function i(e,t){this.callback=e,this.time=t,this.flush=!1,this.next=null}function o(e,t,o){o=null==o?Date.now():+o,null!=t&&(o+=+t);var u=new i(e,o);r?r.next=u:m=u,r=u,n(o)}function u(e,t,n){n=null==n?Date.now():+n,null!=t&&(n+=+t),f.callback=e,f.time=n}function a(e){e=null==e?Date.now():+e;var t=f;for(f=m;f;)e>=f.time&&(f.flush=f.callback(e-f.time,e)),f=f.next;f=t,e=1/0;for(var n,i=m;i;)i.flush?i=n?n.next=i.next:m=i.next:(i.time<e&&(e=i.time),i=(n=i).next);return r=n,e}var m,r,f,l,s,c=1/0;e.timer=o,e.timerReplace=u,e.timerFlush=a}); |
{ | ||
"name": "d3-timer", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "An efficient queue capable of managing thousands of concurrent animations.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,12 +7,10 @@ # d3-timer | ||
* Timer callbacks are now passed the current time as a second argument, in addition to the elapsed time; this is useful for precise scheduling of secondary timers. | ||
* A new [timerReplace](#timerReplace) method has been added to replace the current timer within a timer callback. | ||
* The timer.flush method has been renamed [timerFlush](#timerFlush). | ||
* The timer.flush method has been renamed [timerFlush](#timerFlush). This method now accepts an optional *time* argument representing the current time, and returns the time of the earliest next timer. Calling this method within a timer callback no longer causes a crash. | ||
* Calling [timerFlush](#timerFlush) within a timer callback no longer causes a crash. | ||
* Calling [timer](#timer) within a timer callback no longer makes a duplicate requestAnimationFrame. Calling this method with a delay greater than 24ms when no earlier timers are active guarantees a setTimeout rather than a requestAnimationFrame. | ||
* Calling [timer](#timer) within a timer callback no longer makes a duplicate requestAnimationFrame. | ||
* Timer callbacks are now passed the current time as a second argument, in addition to the elapsed time; this is useful for precise scheduling of secondary timers. | ||
<a name="timer" href="#timer">#</a> <b>timer</b>(<i>callback</i>[, <i>delay</i>[, <i>time</i>]]) | ||
@@ -19,0 +17,0 @@ |
@@ -5,11 +5,12 @@ var queueHead, | ||
frame, // is an animation frame pending? | ||
timeout; // is a timeout pending? | ||
timeout, // is a timeout pending? | ||
timeoutTime = Infinity; // the time the timeout will fire | ||
// The timer will continue to fire until callback returns true. | ||
export function timer(callback, delay, then) { | ||
if (delay == null) delay = 0; | ||
if (then == null) then = Date.now(); | ||
export function timer(callback, delay, time) { | ||
if (time == null) time = Date.now(); else time = +time; | ||
if (delay != null) time += +delay; | ||
// Add the callback to the tail of the queue. | ||
var timer = new Timer(callback, then + delay); | ||
var timer = new Timer(callback, time); | ||
if (queueTail) queueTail.next = timer; | ||
@@ -19,15 +20,11 @@ else queueHead = timer; | ||
// Start animatin’! | ||
if (!frame) { | ||
timeout = clearTimeout(timeout); | ||
frame = requestAnimationFrame(step); | ||
} | ||
wakeAt(time); | ||
}; | ||
// Replace the current timer. Only allowed within a timer callback. | ||
export function timerReplace(callback, delay, then) { | ||
if (delay == null) delay = 0; | ||
if (then == null) then = Date.now(); | ||
export function timerReplace(callback, delay, time) { | ||
if (time == null) time = Date.now(); else time = +time; | ||
if (delay != null) time += +delay; | ||
active.callback = callback; | ||
active.time = then + delay; | ||
active.time = time; | ||
}; | ||
@@ -37,6 +34,6 @@ | ||
// then flush completed timers to avoid concurrent queue modification. | ||
// Returns the delay until the nextmost active timer. | ||
export function timerFlush() { | ||
var now = Date.now(), | ||
active0 = active; | ||
// Returns the time of the earliest active timer. | ||
export function timerFlush(time) { | ||
if (time == null) time = Date.now(); else time = +time; | ||
var active0 = active; | ||
@@ -46,6 +43,7 @@ // Note: timerFlush can be re-entrant, so we must preserve the old active. | ||
while (active) { | ||
if (now >= active.time) active.flush = active.callback(now - active.time, now); | ||
if (time >= active.time) active.flush = active.callback(time - active.time, time); | ||
active = active.next; | ||
} | ||
active = active0; | ||
time = Infinity; | ||
@@ -56,4 +54,3 @@ // Note: invoking a timer callback can change the timer queue (due to a re- | ||
var t0, | ||
t1 = queueHead, | ||
time = Infinity; | ||
t1 = queueHead; | ||
while (t1) { | ||
@@ -68,3 +65,3 @@ if (t1.flush) { | ||
queueTail = t0; | ||
return time - now; | ||
return time; | ||
}; | ||
@@ -79,14 +76,20 @@ | ||
function step() { | ||
frame = 0; | ||
var delay = timerFlush(); | ||
if (frame) return; // Return if timer(…) was called during flush. | ||
function wake() { | ||
frame = timeout = 0, timeoutTime = Infinity; | ||
wakeAt(timerFlush()); | ||
} | ||
function wakeAt(time) { | ||
if (frame) return; // Fastest wake already set. | ||
var delay = time - Date.now(); | ||
if (delay > 24) { | ||
if (isFinite(delay)) { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(step, delay); | ||
if (timeoutTime > time) { // Note: false if time is infinite. | ||
if (timeout) clearTimeout(timeout); | ||
timeout = setTimeout(wake, delay); | ||
timeoutTime = time; | ||
} | ||
} else { | ||
frame = requestAnimationFrame(step); | ||
if (timeout) timeout = clearTimeout(timeout), timeoutTime = Infinity; | ||
frame = requestAnimationFrame(wake); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13093
182
36