refactor(focus): split propagateKeyPress into phase helpers#19
Merged
Conversation
The function had grown to 135 lines with two near-identical 9-line throttle
checks (once in the capture loop, once in the bubble loop) and two distinct
sub-algorithms inlined. Pull them apart:
- `isElementThrottled(elm, sameKey, currentTime)` — single source of truth
for per-element rate-limiting; previously copy-pasted across the two loops.
- `runCapturePhase(...)` — root→leaf walk, returns whether to stop.
- `runBubblePhase(...)` — leaf→root walk, returns `{ handled, lastHandlerSeen }`
so the debug log keeps its "which element had a handler but didn't claim"
output without a function-scope mutable.
`propagateKeyPress` is now a flat orchestrator: global throttle → history
record → capture → bubble → debug log. Behavior is unchanged; the LoC goes
up slightly (helpers carry signatures and one short doc comment each) but
the orchestrator body drops from ~135 to ~50 lines.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
propagateKeyPresshad grown to 135 lines with two near-identical throttle checks (one per loop) and two distinct sub-algorithms inlined. Pull them apart:isElementThrottled(elm, sameKey, currentTime)— single source of truth for per-element rate-limiting. Previously copy-pasted across the capture and bubble loops as a 9-lineif (elm.throttleInput) { if (sameKey && ...) return true; }block.runCapturePhase(...)— root→leaf walk, returns whether propagation should stop.runBubblePhase(...)— leaf→root walk, returns{ handled, lastHandlerSeen }so the debug log keeps its "which element had a handler but didn't claim" output without a function-scope mutable.propagateKeyPressis now a flat orchestrator: global throttle → history record → capture → bubble → debug log. The orchestrator body drops from ~135 lines to ~50.Behavior
Unchanged. Same handler-firing order, same throttle semantics, same debug-log messages (just with the message body interpolated once instead of twice). All 120 tests pass.
Test plan
npm run tsc— cleannpm test— 120/120 passnpm run lint— 0 errors🤖 Generated with Claude Code