refactor(core): consolidate mirrored page navigation resolvers - #256
Open
mbret wants to merge 2 commits into
Open
refactor(core): consolidate mirrored page navigation resolvers#256mbret wants to merge 2 commits into
mbret wants to merge 2 commits into
Conversation
The left/top and right/bottom page navigation resolvers were three pairs
of mirror-image implementations differing only by the sign of their
position deltas. Unify each pair into a single direction-parametrized
function:
- getSpineItemPositionForLeftPage + ...ForRightPage
-> getSpineItemPositionForPage
- getNavigationForLeftSinglePage + getNavigationForRightOrBottomSinglePage
-> getNavigationForSinglePage
- getNavigationForLeftOrTopPage + getNavigationForRightOrBottomPage
-> getNavigationForPage
A shared PageNavigationDirection ("leftOrTop" | "rightOrBottom") maps to a
+1/-1 sign applied to the page-size deltas, so behavior is identical to the
previous per-direction code. ManualNavigator now passes the direction
instead of selecting a function.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WhdPtBDGt3oDdZchEw8xEv
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
The consolidated resolvers derive both turn directions from a single implementation by flipping the sign of their page-size deltas, but the existing tests only reached the scrollable vertical early return (21% statement / 16% branch coverage of the resolvers). An inverted delta would have turned pages the wrong way with a green suite. Add a table driven test covering every branch the resolver can take: horizontal controlled, out of spine turns, vertical writing in spread for both reading directions, vertical turns in spread and scrollable vertical. The rtl cases are the important ones, since the extra spread page width is the only place readingDirection changes the outcome. Coverage of the resolvers goes to 95% statement / 94% branch, and each individual sign in the implementation is now load bearing for a test. Also reword the getNavigationForSinglePage note, which described the right/bottom page only and no longer matched a function serving both directions. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WhdPtBDGt3oDdZchEw8xEv
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
The page-turn navigation resolvers in
packages/core/src/enhancers/navigation/resolvers/were three pairs of mirror-image implementations — one forleftOrTop, one forrightOrBottom— that differed only by the sign of their position deltas. Every branch, guard, and call was structurally identical; only+/-on page-size arithmetic changed. This PR unifies each pair into a single direction-parametrized function.The direction is a genuine axis of one concept (turn a page one way vs. the other), not a flag papering over divergent behavior: it collapses to a
+1 / -1sign applied to the deltas. The control-flow graph is identical for both directions, and the change adds no new conditionals — the one pre-existing RTL ternary got simpler (two object literals became one).Consolidation
Introduced
pageNavigationDirection.tsexportingPageNavigationDirection("leftOrTop" | "rightOrBottom") andgetPageNavigationDirectionSign(-1forleftOrTop,+1forrightOrBottom).getSpineItemPositionForLeftPage.ts+getSpineItemPositionForRightPage.tsgetSpineItemPositionForPage.tsgetNavigationForLeftSinglePage.ts+getNavigationForRightOrBottomSinglePage.tsgetNavigationForSinglePage.tsgetNavigationForLeftOrTopPage.ts+getNavigationForRightOrBottomPage.tsgetNavigationForPage.tsWhy they are the same concept: the per-pair diffs (normalized for names/comments) were only sign flips on position deltas:
x ± pageWidth, vertical-writingy ∓ pageHeightx/y ± pageSizey ± pageHeight, and the RTL spread branch (which swaps the sign for RTL — preserved as(isRTL ? -sign : sign))Each sign is reproduced exactly via
getPageNavigationDirectionSign(direction).Call sites
manualNavigator.ts—turnWithnow takes aPageNavigationDirectionand passes it togetNavigationForPage, instead of receiving one of two functions.getNavigationForRightOrBottomPage.test.ts→ renamed togetNavigationForPage.test.ts; its two cases now passdirection. Assertions unchanged.These functions are internal to
packages/core(not re-exported from anyindex.ts), so there is no public-API or documentation change (gitbook/has no references — checked).LOC
Verification
Because a sign inversion here would silently turn pages the wrong way, equivalence was proven rather than assumed:
SpinePosition/UnboundSpinePosition, for both directions at all three layers. Compared withObject.isso a+0/−0divergence fromsign * deltawould fail rather than compare equal. All identical.Test coverage gap found and fixed
The pre-existing tests covered only the scrollable-vertical early return — 21% statement / 16% branch of these resolvers. The RTL spread branch, both deeper layers, and every other sign site had no unit coverage, so an inverted delta would have shipped with a green suite.
getNavigationForPage.directions.test.tsadds a table-driven test over every branch the resolver can take (horizontal controlled, out-of-spine turns, vertical-writing spread in both reading directions, vertical turns in spread, scrollable vertical). Coverage of the resolvers is now 94.7% statement / 93.75% branch / 100% functions, and all 7 sign mutations (including invertinggetPageNavigationDirectionSignitself) are caught by it.The
rtlcases matter most: the extra spread page width is the only placereadingDirectionchanges the outcome, and it was previously untested.Also reworded the
getNavigationForSinglePage@importantnote, which described the right/bottom page only and no longer read correctly on a function serving both directions.Gates
Toolchain pinned via
.nvmrc(Node 25). Baseline recorded on a clean checkout before editing; green before and after:npm run format/npm run lint— cleannpm run build— 17 projectsnpm run tsc— 6 projectspackages/coresuite — 194 tests passOther opportunities noted (not in this PR)
Left for future runs to keep this PR one coherent story:
playback/AudioController.test.ts,enhancers/pagination/trackPaginationInfo.test.ts, andgenerators/manifest/characterization.test.ts(intra-file duplication).🤖 Generated with Claude Code
https://claude.ai/code/session_01WhdPtBDGt3oDdZchEw8xEv