2018-11-12 20:31:58 +08:00
|
|
|
import raf from 'raf';
|
|
|
|
|
|
|
|
interface RafMap {
|
2018-12-07 20:02:01 +08:00
|
|
|
[id: number]: number;
|
2018-11-12 20:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let id: number = 0;
|
|
|
|
const ids: RafMap = {};
|
|
|
|
|
|
|
|
// Support call raf with delay specified frame
|
|
|
|
export default function wrapperRaf(callback: () => void, delayFrames: number = 1): number {
|
|
|
|
const myId: number = id++;
|
|
|
|
let restFrames: number = delayFrames;
|
|
|
|
|
|
|
|
function internalCallback() {
|
|
|
|
restFrames -= 1;
|
|
|
|
|
|
|
|
if (restFrames <= 0) {
|
|
|
|
callback();
|
2019-04-28 23:52:35 +08:00
|
|
|
delete ids[myId];
|
2018-11-12 20:31:58 +08:00
|
|
|
} else {
|
2019-04-28 23:52:35 +08:00
|
|
|
ids[myId] = raf(internalCallback);
|
2018-11-12 20:31:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 23:52:35 +08:00
|
|
|
ids[myId] = raf(internalCallback);
|
2018-11-12 20:31:58 +08:00
|
|
|
|
|
|
|
return myId;
|
|
|
|
}
|
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapperRaf.cancel = function(pid?: number) {
|
|
|
|
if (pid === undefined) return;
|
|
|
|
|
2019-01-06 13:25:59 +08:00
|
|
|
raf.cancel(ids[pid]);
|
|
|
|
delete ids[pid];
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-04-09 16:39:33 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
wrapperRaf.ids = ids; // export this for test usage
|