Skip to content

Commit 5a5972c

Browse files
authored
fix: add flag to avoid thread cleanup when updating channel state (#1702)
## CLA - [ ] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required). - [ ] Code changes are tested ## Description of the changes, What, Why and How? https://getstream.zendesk.com/agent/tickets/77916 Bug report: - In RN, sometimes existing messages from the thread are wiped out after the app is moved from the background <-> foreground - The customer says it happens for React, too, but there is no additional information there - What I found (couldn't reproduce, but there is a possible code path): - RN fires a `queryChannels` and `reloadThread` calls independently from each other on state recovery. If `queryChannels` finishes after `reloadThread`, the thread clean up wipes out thread messages. - Since SDKs don't rely on threads being cleaned up (they always call `getReplies` when a thread is opened), we could remove the thread cleanup code. The reason I didn't want to do that because I think it's possible that integrators using client directly may do something like: ``` if (!channel.state.threads[message.id]) { // get replies only if the thread is not initialized } ``` - So I introduced a flag in the client as a somewhat hacky workaround -> since we're planning a state rewrite anyway, I didn't want to spend any more time in this rabbithole - For React: I couldn't reproduce, nor could I see a viable code path for this: as far as I can tell in the React thread reload happens in response to parent message reference changing, which ensures that thread reload can happen only after `queryChannels` finishes. We can ask the customer to open a separate ticket for React, if they still the issue there. ## Changelog -
1 parent fd86795 commit 5a5972c

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/channel_state.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@ export class ChannelState {
243243
.state.updateUserReference(message.user, this._channel.cid);
244244
}
245245

246-
if (initializing && message.id && this.threads[message.id]) {
246+
if (
247+
initializing &&
248+
message.id &&
249+
this.threads[message.id] &&
250+
!this._channel.getClient().preventThreadCleanup
251+
) {
247252
// If we are initializing the state of channel (e.g., in case of connection recovery),
248253
// then in that case we remove thread related to this message from threads object.
249254
// This way we can ensure that we don't have any stale data in thread object

src/client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ export class StreamChat {
321321
* manually calling queryChannels endpoint.
322322
*/
323323
recoverStateOnReconnect?: boolean;
324+
/**
325+
* If true, we will not clean up threads when channel state is in initializing state.
326+
* The main use case for SDKs who do independent state recovery for channels.
327+
*/
328+
preventThreadCleanup = false;
324329
moderation: Moderation;
325330
mutedChannels: ChannelMute[];
326331
mutedUsers: Mute[];

0 commit comments

Comments
 (0)