-
Notifications
You must be signed in to change notification settings - Fork 156
Fix offline transactions for React Native #1137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kevin-dp
wants to merge
5
commits into
main
Choose a base branch
from
ai/issue-1013-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,498
−83
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4826ee
test: assert DefaultOnlineDetector works in React Native environments…
kevin-dp 3f17c31
OnlineDetector for RN
kevin-dp 288bd38
ci: apply automated fixes
autofix-ci[bot] b42ffaf
Changeset
kevin-dp f6203ce
docs: add platform-specific usage instructions for web and React Native
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/offline-transactions': patch | ||
| --- | ||
|
|
||
| Introduce specialized OnlineDetector for React Native |
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
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
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
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
105 changes: 105 additions & 0 deletions
105
packages/offline-transactions/src/connectivity/ReactNativeOnlineDetector.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import NetInfo from '@react-native-community/netinfo' | ||
| import { AppState } from 'react-native' | ||
| import type { AppStateStatus, NativeEventSubscription } from 'react-native' | ||
| import type { OnlineDetector } from '../types' | ||
|
|
||
| /** | ||
| * React Native online detector that uses RN APIs. | ||
| * Listens for: | ||
| * - Network connectivity changes via `@react-native-community/netinfo` | ||
| * - App state changes (foreground/background) via `AppState` | ||
| */ | ||
| export class ReactNativeOnlineDetector implements OnlineDetector { | ||
| private listeners: Set<() => void> = new Set() | ||
| private netInfoUnsubscribe: (() => void) | null = null | ||
| private appStateSubscription: NativeEventSubscription | null = null | ||
| private isListening = false | ||
| private wasConnected = true | ||
|
|
||
| constructor() { | ||
| this.startListening() | ||
| } | ||
|
|
||
| private startListening(): void { | ||
| if (this.isListening) { | ||
| return | ||
| } | ||
|
|
||
| this.isListening = true | ||
|
|
||
| // Subscribe to network state changes | ||
| this.netInfoUnsubscribe = NetInfo.addEventListener((state) => { | ||
| const isConnected = | ||
| state.isConnected === true && state.isInternetReachable !== false | ||
|
|
||
| // Only notify when transitioning to online | ||
| if (isConnected && !this.wasConnected) { | ||
| this.notifyListeners() | ||
| } | ||
|
|
||
| this.wasConnected = isConnected | ||
| }) | ||
|
|
||
| // Subscribe to app state changes (foreground/background) | ||
| this.appStateSubscription = AppState.addEventListener( | ||
| `change`, | ||
| this.handleAppStateChange, | ||
| ) | ||
| } | ||
|
|
||
| private handleAppStateChange = (nextState: AppStateStatus): void => { | ||
| // Notify when app becomes active (foreground) | ||
| if (nextState === `active`) { | ||
| this.notifyListeners() | ||
| } | ||
| } | ||
|
|
||
| private stopListening(): void { | ||
| if (!this.isListening) { | ||
| return | ||
| } | ||
|
|
||
| this.isListening = false | ||
|
|
||
| if (this.netInfoUnsubscribe) { | ||
| this.netInfoUnsubscribe() | ||
| this.netInfoUnsubscribe = null | ||
| } | ||
|
|
||
| if (this.appStateSubscription) { | ||
| this.appStateSubscription.remove() | ||
| this.appStateSubscription = null | ||
| } | ||
| } | ||
|
|
||
| private notifyListeners(): void { | ||
| for (const listener of this.listeners) { | ||
| try { | ||
| listener() | ||
| } catch (error) { | ||
| console.warn(`ReactNativeOnlineDetector listener error:`, error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| subscribe(callback: () => void): () => void { | ||
| this.listeners.add(callback) | ||
|
|
||
| return () => { | ||
| this.listeners.delete(callback) | ||
|
|
||
| if (this.listeners.size === 0) { | ||
| this.stopListening() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| notifyOnline(): void { | ||
| this.notifyListeners() | ||
| } | ||
|
|
||
| dispose(): void { | ||
| this.stopListening() | ||
| this.listeners.clear() | ||
| } | ||
| } |
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
24 changes: 24 additions & 0 deletions
24
packages/offline-transactions/src/react-native/OfflineExecutor.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { OfflineExecutor as BaseOfflineExecutor } from '../OfflineExecutor' | ||
| import { ReactNativeOnlineDetector } from '../connectivity/ReactNativeOnlineDetector' | ||
| import type { OfflineConfig } from '../types' | ||
|
|
||
| /** | ||
| * OfflineExecutor configured for React Native environments. | ||
| * Uses ReactNativeOnlineDetector by default instead of WebOnlineDetector. | ||
| */ | ||
| export class OfflineExecutor extends BaseOfflineExecutor { | ||
| constructor(config: OfflineConfig) { | ||
| super({ | ||
| ...config, | ||
| onlineDetector: config.onlineDetector ?? new ReactNativeOnlineDetector(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Start an offline executor configured for React Native environments. | ||
| * Uses ReactNativeOnlineDetector by default instead of WebOnlineDetector. | ||
| */ | ||
| export function startOfflineExecutor(config: OfflineConfig): OfflineExecutor { | ||
| return new OfflineExecutor(config) | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claude no need to repeat all the same code as we already show for the web example. Just show that the import is different.