Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not store channelData in thread state #1337

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type QueryRepliesOptions<T extends ExtendableGenerics> = {

export type ThreadState<T extends ExtendableGenerics = DefaultGenerics> = {
active: boolean;

channel: Channel<T>;
createdAt: Date;
deletedAt: Date | null;
isStateStale: boolean;
Expand All @@ -47,12 +47,8 @@ export type ThreadState<T extends ExtendableGenerics = DefaultGenerics> = {
participants: ThreadResponse<T>['thread_participants'];
read: ThreadReadStatus<T>;
replyCount: number;
staggeredRead: ThreadReadStatus<T>;
updatedAt: Date | null;

channel?: Channel<T>;
channelData?: ThreadResponse<T>['channel'];

// messageId as cursor
nextCursor?: string | null;
previousCursor?: string | null;
Expand Down Expand Up @@ -92,12 +88,12 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
private unsubscribeFunctions: Set<() => void> = new Set();
private failedRepliesMap: Map<string, FormatMessageResponse<Scg>> = new Map();

constructor({ client, threadData = {} }: { client: StreamChat<Scg>; threadData?: Partial<ThreadResponse<Scg>> }) {
constructor({ client, threadData }: { client: StreamChat<Scg>; threadData: ThreadResponse<Scg> }) {
const {
read: unformattedRead = [],
latest_replies: latestReplies = [],
thread_participants: threadParticipants = [],
reply_count: replyCount = 0,
latest_replies: latestReplies,
thread_participants: threadParticipants,
reply_count: replyCount,
} = threadData;

const read = transformReadArrayToDictionary(unformattedRead);
Expand All @@ -108,8 +104,7 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
// used as handler helper - actively mark read all of the incoming messages
// if the thread is active (visibly selected in the UI or main focal point in advanced list)
active: false,
channelData: threadData.channel, // not channel instance
channel: threadData.channel && client.channel(threadData.channel.type, threadData.channel.id),
channel: client.channel(threadData.channel.type, threadData.channel.id, threadData.channel),
createdAt: threadData.created_at ? new Date(threadData.created_at) : placeholderDate,
deletedAt: threadData.parent_message?.deleted_at ? new Date(threadData.parent_message.deleted_at) : null,
latestReplies: latestReplies.map(formatMessage),
Expand All @@ -118,8 +113,6 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
participants: threadParticipants,
// actual read state in-sync with BE values
read,
// also read state but staggered - used for UI purposes (unread count banner)
staggeredRead: read,
replyCount,
updatedAt: threadData.updated_at ? new Date(threadData.updated_at) : null,

Expand Down Expand Up @@ -163,7 +156,6 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {

const {
read,
staggeredRead,
replyCount,
latestReplies,
parentMessage,
Expand All @@ -173,7 +165,6 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
updatedAt,
nextCursor,
previousCursor,
channelData,
} = thread.state.getLatestValue();

this.state.next((current) => {
Expand All @@ -182,7 +173,6 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
return {
...current,
read,
staggeredRead,
replyCount,
latestReplies: latestReplies.length ? latestReplies.concat(failedReplies) : latestReplies,
parentMessage,
Expand All @@ -192,7 +182,6 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
updatedAt,
nextCursor,
previousCursor,
channelData,
isStateStale: false,
};
});
Expand Down Expand Up @@ -272,9 +261,9 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {
const currentUserId = this.client.user?.id;
if (!event.channel || !event.user || !currentUserId || currentUserId !== event.user.id) return;

const { channelData } = this.state.getLatestValue();
const { channel } = this.state.getLatestValue();

if (!channelData || event.channel.cid !== channelData.cid) return;
if (event.channel.cid !== channel.cid) return;

this.state.patchedNext('isStateStale', true);
}).unsubscribe,
Expand Down Expand Up @@ -440,8 +429,7 @@ export class Thread<Scg extends ExtendableGenerics = DefaultGenerics> {

// update channel on channelData change (unlikely but handled anyway)
if (message.channel) {
newData['channelData'] = message.channel;
newData['channel'] = this.client.channel(message.channel.type, message.channel.id);
newData['channel'] = this.client.channel(message.channel.type, message.channel.id, message.channel);
}

return newData;
Expand Down
Loading