| 1 | // https://d3js.org/d3-timer/ v3.0.1 Copyright 2010-2021 Mike Bostock
|
|---|
| 2 | (function (global, factory) {
|
|---|
| 3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|---|
| 4 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|---|
| 5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
|
|---|
| 6 | }(this, (function (exports) { 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | var frame = 0, // is an animation frame pending?
|
|---|
| 9 | timeout$1 = 0, // is a timeout pending?
|
|---|
| 10 | interval$1 = 0, // are any timers active?
|
|---|
| 11 | pokeDelay = 1000, // how frequently we check for clock skew
|
|---|
| 12 | taskHead,
|
|---|
| 13 | taskTail,
|
|---|
| 14 | clockLast = 0,
|
|---|
| 15 | clockNow = 0,
|
|---|
| 16 | clockSkew = 0,
|
|---|
| 17 | clock = typeof performance === "object" && performance.now ? performance : Date,
|
|---|
| 18 | setFrame = typeof window === "object" && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : function(f) { setTimeout(f, 17); };
|
|---|
| 19 |
|
|---|
| 20 | function now() {
|
|---|
| 21 | return clockNow || (setFrame(clearNow), clockNow = clock.now() + clockSkew);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | function clearNow() {
|
|---|
| 25 | clockNow = 0;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | function Timer() {
|
|---|
| 29 | this._call =
|
|---|
| 30 | this._time =
|
|---|
| 31 | this._next = null;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | Timer.prototype = timer.prototype = {
|
|---|
| 35 | constructor: Timer,
|
|---|
| 36 | restart: function(callback, delay, time) {
|
|---|
| 37 | if (typeof callback !== "function") throw new TypeError("callback is not a function");
|
|---|
| 38 | time = (time == null ? now() : +time) + (delay == null ? 0 : +delay);
|
|---|
| 39 | if (!this._next && taskTail !== this) {
|
|---|
| 40 | if (taskTail) taskTail._next = this;
|
|---|
| 41 | else taskHead = this;
|
|---|
| 42 | taskTail = this;
|
|---|
| 43 | }
|
|---|
| 44 | this._call = callback;
|
|---|
| 45 | this._time = time;
|
|---|
| 46 | sleep();
|
|---|
| 47 | },
|
|---|
| 48 | stop: function() {
|
|---|
| 49 | if (this._call) {
|
|---|
| 50 | this._call = null;
|
|---|
| 51 | this._time = Infinity;
|
|---|
| 52 | sleep();
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | function timer(callback, delay, time) {
|
|---|
| 58 | var t = new Timer;
|
|---|
| 59 | t.restart(callback, delay, time);
|
|---|
| 60 | return t;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | function timerFlush() {
|
|---|
| 64 | now(); // Get the current time, if not already set.
|
|---|
| 65 | ++frame; // Pretend we’ve set an alarm, if we haven’t already.
|
|---|
| 66 | var t = taskHead, e;
|
|---|
| 67 | while (t) {
|
|---|
| 68 | if ((e = clockNow - t._time) >= 0) t._call.call(undefined, e);
|
|---|
| 69 | t = t._next;
|
|---|
| 70 | }
|
|---|
| 71 | --frame;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | function wake() {
|
|---|
| 75 | clockNow = (clockLast = clock.now()) + clockSkew;
|
|---|
| 76 | frame = timeout$1 = 0;
|
|---|
| 77 | try {
|
|---|
| 78 | timerFlush();
|
|---|
| 79 | } finally {
|
|---|
| 80 | frame = 0;
|
|---|
| 81 | nap();
|
|---|
| 82 | clockNow = 0;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | function poke() {
|
|---|
| 87 | var now = clock.now(), delay = now - clockLast;
|
|---|
| 88 | if (delay > pokeDelay) clockSkew -= delay, clockLast = now;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | function nap() {
|
|---|
| 92 | var t0, t1 = taskHead, t2, time = Infinity;
|
|---|
| 93 | while (t1) {
|
|---|
| 94 | if (t1._call) {
|
|---|
| 95 | if (time > t1._time) time = t1._time;
|
|---|
| 96 | t0 = t1, t1 = t1._next;
|
|---|
| 97 | } else {
|
|---|
| 98 | t2 = t1._next, t1._next = null;
|
|---|
| 99 | t1 = t0 ? t0._next = t2 : taskHead = t2;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | taskTail = t0;
|
|---|
| 103 | sleep(time);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | function sleep(time) {
|
|---|
| 107 | if (frame) return; // Soonest alarm already set, or will be.
|
|---|
| 108 | if (timeout$1) timeout$1 = clearTimeout(timeout$1);
|
|---|
| 109 | var delay = time - clockNow; // Strictly less than if we recomputed clockNow.
|
|---|
| 110 | if (delay > 24) {
|
|---|
| 111 | if (time < Infinity) timeout$1 = setTimeout(wake, time - clock.now() - clockSkew);
|
|---|
| 112 | if (interval$1) interval$1 = clearInterval(interval$1);
|
|---|
| 113 | } else {
|
|---|
| 114 | if (!interval$1) clockLast = clock.now(), interval$1 = setInterval(poke, pokeDelay);
|
|---|
| 115 | frame = 1, setFrame(wake);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | function timeout(callback, delay, time) {
|
|---|
| 120 | var t = new Timer;
|
|---|
| 121 | delay = delay == null ? 0 : +delay;
|
|---|
| 122 | t.restart(elapsed => {
|
|---|
| 123 | t.stop();
|
|---|
| 124 | callback(elapsed + delay);
|
|---|
| 125 | }, delay, time);
|
|---|
| 126 | return t;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | function interval(callback, delay, time) {
|
|---|
| 130 | var t = new Timer, total = delay;
|
|---|
| 131 | if (delay == null) return t.restart(callback, delay, time), t;
|
|---|
| 132 | t._restart = t.restart;
|
|---|
| 133 | t.restart = function(callback, delay, time) {
|
|---|
| 134 | delay = +delay, time = time == null ? now() : +time;
|
|---|
| 135 | t._restart(function tick(elapsed) {
|
|---|
| 136 | elapsed += total;
|
|---|
| 137 | t._restart(tick, total += delay, time);
|
|---|
| 138 | callback(elapsed);
|
|---|
| 139 | }, delay, time);
|
|---|
| 140 | };
|
|---|
| 141 | t.restart(callback, delay, time);
|
|---|
| 142 | return t;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | exports.interval = interval;
|
|---|
| 146 | exports.now = now;
|
|---|
| 147 | exports.timeout = timeout;
|
|---|
| 148 | exports.timer = timer;
|
|---|
| 149 | exports.timerFlush = timerFlush;
|
|---|
| 150 |
|
|---|
| 151 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 152 |
|
|---|
| 153 | })));
|
|---|