Skip to content

Commit 36cd9ca

Browse files
authored
add raf util (#112)
* add raf util * remove comment * add test case
1 parent 1e8543f commit 36cd9ca

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/raf.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let raf = (fn: () => void) => +setTimeout(fn, 16);
2+
let caf = (num: number) => clearTimeout(num);
3+
4+
if (typeof window !== 'undefined') {
5+
raf = requestAnimationFrame;
6+
caf = cancelAnimationFrame;
7+
}
8+
9+
export default function wrapperRaf(callback: () => void): number {
10+
return raf(callback);
11+
}
12+
13+
wrapperRaf.cancel = caf;

tests/raf.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import raf from '../src/raf';
2+
3+
describe('raf', () => {
4+
it('test Raf', done => {
5+
jest.useRealTimers();
6+
7+
let bamboo = false;
8+
raf(() => {
9+
bamboo = true;
10+
});
11+
12+
expect(bamboo).toBe(false);
13+
14+
raf(() => {
15+
expect(bamboo).toBe(true);
16+
done();
17+
});
18+
});
19+
});

0 commit comments

Comments
 (0)