Skip to content

ExpoKeepAwakeのActivity破棄後のdeactivate例外と2回目の有効化無視を解消 - #5979

Merged
TinyKitten merged 2 commits into
devfrom
claude/fix-expo-keepawake-error-kdGUD
May 13, 2026
Merged

TinyKitten merged 2 commits into
devfrom
claude/fix-expo-keepawake-error-kdGUD

Conversation

@TinyKitten

@TinyKitten TinyKitten commented May 13, 2026 •

Copy link
Copy Markdown
Member

概要

Sentry に報告されていた ExpoKeepAwake.deactivate の The current activity is no longer available 例外と、路線選択を繰り返すと画面スリープが防止されなくなる不具合(同じネイティブバグの 2 つの症状)を解消する。

変更の種類

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

変更内容

expo-keep-awake の Android ネイティブ実装 ExpoKeepAwakeManager.deactivate は currentActivity 取得時に例外が出ると tags Set からタグが除去されないまま中断する。その結果、

  • (1) JS 側の useKeepAwake cleanup が unhandled rejection を投げて Sentry に報告される
  • (2) 次回 activate が if (!isActivated) で短絡し、新しい Activity の window に FLAG_KEEP_SCREEN_ON が再付与されず画面がスリープしてしまう

という同根の不具合が発生していた。前回の suppressDeactivateWarnings 指定は (1) を黙らせるだけで (2) の状態破損は残ったままだったため、Main 画面の useKeepAwake を自前ラッパーに置き換える。

  • src/hooks/useKeepAwake.ts を新規追加。固定タグ TrainLCDMainKeepAwake を使い、マウント時と AppState の active 復帰時に deactivateKeepAwake → activateKeepAwakeAsync を順に呼んでネイティブ側 tags Set を強制的にクリアし、新しい window にフラグを付け直す。cleanup 時の deactivate 例外も .catch(() => {}) で握りつぶす。
  • src/screens/Main.tsx: expo-keep-awake の useKeepAwake 直接呼び出しを ~/hooks の自前 useKeepAwake に置き換え(suppressDeactivateWarnings 指定は自前フックに内包されたので除去)。
  • src/hooks/index.ts: 自前フックの再エクスポートを追加。
  • src/hooks/useKeepAwake.test.tsx: マウント時の deactivate→activate の順序、AppState active 復帰時の再実行、background 時の非実行、unmount 時のリスナー解除と deactivate、deactivate 失敗時の例外握りつぶしを検証。

テスト

  • npm run lint が通ること
  • npm test が通ること(新規 5 ケース含む全 1546 ケース成功)
  • npm run typecheck が通ること

関連Issue

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


Generated by Claude Code

Summary by CodeRabbit

リリースノート

  • 新機能

    • デバイスの画面をアクティブに保つ機能を再利用可能なフックとして公開しました。
  • テスト

    • 画面キープアクティブ機能の包括的なテストスイートを追加しました。
  • リファクタリング

    • 画面キープアクティブ機能の実装を改善し、アプリの状態遷移に適切に対応するようにしました。

Review Change Stack

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

coderabbitai Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

概要

新しいuseKeepAwakeフックを実装し、デバイス画面の消灯を防止します。このフックはマウント時に非アクティブ化→アクティブ化シーケンスを実行し、アプリが'active'状態に復帰する際に同じシーケンスを再実行し、包括的なテストで動作を検証してから既存コンポーネントに統合しました。

変更内容

useKeepAwakeフック実装と統合

レイヤー / ファイル 概要
useKeepAwakeフック実装
src/hooks/useKeepAwake.ts
KEEP_AWAKE_TAG定数、マウント時の非アクティブ化→アクティブ化シーケンス実装、AppState 'change'リスナーによる'active'状態復帰時の再実行、アンマウント時のリスナー削除と非アクティブ化を含むフック実装。
useKeepAwakeテストスイート
src/hooks/useKeepAwake.test.tsx
expo-keep-awakeとAppState.addEventListenerのモック設定、マウント時の動作、'active'状態への遷移、'background'状態への遷移、アンマウント時の動作、エラーハンドリングをカバーする5つのテストケース。
フックの再エクスポートと統合
src/hooks/index.ts, src/screens/Main.tsx
useKeepAwakeを~/hooksから再エクスポート、Mainコンポーネントのインポート元をexpo-keep-awakeから~/hooksに変更。

シーケンス図

sequenceDiagram
  participant Component
  participant useKeepAwake
  participant expoKeepAwake as expo-keep-awake
  participant AppState
  Component->>useKeepAwake: mount
  useKeepAwake->>expoKeepAwake: deactivateKeepAwake(tag)
  note over expoKeepAwake: errors ignored
  useKeepAwake->>expoKeepAwake: activateKeepAwakeAsync(tag)
  useKeepAwake->>AppState: addEventListener('change')
  AppState->>useKeepAwake: 'active' state change
  useKeepAwake->>expoKeepAwake: deactivateKeepAwake(tag)
  useKeepAwake->>expoKeepAwake: activateKeepAwakeAsync(tag)
  Component->>useKeepAwake: unmount
  useKeepAwake->>AppState: removeEventListener()
  useKeepAwake->>expoKeepAwake: deactivateKeepAwake(tag)
Loading

推定コードレビュー工数

🎯 3 (中程度) | ⏱️ ~25 分

詩

ウサギのしっぽはピンク色で
keep-awakeでスクリーン輝く
AppStateの声を聞いて
デバイスは起きたままだよ
テストも完璧、バグもなし🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed タイトルは、Android ネイティブの deactivate 例外と2回目の有効化無視を解消する点を簡潔に表現しており、実装内容(useKeepAwake の自前ラッパー化)と一致している。
Description check ✅ Passed PR 説明はテンプレートの全セクション(概要、変更の種類、変更内容、テスト、関連 Issue)を網羅し、ネイティブバグの詳細、対応内容、テスト実施結果が明確に記載されている。
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/fix-expo-keepawake-error-kdGUD

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

@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.

🧹 Nitpick comments (1)
src/hooks/useKeepAwake.test.tsx (1)

29-31: ⚡ Quick win

jest.clearAllMocks() を afterEach に移してください。

Line 29-31 は現状 beforeEach ですが、このリポジトリのテスト規約に合わせて afterEach へ統一してください。

🔧 提案差分
 describe('useKeepAwake', () => {
-  beforeEach(() => {
+  afterEach(() => {
     jest.clearAllMocks();
   });

As per coding guidelines "Call jest.clearAllMocks() in afterEach blocks".

🤖 Prompt for 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.

In `@src/hooks/useKeepAwake.test.tsx` around lines 29 - 31, The test setup
currently calls jest.clearAllMocks() inside the beforeEach block; move that call
into an afterEach block instead to conform to the repository test guideline.
Locate the beforeEach in useKeepAwake.test.tsx, remove jest.clearAllMocks() from
that beforeEach, and add an afterEach(() => { jest.clearAllMocks(); }) so mocks
are cleared after each test case.
🤖 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.

Nitpick comments:
In `@src/hooks/useKeepAwake.test.tsx`:
- Around line 29-31: The test setup currently calls jest.clearAllMocks() inside
the beforeEach block; move that call into an afterEach block instead to conform
to the repository test guideline. Locate the beforeEach in
useKeepAwake.test.tsx, remove jest.clearAllMocks() from that beforeEach, and add
an afterEach(() => { jest.clearAllMocks(); }) so mocks are cleared after each
test case.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 116d35be-56f5-42d4-b8e9-cbdc26434b15

📥 Commits

Reviewing files that changed from the base of the PR and between 627d505 and 53111fa.

📒 Files selected for processing (4)
  • src/hooks/index.ts
  • src/hooks/useKeepAwake.test.tsx
  • src/hooks/useKeepAwake.ts
  • src/screens/Main.tsx

@TinyKitten
TinyKitten merged commit ae24580 into dev May 13, 2026
6 checks passed
@TinyKitten
TinyKitten deleted the claude/fix-expo-keepawake-error-kdGUD branch May 13, 2026 02:59
@TinyKitten TinyKitten mentioned this pull request May 13, 2026
4 of 9 tasks
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