Implement throttle with leading and trailing calls
Build a context-preserving throttle with controllable leading, trailing, cancel, and flush behavior.Description
Requirements
The solution exports the required throttle API from the editable module.
Leading call works deterministically for the documented normal, boundary, and repeated-use scenarios.
Trailing call works deterministically for the documented normal, boundary, and repeated-use scenarios.
Latest arguments works deterministically for the documented normal, boundary, and repeated-use scenarios.
Preserves this works deterministically for the documented normal, boundary, and repeated-use scenarios.
Wait boundary works deterministically for the documented normal, boundary, and repeated-use scenarios.
Cancel works deterministically for the documented normal, boundary, and repeated-use scenarios.
Flush works deterministically for the documented normal, boundary, and repeated-use scenarios.
Leading and trailing burst
Call with A, B, and C during one 100 ms window
A runs immediately and C runs at the trailing boundary
The deferred call uses the latest arguments rather than the first suppressed call.
Constraints
Use at most one pending trailing timerPreserve the caller's this valuecancel and flush must leave a reusable wrapper
Hints
Hint 1
Track both the last invocation time and the latest deferred call data.
Hint 2
Treat cancel as a complete reset of timer, arguments, receiver, and timestamps.
Hint 3
Implement flush through the same invocation path used by the timer.