2019-01-12 03:33:27 +00:00
|
|
|
import raf from 'raf';
|
2018-12-13 13:26:21 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let id = 0;
|
|
|
|
const ids = {};
|
2018-12-13 13:26:21 +00:00
|
|
|
|
|
|
|
// Support call raf with delay specified frame
|
2019-01-12 03:33:27 +00:00
|
|
|
export default function wrapperRaf(callback, delayFrames = 1) {
|
|
|
|
const myId = id++;
|
|
|
|
let restFrames = delayFrames;
|
2018-12-13 13:26:21 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function internalCallback() {
|
|
|
|
restFrames -= 1;
|
2018-12-13 13:26:21 +00:00
|
|
|
|
|
|
|
if (restFrames <= 0) {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback();
|
|
|
|
delete ids[id];
|
2018-12-13 13:26:21 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
ids[id] = raf(internalCallback);
|
2018-12-13 13:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
ids[id] = raf(internalCallback);
|
2018-12-13 13:26:21 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return myId;
|
2018-12-13 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 05:26:03 +00:00
|
|
|
wrapperRaf.cancel = function(pid) {
|
|
|
|
raf.cancel(ids[pid]);
|
|
|
|
delete ids[pid];
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|