Skip to content

Commit e68b632

Browse files
refactor: adjust THREAD_RESPONSE_RESERVED_KEYS to be a dictionary instead (#1435)
1 parent 964f008 commit e68b632

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export * from './poll_manager';
1414
export * from './segment';
1515
export * from './signing';
1616
export * from './store';
17-
export * from './thread';
17+
export { Thread } from './thread';
18+
export type { ThreadState, ThreadReadState, ThreadRepliesPagination, ThreadUserReadState } from './thread';
1819
export * from './thread_manager';
1920
export * from './token_manager';
2021
export * from './types';

src/thread.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,33 @@ export type ThreadReadState<SCG extends ExtendableGenerics = DefaultGenerics> =
6868
const DEFAULT_PAGE_LIMIT = 50;
6969
const DEFAULT_SORT: { created_at: AscDesc }[] = [{ created_at: -1 }];
7070
const MARK_AS_READ_THROTTLE_TIMEOUT = 1000;
71-
const THREAD_RESERVED_KEYS = [
72-
'channel',
73-
'channel_cid',
74-
'created_at',
75-
'created_by_user_id',
76-
'parent_message_id',
77-
'title',
78-
'updated_at',
79-
'latest_replies',
80-
'active_participant_count',
81-
'deleted_at',
82-
'last_message_at',
83-
'participant_count',
84-
'reply_count',
85-
'read',
86-
'thread_participants',
87-
'created_by',
88-
'parent_message',
89-
] as const;
71+
// TODO: remove this once we move to API v2
72+
export const THREAD_RESPONSE_RESERVED_KEYS: Record<keyof ThreadResponse, true> = {
73+
channel: true,
74+
channel_cid: true,
75+
created_at: true,
76+
created_by_user_id: true,
77+
parent_message_id: true,
78+
title: true,
79+
updated_at: true,
80+
latest_replies: true,
81+
active_participant_count: true,
82+
deleted_at: true,
83+
last_message_at: true,
84+
participant_count: true,
85+
reply_count: true,
86+
read: true,
87+
thread_participants: true,
88+
created_by: true,
89+
parent_message: true,
90+
};
9091

9192
// TODO: remove this once we move to API v2
92-
const constructCustomDataObject = <SCG extends ExtendableGenerics>(threadData: ThreadResponse<SCG>) => {
93+
const constructCustomDataObject = <T extends ThreadResponse>(threadData: T) => {
9394
const custom: ThreadResponseCustomData = {};
9495

9596
for (const key in threadData) {
96-
if (THREAD_RESERVED_KEYS.includes(key as keyof ThreadResponse<SCG>)) {
97+
if (THREAD_RESPONSE_RESERVED_KEYS[key as keyof ThreadResponse]) {
9798
continue;
9899
}
99100

@@ -145,7 +146,7 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
145146
replyCount: threadData.reply_count ?? 0,
146147
updatedAt: threadData.updated_at ? new Date(threadData.updated_at) : null,
147148
title: threadData.title,
148-
custom: constructCustomDataObject<SCG>(threadData),
149+
custom: constructCustomDataObject(threadData),
149150
});
150151

151152
this.id = threadData.parent_message_id;
@@ -253,7 +254,7 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
253254
updatedAt: new Date(threadData.updated_at),
254255
deletedAt: threadData.deleted_at ? new Date(threadData.deleted_at) : null,
255256
// TODO: use threadData.custom once we move to API v2
256-
custom: constructCustomDataObject<SCG>(threadData),
257+
custom: constructCustomDataObject(threadData),
257258
});
258259
}).unsubscribe;
259260
};

test/unit/threads.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ThreadResponse,
1818
THREAD_MANAGER_INITIAL_STATE,
1919
} from '../../src';
20+
import { THREAD_RESPONSE_RESERVED_KEYS } from '../../src/thread';
2021

2122
const TEST_USER_ID = 'observer';
2223

@@ -605,6 +606,7 @@ describe('Threads 2.0', () => {
605606

606607
const stateBefore = thread.state.getLatestValue();
607608

609+
expect(stateBefore.custom).to.not.have.keys(Object.keys(THREAD_RESPONSE_RESERVED_KEYS));
608610
expect(stateBefore.custom).to.have.keys([customKey1, customKey2]);
609611
expect(stateBefore.custom[customKey1]).to.equal(1);
610612

@@ -617,6 +619,7 @@ describe('Threads 2.0', () => {
617619

618620
const stateAfter = thread.state.getLatestValue();
619621

622+
expect(stateAfter.custom).to.not.have.keys(Object.keys(THREAD_RESPONSE_RESERVED_KEYS));
620623
expect(stateAfter.custom).to.not.have.property(customKey2);
621624
expect(stateAfter.custom[customKey1]).to.equal(2);
622625
});

0 commit comments

Comments
 (0)