refactor(handlers): funnel HandlerRegistry's two maps through one insert/evict pair#149
Merged
Merged
Conversation
…ert/evict pair Closes Part I Wave 3 #3 of the arch+DX audit. Every handler lived in BOTH the instance `handlers` dict and the `static globalTable`, hand-synced across four methods (register/remove/closeScope/deinit) — a missed write leaked a global entry (a handler outliving its component) or corrupted scope diagnostics. Both maps are genuinely load-bearing and can't be collapsed: `globalTable` backs production JS dispatch via the single `window.__swiflowDispatch` callback (ID-only, can't know the owning registry), while the instance dict backs `TestRenderer`'s per-instance dispatch (so parallel host tests don't cross-contaminate through a shared static). So instead of the audit's "one authoritative map" option, this funnels ALL mutation through one private `insert(_:scope:)` / `evict(_:)` pair. The two maps (and a handler's scope attribution) now have exactly one add path and one remove path and can never drift; register/remove/closeScope/deinit all got shorter. Behavior-preserving. Adds 4 sync-invariant tests (register → both dispatch paths reach it; remove/closeScope → neither; a released registry drops its handlers from the global table). Verified: host suite 1361/273, counter e2e 11/11 (real-browser dispatch flows through the refactored globalTable). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📦 Bundle size
✅ Within budget (≤5% growth allowed). Baseline: Swift 6.3, WASM SDK 6.3-RELEASE, measured 2026-06-18. |
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
Closes Part I Wave 3 #3 of the arch+DX audit. Every handler lived in both the instance
handlersdict and thestatic globalTable, hand-synced across four methods (register,remove,closeScope,deinit). A missed write in any of them leaked aglobalTableentry (a handler outliving its component) or corrupted scope diagnostics.Why not the audit's "one authoritative map"
Both maps are genuinely load-bearing for different consumers, so they can't be collapsed:
globalTable(static, all registries) backsdispatchGlobal— used by the singlewindow.__swiflowDispatchJS callback, which receives only an integer handler ID and can't know which registry owns it.handlersdict backsdispatch— used byTestRenderer, per-instance so parallel host tests don't cross-contaminate through a shared static.Approach — one mutation funnel
Instead of collapsing the maps, funnel all mutation through one private pair:
register/remove/closeScope/deinitnow route through these, so the two maps (and a handler's scope attribution) can never drift — one add path, one remove path. Every one of the four call sites got shorter.Test plan
register→ both dispatch paths reach the handler;remove/closeScope→ neither does; a released registry (deinit) drops its handlers from the global table (no leak).HandlerRegistrytests pass unchanged — the refactor is behavior-preserving.dispatchGlobal→ the refactoredglobalTable, exercising the production dispatch path end-to-end.🤖 Generated with Claude Code