source: node_modules/d3-timer/src/timer.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 2.8 KB
Line 
1var frame = 0, // is an animation frame pending?
2 timeout = 0, // is a timeout pending?
3 interval = 0, // are any timers active?
4 pokeDelay = 1000, // how frequently we check for clock skew
5 taskHead,
6 taskTail,
7 clockLast = 0,
8 clockNow = 0,
9 clockSkew = 0,
10 clock = typeof performance === "object" && performance.now ? performance : Date,
11 setFrame = typeof window === "object" && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : function(f) { setTimeout(f, 17); };
12
13export function now() {
14 return clockNow || (setFrame(clearNow), clockNow = clock.now() + clockSkew);
15}
16
17function clearNow() {
18 clockNow = 0;
19}
20
21export function Timer() {
22 this._call =
23 this._time =
24 this._next = null;
25}
26
27Timer.prototype = timer.prototype = {
28 constructor: Timer,
29 restart: function(callback, delay, time) {
30 if (typeof callback !== "function") throw new TypeError("callback is not a function");
31 time = (time == null ? now() : +time) + (delay == null ? 0 : +delay);
32 if (!this._next && taskTail !== this) {
33 if (taskTail) taskTail._next = this;
34 else taskHead = this;
35 taskTail = this;
36 }
37 this._call = callback;
38 this._time = time;
39 sleep();
40 },
41 stop: function() {
42 if (this._call) {
43 this._call = null;
44 this._time = Infinity;
45 sleep();
46 }
47 }
48};
49
50export function timer(callback, delay, time) {
51 var t = new Timer;
52 t.restart(callback, delay, time);
53 return t;
54}
55
56export function timerFlush() {
57 now(); // Get the current time, if not already set.
58 ++frame; // Pretend we’ve set an alarm, if we haven’t already.
59 var t = taskHead, e;
60 while (t) {
61 if ((e = clockNow - t._time) >= 0) t._call.call(undefined, e);
62 t = t._next;
63 }
64 --frame;
65}
66
67function wake() {
68 clockNow = (clockLast = clock.now()) + clockSkew;
69 frame = timeout = 0;
70 try {
71 timerFlush();
72 } finally {
73 frame = 0;
74 nap();
75 clockNow = 0;
76 }
77}
78
79function poke() {
80 var now = clock.now(), delay = now - clockLast;
81 if (delay > pokeDelay) clockSkew -= delay, clockLast = now;
82}
83
84function nap() {
85 var t0, t1 = taskHead, t2, time = Infinity;
86 while (t1) {
87 if (t1._call) {
88 if (time > t1._time) time = t1._time;
89 t0 = t1, t1 = t1._next;
90 } else {
91 t2 = t1._next, t1._next = null;
92 t1 = t0 ? t0._next = t2 : taskHead = t2;
93 }
94 }
95 taskTail = t0;
96 sleep(time);
97}
98
99function sleep(time) {
100 if (frame) return; // Soonest alarm already set, or will be.
101 if (timeout) timeout = clearTimeout(timeout);
102 var delay = time - clockNow; // Strictly less than if we recomputed clockNow.
103 if (delay > 24) {
104 if (time < Infinity) timeout = setTimeout(wake, time - clock.now() - clockSkew);
105 if (interval) interval = clearInterval(interval);
106 } else {
107 if (!interval) clockLast = clock.now(), interval = setInterval(poke, pokeDelay);
108 frame = 1, setFrame(wake);
109 }
110}
Note: See TracBrowser for help on using the repository browser.