Skip to content

Commit

Permalink
fix: raf.cancel not clean up correct map id (#482)
Browse files Browse the repository at this point in the history
* test: test driven

* fix: raf canel logic
  • Loading branch information
zombieJ authored Nov 6, 2023
1 parent 677d3ac commit d7901b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/raf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ const wrapperRaf = (callback: () => void, times = 1): number => {

wrapperRaf.cancel = (id: number) => {
const realId = rafIds.get(id);
cleanup(realId);
cleanup(id);
return caf(realId);
};

if (process.env.NODE_ENV !== 'production') {
wrapperRaf.ids = () => rafIds;
}

export default wrapperRaf;
8 changes: 8 additions & 0 deletions tests/raf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ describe('raf', () => {
it('cancel', done => {
let bamboo = false;

// Call some native raf
for (let i = 0; i < 10; i += 1) {
const nativeId = requestAnimationFrame(() => {});
cancelAnimationFrame(nativeId);
}

const id = raf(() => {
bamboo = true;
}, 2);
expect(raf.ids().has(id)).toBeTruthy();

raf.cancel(id);
expect(raf.ids().has(id)).toBeFalsy();

requestAnimationFrame(() => {
requestAnimationFrame(() => {
Expand Down

0 comments on commit d7901b2

Please sign in to comment.