Skip to content

取得中にプリセット一覧が作り直されると駅データが空になる問題を修正 - #6643

Merged
TinyKitten merged 2 commits into
devfrom
claude/preset-auto-origin-station-5rjpbd
Aug 10, 2026
Merged

TinyKitten merged 2 commits into
devfrom
claude/preset-auto-origin-station-5rjpbd

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

概要

新しく保存したプリセットだけが「プリセットの取得に失敗しました」と表示され、そのまま復帰しなくなる問題を修正します。

PresetCard がこのエラーを出すのは from / to が undefined のときだけで、getPresetRouteEndpoints を辿ると、それが起きるのは stations が空配列のときのみです。つまり終着駅や direction の値ではなく、駅データの取得結果がプリセットに紐づいていないことが原因でした。

usePresetCarouselData にレースコンディションがあります。駅データの取得中に routes が作り直されると(updateRoutes() による DB 再読み込みなど)、fetchKey は変わらないため「取得不要」と判定され、進行中の取得を requestId の更新で打ち切ったうえで、既存の carouselData から駅を引き当てようとします。新規保存したプリセットはまだ carouselData に無いので ?? [] で空配列に確定し、prevFetchKeyRef は更新済みなので再取得も走らず、そのプリセットだけエラー表示のまま固定されます。

既存プリセットの駅クエリは react-query のキャッシュに載っていて即座に解決する一方、新規保存したプリセットの路線は未キャッシュでネットワーク待ちになるため、新規保存分だけがこのレースに負けます。

変更の種類

  • バグ修正
  • 新機能
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • 取得済み駅データを carouselData から引き当てる方式をやめ、路線ID / 種別(lineGroup)ID をキーにした永続キャッシュ(lineStationsCacheRef / trainTypeStationsCacheRef)で保持するよう変更。取得完了前に再描画されても、まだ駅を持たないプリセットが空配列で確定しない
  • 駅データが未取得のプリセットが1件でもあれば取得する判定(hasAllStations)を追加。fetchKey の一致だけで「取得済み」と誤認しないようにした
  • 表示項目だけの更新では requestId を進めないようにし、進行中の取得を打ち切らないよう変更
  • 進行中の取得対象を inFlightFetchKeyRef で保持し、同じ fetchKey の取得を二重発行しないよう変更。hasAllStations は取得完了まで false のままなので、これが無いと routes が作り直されるたびに取得をやり直し、先行リクエストを無効化してしまう
  • 取得完了時の表示組み立てを latestRoutesRef 経由で最新の routes から行うよう変更。effect のクロージャに閉じ込めると、取得中に routes が変わった場合に古い表示内容で確定し、以降の再描画も走らなくなる
  • 各 await の直後・共有キャッシュの更新前に requestId を検証し、追い越された古い応答がキャッシュを汚染しないよう変更
  • 応答に含まれなかったキーも取得済みとしてキャッシュへ記録し、再取得を繰り返さないようにした
  • 未使用になった carouselDataRef を削除
  • ユニットテストを4件追加(取得中の 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

スクリーンショット(任意)

@github-actions github-actions Bot added the react label Aug 10, 2026
@TinyKitten TinyKitten self-assigned this Aug 10, 2026
@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

usePresetCarouselData は駅データを路線ID・種別IDごとの共有キャッシュで管理します。取得中に routes が再生成されても重複取得を抑制し、最新の routes とキャッシュから carouselData を再構築します。古い応答による上書きも防止します。

Changes

プリセットカルーセルの駅データ取得

Layer / File(s) Summary
駅データキャッシュの構造
src/hooks/usePresetCarouselData.ts
最新の routes、進行中の取得キー、路線ID別・種別ID別の駅データを参照に保持します。不足データがある場合に再取得し、同一対象の重複リクエストを抑制します。
取得結果と表示データの再構築
src/hooks/usePresetCarouselData.ts
取得結果を共有キャッシュへ保存します。応答にないIDは空配列として記録します。表示項目だけが変わった場合は取得を中断せず、最新の routes とキャッシュから表示データを再構築します。古い応答によるキャッシュ更新を抑制します。
routes 再生成と応答競合の検証
src/hooks/usePresetCarouselData.test.ts
取得中の routes 再生成、プリセット追加、応答順序の逆転を検証します。駅データの順序と共有キャッシュの内容を確認します。

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 を再構築
Loading

Possibly related PRs

  • TrainLCD/MobileApp#6585: プリセットカルーセルの駅データを利用するウィジェット同期処理と関連します。

Poem

うさぎがキャッシュを整えて
路線と種別を保存する
routes が変わっても駅は消えず
古い応答は追い越せない
新しいプリセットも並んだよ 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed 概要、変更内容、テスト、変更種別を具体的に記載し、必須項目をほぼ満たしています。
Title check ✅ Passed 駅データが空になる主要な不具合と修正内容を簡潔かつ具体的に示しています。
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/preset-auto-origin-station-5rjpbd

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1a035c8 and 36e5a98.

📒 Files selected for processing (2)
  • src/hooks/usePresetCarouselData.test.ts
  • src/hooks/usePresetCarouselData.ts

Comment thread src/hooks/usePresetCarouselData.ts Resolved
Comment thread src/hooks/usePresetCarouselData.ts Resolved
@TinyKitten
TinyKitten merged commit 0a2c9b8 into dev Aug 10, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the claude/preset-auto-origin-station-5rjpbd branch August 10, 2026 09:45
This was referenced Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants