From d7901b2b9e1e78d8b2385e75da94ebd0cab25791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Mon, 6 Nov 2023 18:53:10 +0800 Subject: [PATCH] fix: raf.cancel not clean up correct map id (#482) * test: test driven * fix: raf canel logic --- src/raf.ts | 6 +++++- tests/raf.test.js | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/raf.ts b/src/raf.ts index 1298d21b..0e5f7d49 100644 --- a/src/raf.ts +++ b/src/raf.ts @@ -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; diff --git a/tests/raf.test.js b/tests/raf.test.js index 5a17076c..e6bfa22d 100644 --- a/tests/raf.test.js +++ b/tests/raf.test.js @@ -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(() => {