| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.Timer = Timer;
|
|---|
| 7 | exports.now = now;
|
|---|
| 8 | exports.timer = timer;
|
|---|
| 9 | exports.timerFlush = timerFlush;
|
|---|
| 10 | var frame = 0,
|
|---|
| 11 | // is an animation frame pending?
|
|---|
| 12 | timeout = 0,
|
|---|
| 13 | // is a timeout pending?
|
|---|
| 14 | interval = 0,
|
|---|
| 15 | // are any timers active?
|
|---|
| 16 | pokeDelay = 1000,
|
|---|
| 17 | // how frequently we check for clock skew
|
|---|
| 18 | taskHead,
|
|---|
| 19 | taskTail,
|
|---|
| 20 | clockLast = 0,
|
|---|
| 21 | clockNow = 0,
|
|---|
| 22 | clockSkew = 0,
|
|---|
| 23 | clock = typeof performance === "object" && performance.now ? performance : Date,
|
|---|
| 24 | setFrame = typeof window === "object" && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : function (f) {
|
|---|
| 25 | setTimeout(f, 17);
|
|---|
| 26 | };
|
|---|
| 27 | function now() {
|
|---|
| 28 | return clockNow || (setFrame(clearNow), clockNow = clock.now() + clockSkew);
|
|---|
| 29 | }
|
|---|
| 30 | function clearNow() {
|
|---|
| 31 | clockNow = 0;
|
|---|
| 32 | }
|
|---|
| 33 | function Timer() {
|
|---|
| 34 | this._call = this._time = this._next = null;
|
|---|
| 35 | }
|
|---|
| 36 | Timer.prototype = timer.prototype = {
|
|---|
| 37 | constructor: Timer,
|
|---|
| 38 | restart: function (callback, delay, time) {
|
|---|
| 39 | if (typeof callback !== "function") throw new TypeError("callback is not a function");
|
|---|
| 40 | time = (time == null ? now() : +time) + (delay == null ? 0 : +delay);
|
|---|
| 41 | if (!this._next && taskTail !== this) {
|
|---|
| 42 | if (taskTail) taskTail._next = this;else taskHead = this;
|
|---|
| 43 | taskTail = this;
|
|---|
| 44 | }
|
|---|
| 45 | this._call = callback;
|
|---|
| 46 | this._time = time;
|
|---|
| 47 | sleep();
|
|---|
| 48 | },
|
|---|
| 49 | stop: function () {
|
|---|
| 50 | if (this._call) {
|
|---|
| 51 | this._call = null;
|
|---|
| 52 | this._time = Infinity;
|
|---|
| 53 | sleep();
|
|---|
| 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 | function timerFlush() {
|
|---|
| 63 | now(); // Get the current time, if not already set.
|
|---|
| 64 | ++frame; // Pretend we’ve set an alarm, if we haven’t already.
|
|---|
| 65 | var t = taskHead,
|
|---|
| 66 | 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 | function wake() {
|
|---|
| 74 | clockNow = (clockLast = clock.now()) + clockSkew;
|
|---|
| 75 | frame = timeout = 0;
|
|---|
| 76 | try {
|
|---|
| 77 | timerFlush();
|
|---|
| 78 | } finally {
|
|---|
| 79 | frame = 0;
|
|---|
| 80 | nap();
|
|---|
| 81 | clockNow = 0;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | function poke() {
|
|---|
| 85 | var now = clock.now(),
|
|---|
| 86 | delay = now - clockLast;
|
|---|
| 87 | if (delay > pokeDelay) clockSkew -= delay, clockLast = now;
|
|---|
| 88 | }
|
|---|
| 89 | function nap() {
|
|---|
| 90 | var t0,
|
|---|
| 91 | t1 = taskHead,
|
|---|
| 92 | t2,
|
|---|
| 93 | time = Infinity;
|
|---|
| 94 | while (t1) {
|
|---|
| 95 | if (t1._call) {
|
|---|
| 96 | if (time > t1._time) time = t1._time;
|
|---|
| 97 | t0 = t1, t1 = t1._next;
|
|---|
| 98 | } else {
|
|---|
| 99 | t2 = t1._next, t1._next = null;
|
|---|
| 100 | t1 = t0 ? t0._next = t2 : taskHead = t2;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | taskTail = t0;
|
|---|
| 104 | sleep(time);
|
|---|
| 105 | }
|
|---|
| 106 | function sleep(time) {
|
|---|
| 107 | if (frame) return; // Soonest alarm already set, or will be.
|
|---|
| 108 | if (timeout) timeout = clearTimeout(timeout);
|
|---|
| 109 | var delay = time - clockNow; // Strictly less than if we recomputed clockNow.
|
|---|
| 110 | if (delay > 24) {
|
|---|
| 111 | if (time < Infinity) timeout = setTimeout(wake, time - clock.now() - clockSkew);
|
|---|
| 112 | if (interval) interval = clearInterval(interval);
|
|---|
| 113 | } else {
|
|---|
| 114 | if (!interval) clockLast = clock.now(), interval = setInterval(poke, pokeDelay);
|
|---|
| 115 | frame = 1, setFrame(wake);
|
|---|
| 116 | }
|
|---|
| 117 | } |
|---|