Skip to content

Commit f4fa5a6

Browse files
committed
perf: skip reacting to own ws events if optimistic update happened
1 parent a0e28a1 commit f4fa5a6

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ const ChannelWithContext = <
716716
* Its a map of filename to AbortController
717717
*/
718718
const uploadAbortControllerRef = useRef<Map<string, AbortController>>(new Map());
719+
const optimisticallyUpdatedNewMessages = useRef<Set<string>>(new Set());
719720

720721
const channelId = channel?.id || '';
721722
const pollCreationEnabled = !channel.disconnected && !!channel?.id && channel?.getConfig()?.polls;
@@ -837,9 +838,18 @@ const ChannelWithContext = <
837838
}
838839

839840
// only update channel state if the events are not the previously subscribed useEffect's subscription events
841+
console.log('EVENT: ', event);
840842
if (channel && channel.initialized) {
841-
if (event.type === 'message.new') {
842-
copyMessagesStateFromChannelThrottled();
843+
if (event.type === 'message.new' || event.type === 'notification.message_new') {
844+
const messageId = event.message?.id ?? '';
845+
if (
846+
event.user?.id !== client.userID ||
847+
!optimisticallyUpdatedNewMessages.current.has(messageId)
848+
) {
849+
console.log('INSIDE', Array.from(optimisticallyUpdatedNewMessages.current));
850+
copyMessagesStateFromChannelThrottled();
851+
}
852+
optimisticallyUpdatedNewMessages.current.delete(messageId);
843853
return;
844854
}
845855

@@ -848,6 +858,7 @@ const ChannelWithContext = <
848858
return;
849859
}
850860

861+
console.log('FULL STATE UPDATE');
851862
copyChannelState();
852863
}
853864
}
@@ -1180,13 +1191,17 @@ const ChannelWithContext = <
11801191
* MESSAGE METHODS
11811192
*/
11821193
const updateMessage: MessagesContextValue<StreamChatGenerics>['updateMessage'] =
1183-
useStableCallback((updatedMessage, extraState = {}) => {
1194+
useStableCallback((updatedMessage, extraState = {}, throttled = false) => {
11841195
if (!channel) {
11851196
return;
11861197
}
11871198

11881199
channel.state.addMessageSorted(updatedMessage, true);
1189-
copyMessagesStateFromChannelThrottled();
1200+
if (throttled) {
1201+
copyMessagesStateFromChannelThrottled();
1202+
} else {
1203+
copyMessagesStateFromChannel(channel);
1204+
}
11901205

11911206
if (thread && updatedMessage.parent_id) {
11921207
extraState.threadMessages = channel.state.threads[updatedMessage.parent_id] || [];
@@ -1401,7 +1416,7 @@ const ChannelWithContext = <
14011416
if (retrying) {
14021417
replaceMessage(message, messageResponse.message);
14031418
} else {
1404-
updateMessage(messageResponse.message);
1419+
updateMessage(messageResponse.message, {}, true);
14051420
}
14061421
}
14071422
} catch (err) {
@@ -1436,6 +1451,7 @@ const ChannelWithContext = <
14361451
messageInput: '',
14371452
});
14381453
threadInstance?.upsertReplyLocally?.({ message: messagePreview });
1454+
optimisticallyUpdatedNewMessages.current.add(messagePreview.id);
14391455

14401456
if (enableOfflineSupport) {
14411457
// While sending a message, we add the message to local db with failed status, so that

package/src/contexts/messagesContext/MessagesContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ export type MessagesContextValue<
322322
messageInput?: string;
323323
threadMessages?: ChannelState<StreamChatGenerics>['threads'][string];
324324
},
325+
throttled?: boolean,
325326
) => void;
326327
/**
327328
* Custom UI component to display enriched url preview.

0 commit comments

Comments
 (0)