Skip to content

Commit d994082

Browse files
authored
Feature/react native subscriptions improvement (#279)
This PR introduces two key changes to improve the reliability of GraphQL subscriptions when the app transitions between background and foreground states: 1. New Hook: `useAppStateSubscription` Added a reusable hook that listens to app state changes using React Native’s AppState API. Executes a provided callback (onAppResume) when the app returns to the foreground (e.g., after being backgrounded or locked). Useful for re-subscribing to real-time data or refetching stale content. Example: ```js AppStateSubscription(() => { refetch({ id: chatRoom.id }, { fetchPolicy: 'store-and-network' }) }) ``` 2. Improved `useMessagesListSubscription` Hook Enhanced the subscription lifecycle by subscribing and unsubscribing based on the app's active/inactive state. Ensures real-time updates are re-established automatically after app resumes from the background or device unlock. Prevents missed events or stale subscriptions due to suspended WebSocket connections on iOS. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Introduced a new hook to detect when the app returns to the foreground. * **Improvements** * Enhanced message subscription logic to better handle app state transitions, providing more reliable updates when the app becomes active or inactive. * **Chores** * Updated several internal dependencies across packages to their latest versions. * Incremented package versions and updated changelogs for improved tracking. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent ba018f5 commit d994082

File tree

16 files changed

+109
-21
lines changed

16 files changed

+109
-21
lines changed

packages/authentication/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @baseapp-frontend/authentication
22

3+
## 5.0.2
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- @baseapp-frontend/utils@4.0.1
9+
310
## 5.0.1
411

512
### Patch Changes

packages/authentication/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/authentication",
33
"description": "Authentication modules.",
4-
"version": "5.0.1",
4+
"version": "5.0.2",
55
"main": "./index.ts",
66
"types": "dist/index.d.ts",
77
"sideEffects": false,

packages/components/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# @baseapp-frontend/components
22

3+
## 1.2.8
4+
5+
### Minor Changes
6+
7+
- Improved the `useMessagesListSubscription` hook to subscribe and unsubscribe based on the application's state — e.g., when the app goes inactive and then returns to the foreground, or vice versa.
8+
9+
### Patch Changes
10+
11+
- Updated dependencies
12+
- @baseapp-frontend/utils@4.0.1
13+
- @baseapp-frontend/authentication@5.0.2
14+
- @baseapp-frontend/design-system@1.0.21
15+
- @baseapp-frontend/graphql@1.3.3
16+
317
## 1.2.7
418

519
### Minor Changes

packages/components/modules/messages/native/graphql/subscriptions/useMessagesListSubscription.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useCallback, useMemo, useRef } from 'react'
22

3+
import { useAppStateSubscription } from '@baseapp-frontend/utils/hooks/useAppStateSubscription'
4+
35
import { useFocusEffect } from 'expo-router'
46
import { ConnectionHandler, Disposable, Environment, requestSubscription } from 'react-relay'
57

@@ -11,6 +13,7 @@ export const useMessagesListSubscription = (
1113
environment: Environment,
1214
) => {
1315
const disposableRef = useRef<Disposable | null>(null)
16+
1417
const connectionID = useMemo(
1518
() => ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages'),
1619
[roomId],
@@ -29,18 +32,27 @@ export const useMessagesListSubscription = (
2932
[roomId, profileId, connectionID],
3033
)
3134

32-
// Subscribe to the messages list subscription when the component is focused
33-
// and clean up the subscription when the component is unfocused
34-
useFocusEffect(
35-
useCallback(() => {
36-
if (!roomId || !profileId) return undefined
35+
const subscribe = useCallback(() => {
36+
if (!roomId || !profileId) return
37+
disposableRef.current?.dispose?.()
38+
disposableRef.current = requestSubscription(environment, config)
39+
}, [roomId, profileId, environment, config])
3740

38-
disposableRef.current = requestSubscription(environment, config)
41+
const unsubscribe = useCallback(() => {
42+
disposableRef.current?.dispose?.()
43+
disposableRef.current = null
44+
}, [])
3945

46+
useFocusEffect(
47+
useCallback(() => {
48+
subscribe()
4049
return () => {
41-
disposableRef.current?.dispose?.()
42-
disposableRef.current = null
50+
unsubscribe()
4351
}
44-
}, [config, environment]),
52+
}, [subscribe, unsubscribe]),
4553
)
54+
55+
useAppStateSubscription(() => {
56+
subscribe()
57+
})
4658
}

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/components",
33
"description": "BaseApp components modules such as comments, notifications, messages, and more.",
4-
"version": "1.2.7",
4+
"version": "1.2.8",
55
"sideEffects": false,
66
"scripts": {
77
"babel:transpile": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",

packages/design-system/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @baseapp-frontend/design-system
22

3+
## 1.0.21
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- @baseapp-frontend/utils@4.0.1
9+
310
## 1.0.20
411

512
### Patch Changes

packages/design-system/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/design-system",
33
"description": "Design System components and configurations.",
4-
"version": "1.0.20",
4+
"version": "1.0.21",
55
"sideEffects": false,
66
"scripts": {
77
"tsup:bundle": "tsup --tsconfig tsconfig.build.json",

packages/graphql/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @baseapp-frontend/graphql
22

3+
## 1.3.3
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- @baseapp-frontend/utils@4.0.1
9+
- @baseapp-frontend/authentication@5.0.2
10+
311
## 1.3.2
412

513
### Patch Changes

packages/graphql/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/graphql",
33
"description": "GraphQL configurations and utilities",
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"main": "./index.ts",
66
"types": "dist/index.d.ts",
77
"sideEffects": false,

packages/provider/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @baseapp-frontend/provider
22

3+
## 2.0.15
4+
5+
### Patch Changes
6+
7+
- Updated dependencies
8+
- @baseapp-frontend/utils@4.0.1
9+
310
## 2.0.14
411

512
### Patch Changes

0 commit comments

Comments
 (0)