Skip to content

Commit 91101a9

Browse files
authored
Feature/messages list subscription (#275)
- Implements a new hook: `useMessagesListSubscriptionOnRn` - Handles real-time message updates via GraphQL subscription (chatRoomOnMessage) - Automatically subscribes/unsubscribes on screen focus using expo useFocusEffect. Ensures clean disposal of the subscription to avoid memory leaks or duplicate events - Uses Relay's ConnectionHandler.getConnectionID and append new messages to the proper connection <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Introduced a new hook for React Native that manages message list subscriptions, ensuring updates are received only while the relevant screen is focused. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 0b0952b commit 91101a9

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

packages/components/CHANGELOG.md

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

3+
## 1.2.6
4+
5+
- Implements a new hook: `useMessagesListSubscriptionOnRn`
6+
- Handles real-time message updates via GraphQL subscription (chatRoomOnMessage)
7+
- Automatically subscribes/unsubscribes on screen focus using expo useFocusEffect. Ensures clean disposal of the subscription to avoid memory leaks or duplicate events
8+
- Uses Relay's ConnectionHandler.getConnectionID and append new messages to the proper connection
9+
310
## 1.2.5
411

512
### Patch Changes
@@ -8,6 +15,7 @@
815
- @baseapp-frontend/design-system@1.0.21
916
- @baseapp-frontend/authentication@5.0.1
1017

18+
1119
## 1.2.4
1220

1321
### Minor Changes

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { useMemo } from 'react'
1+
import { useCallback, useMemo, useRef } from 'react'
22

3-
import { ConnectionHandler, graphql, useSubscription } from 'react-relay'
3+
import { useFocusEffect } from 'expo-router'
4+
import {
5+
ConnectionHandler,
6+
Disposable,
7+
Environment,
8+
graphql,
9+
requestSubscription,
10+
useSubscription,
11+
} from 'react-relay'
412

513
export const newMessageSubscription = graphql`
614
subscription useMessagesListSubscription($roomId: ID!, $profileId: ID!, $connections: [ID!]!) {
@@ -33,3 +41,43 @@ export const useMessagesListSubscription = (roomId: string, profileId: string) =
3341

3442
return useSubscription(config)
3543
}
44+
45+
export const useMessagesListSubscriptionOnRn = (
46+
roomId: string,
47+
profileId: string,
48+
environment: Environment,
49+
) => {
50+
const disposableRef = useRef<Disposable | null>(null)
51+
const connectionID = useMemo(
52+
() => ConnectionHandler.getConnectionID(roomId, 'chatRoom_allMessages'),
53+
[roomId],
54+
)
55+
56+
const config = useMemo(
57+
() => ({
58+
subscription: newMessageSubscription,
59+
variables: {
60+
roomId,
61+
profileId,
62+
connections: [connectionID],
63+
},
64+
onError: console.error,
65+
}),
66+
[roomId, profileId, connectionID],
67+
)
68+
69+
// Subscribe to the messages list subscription when the component is focused
70+
// and clean up the subscription when the component is unfocused
71+
useFocusEffect(
72+
useCallback(() => {
73+
if (!roomId || !profileId) return undefined
74+
75+
disposableRef.current = requestSubscription(environment, config)
76+
77+
return () => {
78+
disposableRef.current?.dispose?.()
79+
disposableRef.current = null
80+
}
81+
}, [config, environment]),
82+
)
83+
}

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.5",
4+
"version": "1.2.6",
55
"sideEffects": false,
66
"scripts": {
77
"babel:transpile": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",

0 commit comments

Comments
 (0)