[PR #7552] [MERGED] fix(expo): prevent duplicate listener notifications in FocusManager and OnlineManager #15642

Closed
opened 2026-04-13 10:09:09 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7552
Author: @kimchi-developer
Created: 1/22/2026
Status: Merged
Merged: 1/22/2026
Merged by: @himself65

Base: canaryHead: fix/expo-online-focus-manager-duplicate-events


📝 Commits (3)

  • 2dda8bf fix(expo): prevent duplicate listener notifications in FocusManager and OnlineManager
  • d9699fe fix: test
  • 90973c6 test: fix

📊 Changes

3 files changed (+48 additions, -0 deletions)

View changed files

📝 packages/expo/src/focus-manager.ts (+3 -0)
📝 packages/expo/src/online-manager.ts (+1 -0)
📝 packages/expo/test/expo.test.ts (+44 -0)

📄 Description

Problem

ExpoOnlineManager.setOnline() and ExpoFocusManager.setFocused() notify listeners on every call, even when the state value hasn't actually changed.

This causes issues when expo-network fires multiple events with the same isInternetReachable: true value during app initialization. Each notification triggers a session refetch via the session refresh manager, leading to:

  • Unnecessary /get-session API calls
  • Infinite re-render loops in React Native apps
  • Poor performance and potential rate limiting

Root Cause

// Current code - always notifies
setOnline(online: boolean) {
  this.isOnline = online;
  this.listeners.forEach((listener) => listener(online)); // Called even if value unchanged
}

Solution

Add state comparison before notifying listeners:

setOnline(online: boolean) {
  if (this.isOnline === online) return; // Skip if no change
  this.isOnline = online;
  this.listeners.forEach((listener) => listener(online));
}

Same fix applied to ExpoFocusManager.setFocused().

Testing

Tested in a React Native Expo app where the issue was causing continuous session refetches. After the fix, listeners are only notified on actual state changes.


Summary by cubic

Prevent duplicate listener notifications by only notifying on actual state changes in ExpoOnlineManager and ExpoFocusManager. This stops redundant session refetches and render loops in React Native Expo apps.

  • Bug Fixes
    • Added isFocused state and early return in setFocused.
    • Added early return in setOnline.
    • Reduces duplicate expo-network init events and unnecessary /get-session calls.

Written for commit 90973c6502. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7552 **Author:** [@kimchi-developer](https://github.com/kimchi-developer) **Created:** 1/22/2026 **Status:** ✅ Merged **Merged:** 1/22/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/expo-online-focus-manager-duplicate-events` --- ### 📝 Commits (3) - [`2dda8bf`](https://github.com/better-auth/better-auth/commit/2dda8bfc20cda1582e011bbd52fdc4cdc08d1096) fix(expo): prevent duplicate listener notifications in FocusManager and OnlineManager - [`d9699fe`](https://github.com/better-auth/better-auth/commit/d9699fea194d2d196f964f0e3546db2f6f0c0ad5) fix: test - [`90973c6`](https://github.com/better-auth/better-auth/commit/90973c6502e5b3390a627c8f1587b6f97ddc8120) test: fix ### 📊 Changes **3 files changed** (+48 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/expo/src/focus-manager.ts` (+3 -0) 📝 `packages/expo/src/online-manager.ts` (+1 -0) 📝 `packages/expo/test/expo.test.ts` (+44 -0) </details> ### 📄 Description ## Problem `ExpoOnlineManager.setOnline()` and `ExpoFocusManager.setFocused()` notify listeners on every call, even when the state value hasn't actually changed. This causes issues when `expo-network` fires multiple events with the same `isInternetReachable: true` value during app initialization. Each notification triggers a session refetch via the session refresh manager, leading to: - Unnecessary `/get-session` API calls - Infinite re-render loops in React Native apps - Poor performance and potential rate limiting ## Root Cause ```typescript // Current code - always notifies setOnline(online: boolean) { this.isOnline = online; this.listeners.forEach((listener) => listener(online)); // Called even if value unchanged } ``` ## Solution Add state comparison before notifying listeners: ```typescript setOnline(online: boolean) { if (this.isOnline === online) return; // Skip if no change this.isOnline = online; this.listeners.forEach((listener) => listener(online)); } ``` Same fix applied to `ExpoFocusManager.setFocused()`. ## Testing Tested in a React Native Expo app where the issue was causing continuous session refetches. After the fix, listeners are only notified on actual state changes. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevent duplicate listener notifications by only notifying on actual state changes in ExpoOnlineManager and ExpoFocusManager. This stops redundant session refetches and render loops in React Native Expo apps. - **Bug Fixes** - Added isFocused state and early return in setFocused. - Added early return in setOnline. - Reduces duplicate expo-network init events and unnecessary /get-session calls. <sup>Written for commit 90973c6502e5b3390a627c8f1587b6f97ddc8120. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:09:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#15642