unis_crm/frontend1/node_modules/@rc-component/util/lib/raf.js

49 lines
1.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
let raf = callback => +setTimeout(callback, 16);
let caf = num => clearTimeout(num);
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
raf = callback => window.requestAnimationFrame(callback);
caf = handle => window.cancelAnimationFrame(handle);
}
let rafUUID = 0;
const rafIds = new Map();
function cleanup(id) {
rafIds.delete(id);
}
const wrapperRaf = (callback, times = 1) => {
rafUUID += 1;
const id = rafUUID;
function callRef(leftTimes) {
if (leftTimes === 0) {
// Clean up
cleanup(id);
// Trigger
callback();
} else {
// Next raf
const realId = raf(() => {
callRef(leftTimes - 1);
});
// Bind real raf id
rafIds.set(id, realId);
}
}
callRef(times);
return id;
};
wrapperRaf.cancel = id => {
const realId = rafIds.get(id);
cleanup(id);
return caf(realId);
};
if (process.env.NODE_ENV !== 'production') {
wrapperRaf.ids = () => rafIds;
}
var _default = exports.default = wrapperRaf;