Skip to content

Commit d7901b2

Browse files
authored
fix: raf.cancel not clean up correct map id (#482)
* test: test driven * fix: raf canel logic
1 parent 677d3ac commit d7901b2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/raf.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ const wrapperRaf = (callback: () => void, times = 1): number => {
4444

4545
wrapperRaf.cancel = (id: number) => {
4646
const realId = rafIds.get(id);
47-
cleanup(realId);
47+
cleanup(id);
4848
return caf(realId);
4949
};
5050

51+
if (process.env.NODE_ENV !== 'production') {
52+
wrapperRaf.ids = () => rafIds;
53+
}
54+
5155
export default wrapperRaf;

tests/raf.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ describe('raf', () => {
2020
it('cancel', done => {
2121
let bamboo = false;
2222

23+
// Call some native raf
24+
for (let i = 0; i < 10; i += 1) {
25+
const nativeId = requestAnimationFrame(() => {});
26+
cancelAnimationFrame(nativeId);
27+
}
28+
2329
const id = raf(() => {
2430
bamboo = true;
2531
}, 2);
32+
expect(raf.ids().has(id)).toBeTruthy();
2633

2734
raf.cancel(id);
35+
expect(raf.ids().has(id)).toBeFalsy();
2836

2937
requestAnimationFrame(() => {
3038
requestAnimationFrame(() => {

0 commit comments

Comments
 (0)