Skip to content

bug: rewrite throttle utility to robustly handle trailing edge #8988

@tobiu

Description

@tobiu

Context

The current throttle implementation in src/util/Function.mjs has two issues:

  1. Inefficiency: It clears and recreates setTimeout on every call during the cooldown period solely to capture the latest arguments.
  2. Fragility: It relies on a strict timing check (Date.now() - lastRanDate) >= delay inside the timeout. If the browser fires the timeout even 1ms early (common jitter), the trailing call is silently dropped.

Objective

Rewrite throttle to use a standard, robust pattern:

  1. Leading Edge: Execute immediately if now - lastRun > delay.
  2. Trailing Edge: If inside cooldown, store the latestArgs. Schedule a SINGLE timeout for the remaining time if one doesn't exist.
  3. Execution: When the timeout fires, execute with latestArgs, update lastRun, and clear the timeout flag.

This ensures ScrollManager (and other components) never lose the final update event, preventing visual desyncs.

Metadata

Metadata

Assignees

Labels

aibugSomething isn't workingcoreCore framework functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions