UNPKG

1.34 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var raf = function raf(callback) {
8 return +setTimeout(callback, 16);
9};
10var caf = function caf(num) {
11 return clearTimeout(num);
12};
13if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
14 raf = function raf(callback) {
15 return window.requestAnimationFrame(callback);
16 };
17 caf = function caf(handle) {
18 return window.cancelAnimationFrame(handle);
19 };
20}
21var rafUUID = 0;
22var rafIds = new Map();
23function cleanup(id) {
24 rafIds.delete(id);
25}
26var wrapperRaf = function wrapperRaf(callback) {
27 var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
28 rafUUID += 1;
29 var id = rafUUID;
30 function callRef(leftTimes) {
31 if (leftTimes === 0) {
32 // Clean up
33 cleanup(id);
34
35 // Trigger
36 callback();
37 } else {
38 // Next raf
39 var realId = raf(function () {
40 callRef(leftTimes - 1);
41 });
42
43 // Bind real raf id
44 rafIds.set(id, realId);
45 }
46 }
47 callRef(times);
48 return id;
49};
50wrapperRaf.cancel = function (id) {
51 var realId = rafIds.get(id);
52 cleanup(id);
53 return caf(realId);
54};
55if (process.env.NODE_ENV !== 'production') {
56 wrapperRaf.ids = function () {
57 return rafIds;
58 };
59}
60var _default = exports.default = wrapperRaf;
\No newline at end of file