Skip to content

Commit d5059a6

Browse files
authored
fix: users can receive/send message after returning to app (#1943)
* fix: connectionChangedHandler is being called even when offline support is disabled * fix: connectionChangedHandler is being called even when offline support is disabled
1 parent e2d34be commit d5059a6

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import { useCreateTypingContext } from './hooks/useCreateTypingContext';
3535
import { useTargetedMessage } from './hooks/useTargetedMessage';
3636

3737
import { ChannelContextValue, ChannelProvider } from '../../contexts/channelContext/ChannelContext';
38-
import { useChannelState } from '../../contexts/channelsStateContext/useChannelState';
3938
import type { UseChannelStateValue } from '../../contexts/channelsStateContext/useChannelState';
39+
import { useChannelState } from '../../contexts/channelsStateContext/useChannelState';
4040
import { ChatContextValue, useChatContext } from '../../contexts/chatContext/ChatContext';
4141
import {
4242
InputMessageInputContextValue,
@@ -771,25 +771,29 @@ const ChannelWithContext = <
771771
const channelSubscriptions: Array<ReturnType<ChannelType['on']>> = [];
772772
const clientSubscriptions: Array<ReturnType<StreamChat['on']>> = [];
773773

774-
const subscribe = () => {
775-
if (!channel) return;
774+
if (!channel) return;
776775

777-
/**
778-
* The more complex sync logic around internet connectivity (NetInfo) is part of Chat.tsx
779-
* listen to client.connection.recovered and all channel events
780-
*/
776+
clientSubscriptions.push(
777+
client.on('channel.deleted', (event) => {
778+
if (event.cid === channel.cid) {
779+
setDeleted(true);
780+
}
781+
}),
782+
);
783+
784+
if (enableOfflineSupport) {
781785
clientSubscriptions.push(DBSyncManager.onSyncStatusChange(connectionChangedHandler));
786+
} else {
782787
clientSubscriptions.push(
783-
client.on('channel.deleted', (event) => {
784-
if (event.cid === channel.cid) {
785-
setDeleted(true);
788+
client.on('connection.changed', (event) => {
789+
if (event.online) {
790+
connectionChangedHandler();
786791
}
787792
}),
788793
);
789-
channelSubscriptions.push(channel.on(handleEvent));
790-
};
794+
}
791795

792-
subscribe();
796+
channelSubscriptions.push(channel.on(handleEvent));
793797

794798
return () => {
795799
clientSubscriptions.forEach((s) => s.unsubscribe());

0 commit comments

Comments
 (0)