From a22e35a5567a20a013ce025af5cd48af8159b1c2 Mon Sep 17 00:00:00 2001 From: Josh <37798644+joshuaellis@users.noreply.github.com> Date: Wed, 13 Jul 2022 18:22:30 +0100 Subject: [PATCH] fix: run stop function and don't flush the current queue --- packages/rafz/src/index.test.ts | 2 ++ packages/rafz/src/index.ts | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/rafz/src/index.test.ts b/packages/rafz/src/index.test.ts index 58ad4faa28..d9ba5c7f1c 100644 --- a/packages/rafz/src/index.test.ts +++ b/packages/rafz/src/index.test.ts @@ -25,6 +25,7 @@ describe('raf looping', () => { }) it('stops looping after single job', () => { raf(() => {}) + mockRaf.step() expect(__raf.isRunning()).toBe(true) mockRaf.step() expect(__raf.isRunning()).toBe(false) @@ -35,6 +36,7 @@ describe('raf looping', () => { mockRaf.step() expect(fn).toHaveBeenCalledTimes(1) raf(fn) + mockRaf.step() expect(__raf.isRunning()).toBe(true) mockRaf.step() expect(__raf.isRunning()).toBe(false) diff --git a/packages/rafz/src/index.ts b/packages/rafz/src/index.ts index 56b84f7a1f..c9d046da7a 100644 --- a/packages/rafz/src/index.ts +++ b/packages/rafz/src/index.ts @@ -53,9 +53,9 @@ let findTimeout = (time: number) => raf.cancel = fn => { onStartQueue.delete(fn) onFrameQueue.delete(fn) + onFinishQueue.delete(fn) updateQueue.delete(fn) writeQueue.delete(fn) - onFinishQueue.delete(fn) } raf.sync = fn => { @@ -157,15 +157,17 @@ function update() { pendingCount -= count } + if (!pendingCount) { + stop() + + return + } + onStartQueue.flush() updateQueue.flush(prevTs ? Math.min(64, ts - prevTs) : 16.667) onFrameQueue.flush() writeQueue.flush() onFinishQueue.flush() - - if (!pendingCount) { - stop() - } } interface Queue {