Repository navigation
取得中にプリセット一覧が作り直されると駅データが空になる問題を修正 - #6643
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01PWNLSQntywfT1pGEFs9bvb
Contributor
📝 WalkthroughWalkthrough
Changesプリセットカルーセルの駅データ取得
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Routes
participant usePresetCarouselData
participant StationDataAPI
participant StationDataCache
Routes->>usePresetCarouselData: routes を更新
usePresetCarouselData->>StationDataCache: 不足する駅データを確認
usePresetCarouselData->>StationDataAPI: 不足データを取得
StationDataAPI-->>usePresetCarouselData: 駅データを返却
usePresetCarouselData->>StationDataCache: 最新リクエストの結果を保存
usePresetCarouselData-->>Routes: キャッシュから carouselData を再構築
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Contributor
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/hooks/usePresetCarouselData.ts`:
- Around line 63-89: In src/hooks/usePresetCarouselData.ts lines 63-89, update
the request state around needsFetch and currentRequestIdRef to track the
in-flight fetchKey, avoid advancing requestId or issuing a duplicate request for
the same key, and build the response using the latest routes before updating
carousel data. In src/hooks/usePresetCarouselData.test.ts lines 199-214, verify
mockQuery is called once, resolve only the first request, and assert the
carousel uses the latest display data.
- Around line 106-150: Prevent stale requests from mutating shared caches: in
src/hooks/usePresetCarouselData.ts:106-150, validate requestId immediately after
each await and before updating lineStationsCache or trainTypeStationsCache,
while preserving applyRoutes behavior for the current request. In
src/hooks/usePresetCarouselData.test.ts:220-278, add two delayed requests,
resolve the newer response first and the older response afterward, and verify
cache-derived display data continues to use the newer station data.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2495c265-47cc-4cd4-8135-5ad0cbf7de6e
📒 Files selected for processing (2)
src/hooks/usePresetCarouselData.test.tssrc/hooks/usePresetCarouselData.ts
Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01PWNLSQntywfT1pGEFs9bvb
4 of 9 tasks
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.
概要
新しく保存したプリセットだけが「プリセットの取得に失敗しました」と表示され、そのまま復帰しなくなる問題を修正します。
PresetCardがこのエラーを出すのはfrom/toがundefinedのときだけで、getPresetRouteEndpointsを辿ると、それが起きるのはstationsが空配列のときのみです。つまり終着駅やdirectionの値ではなく、駅データの取得結果がプリセットに紐づいていないことが原因でした。usePresetCarouselDataにレースコンディションがあります。駅データの取得中にroutesが作り直されると(updateRoutes()による DB 再読み込みなど)、fetchKeyは変わらないため「取得不要」と判定され、進行中の取得をrequestIdの更新で打ち切ったうえで、既存のcarouselDataから駅を引き当てようとします。新規保存したプリセットはまだcarouselDataに無いので?? []で空配列に確定し、prevFetchKeyRefは更新済みなので再取得も走らず、そのプリセットだけエラー表示のまま固定されます。既存プリセットの駅クエリは react-query のキャッシュに載っていて即座に解決する一方、新規保存したプリセットの路線は未キャッシュでネットワーク待ちになるため、新規保存分だけがこのレースに負けます。
変更の種類
変更内容
carouselDataから引き当てる方式をやめ、路線ID / 種別(lineGroup)ID をキーにした永続キャッシュ(lineStationsCacheRef/trainTypeStationsCacheRef)で保持するよう変更。取得完了前に再描画されても、まだ駅を持たないプリセットが空配列で確定しないhasAllStations)を追加。fetchKeyの一致だけで「取得済み」と誤認しないようにしたrequestIdを進めないようにし、進行中の取得を打ち切らないよう変更inFlightFetchKeyRefで保持し、同じfetchKeyの取得を二重発行しないよう変更。hasAllStationsは取得完了までfalseのままなので、これが無いとroutesが作り直されるたびに取得をやり直し、先行リクエストを無効化してしまうlatestRoutesRef経由で最新のroutesから行うよう変更。effect のクロージャに閉じ込めると、取得中にroutesが変わった場合に古い表示内容で確定し、以降の再描画も走らなくなるawaitの直後・共有キャッシュの更新前にrequestIdを検証し、追い越された古い応答がキャッシュを汚染しないよう変更carouselDataRefを削除routes再生成、取得中のプリセット追加、重複取得の抑止、古い応答によるキャッシュ汚染の防止)影響範囲・リグレッションリスク
usePresetCarouselDataに閉じており、保存済みプリセットのデータ構造やgetPresetRouteEndpointsの解決ロジックには手を入れていないrequestIdを進めなくなるが、fetchKeyが変わる更新では従来どおり進めるため、古い応答が新しい応答を上書きすることはないテスト
npm run lintが通ることnpm testが通ることnpm run typecheckが通ること先に失敗するテストを書いて再現を確認してから修正しています。追加した4件はいずれも、対応する修正を戻すと落ちることを個別に確認済みです。
npm testは 215 suites / 2277 tests すべて成功。なお、この環境には API の接続情報も実機も無いため、報告された手順(東京メトロ副都心線 地下鉄赤塚駅から新宿三丁目駅を終着に設定)そのものでの再現確認は行えていません。上記のレースは症状と論理的に一致し、テストで再現・修正を確認していますが、修正後も同じエラーが出る場合は別の原因が残ります。その場合に次に疑うべきは、保存された
trainTypeId(lineGroupId)とlineGroupListStationsの応答に含まれるtrainType.groupIdの不一致です。関連Issue
スクリーンショット(任意)