File size: 3,272 Bytes
0936134 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.d3_timer = {})));
}(this, function (exports) { 'use strict';
var frame = 0;
var timeout = 0;
var interval = 0;
var maxDelay = 5000;
var taskHead;
var taskTail;
var clockLast = 0;
var clockNow = 0;
var clock = typeof performance === "object" ? performance : Date;
var setFrame = typeof requestAnimationFrame === "function" ? requestAnimationFrame : function(callback) { return setTimeout(callback, 17); };
function now() {
return clockNow || (setFrame(clearNow), clockNow = clock.now());
}
function clearNow() {
clockNow = 0;
}
function Timer() {
this._call =
this._time =
this._next = null;
}
Timer.prototype = timer.prototype = {
restart: function(callback, delay, time) {
if (typeof callback !== "function") throw new TypeError("callback is not a function");
time = (time == null ? now() : +time) + (delay == null ? 0 : +delay);
if (!this._call) {
if (taskTail) taskTail._next = this;
else taskHead = this;
taskTail = this;
}
this._call = callback;
this._time = time;
sleep();
},
stop: function() {
if (this._call) {
this._call = null;
this._time = Infinity;
sleep();
}
}
};
function timer(callback, delay, time) {
var t = new Timer;
t.restart(callback, delay, time);
return t;
}
function timerOnce(callback, delay, time) {
var t = new Timer;
t.restart(function(elapsed) { t.stop(); callback(elapsed); }, delay, time);
return t;
}
function timerFlush() {
now(); // Get the current time, if not already set.
++frame; // Pretend we’ve set an alarm, if we haven’t already.
var t = taskHead, e;
while (t) {
if ((e = clockNow - t._time) >= 0) t._call.call(null, e);
t = t._next;
}
--frame;
}
function wake(time) {
clockLast = clockNow = time || clock.now();
frame = timeout = 0;
try {
timerFlush();
} finally {
frame = 0;
nap();
clockNow = 0;
}
}
function poke() {
if (clock.now() - clockLast > maxDelay) {
wake();
}
}
function nap() {
var t0, t1 = taskHead, time = Infinity;
while (t1) {
if (t1._call) {
if (time > t1._time) time = t1._time;
t1 = (t0 = t1)._next;
} else {
t1 = t0 ? t0._next = t1._next : taskHead = t1._next;
}
}
taskTail = t0;
sleep(time);
}
function sleep(time) {
if (frame) return; // Soonest alarm already set, or will be.
if (timeout) timeout = clearTimeout(timeout);
var delay = time - clockNow;
if (delay > 24) {
if (time < Infinity) timeout = setTimeout(wake, delay);
else if (interval) interval = clearInterval(interval);
} else {
if (!interval) interval = setInterval(poke, maxDelay);
frame = 1, setFrame(wake);
}
}
var version = "0.3.1";
exports.version = version;
exports.now = now;
exports.timer = timer;
exports.timerOnce = timerOnce;
exports.timerFlush = timerFlush;
})); |