From 757dfeb6ad64c2c58db8ce356a54e3f440e433c4 Mon Sep 17 00:00:00 2001 From: David Luzar <5153846+dwelle@users.noreply.github.com> Date: Fri, 6 Mar 2026 20:40:36 +0100 Subject: [PATCH] fix(editor): call throttleRAF with lastArgs and remove trailing (#10905) Co-authored-by: Varun Chawla Co-authored-by: aziamimoh Co-authored-by: pgzcoa Co-authored-by: TinaZhang24 --- excalidraw-app/components/DebugCanvas.tsx | 1 - packages/common/src/utils.ts | 29 +++++++------------ .../renderer/renderNewElementScene.ts | 1 - packages/excalidraw/renderer/staticScene.ts | 1 - 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/excalidraw-app/components/DebugCanvas.tsx b/excalidraw-app/components/DebugCanvas.tsx index 2c157e34e7..d9279296f7 100644 --- a/excalidraw-app/components/DebugCanvas.tsx +++ b/excalidraw-app/components/DebugCanvas.tsx @@ -414,7 +414,6 @@ export const debugRenderer = throttleRAF( ) => { _debugRenderer(canvas, appState, elements, scale); }, - { trailing: true }, ); export const loadSavedDebugState = () => { diff --git a/packages/common/src/utils.ts b/packages/common/src/utils.ts index 6d3858a00d..26c75dcfa2 100644 --- a/packages/common/src/utils.ts +++ b/packages/common/src/utils.ts @@ -151,24 +151,19 @@ export const debounce = ( return ret; }; -// throttle callback to execute once per animation frame -export const throttleRAF = ( - fn: (...args: T) => void, - opts?: { trailing?: boolean }, -) => { +// throttle callback to execute once per animation frame using the latest args +export const throttleRAF = (fn: (...args: T) => void) => { let timerId: number | null = null; let lastArgs: T | null = null; - let lastArgsTrailing: T | null = null; - const scheduleFunc = (args: T) => { + const scheduleFunc = () => { timerId = window.requestAnimationFrame(() => { timerId = null; - fn(...args); + const args = lastArgs; lastArgs = null; - if (lastArgsTrailing) { - lastArgs = lastArgsTrailing; - lastArgsTrailing = null; - scheduleFunc(lastArgs); + + if (args) { + fn(...args); } }); }; @@ -180,9 +175,7 @@ export const throttleRAF = ( } lastArgs = args; if (timerId === null) { - scheduleFunc(lastArgs); - } else if (opts?.trailing) { - lastArgsTrailing = args; + scheduleFunc(); } }; ret.flush = () => { @@ -191,12 +184,12 @@ export const throttleRAF = ( timerId = null; } if (lastArgs) { - fn(...(lastArgsTrailing || lastArgs)); - lastArgs = lastArgsTrailing = null; + fn(...lastArgs); + lastArgs = null; } }; ret.cancel = () => { - lastArgs = lastArgsTrailing = null; + lastArgs = null; if (timerId !== null) { cancelAnimationFrame(timerId); timerId = null; diff --git a/packages/excalidraw/renderer/renderNewElementScene.ts b/packages/excalidraw/renderer/renderNewElementScene.ts index 07215dfc07..31aa721329 100644 --- a/packages/excalidraw/renderer/renderNewElementScene.ts +++ b/packages/excalidraw/renderer/renderNewElementScene.ts @@ -88,7 +88,6 @@ export const renderNewElementSceneThrottled = throttleRAF( (config: NewElementSceneRenderConfig) => { _renderNewElementScene(config); }, - { trailing: true }, ); export const renderNewElementScene = ( diff --git a/packages/excalidraw/renderer/staticScene.ts b/packages/excalidraw/renderer/staticScene.ts index 83e45a1227..48c72f8d37 100644 --- a/packages/excalidraw/renderer/staticScene.ts +++ b/packages/excalidraw/renderer/staticScene.ts @@ -483,7 +483,6 @@ export const renderStaticSceneThrottled = throttleRAF( (config: StaticSceneRenderConfig) => { _renderStaticScene(config); }, - { trailing: true }, ); /**