Skip to content

Commit

Permalink
Make things slightly faster, add extra type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Jan 17, 2025
1 parent 964f008 commit ab96dab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export * from './poll_manager';
export * from './segment';
export * from './signing';
export * from './store';
export * from './thread';
export { Thread } from './thread';
export type { ThreadState, ThreadReadState, ThreadRepliesPagination, ThreadUserReadState } from './thread';
export * from './thread_manager';
export * from './token_manager';
export * from './types';
Expand Down
47 changes: 24 additions & 23 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,33 @@ export type ThreadReadState<SCG extends ExtendableGenerics = DefaultGenerics> =
const DEFAULT_PAGE_LIMIT = 50;
const DEFAULT_SORT: { created_at: AscDesc }[] = [{ created_at: -1 }];
const MARK_AS_READ_THROTTLE_TIMEOUT = 1000;
const THREAD_RESERVED_KEYS = [
'channel',
'channel_cid',
'created_at',
'created_by_user_id',
'parent_message_id',
'title',
'updated_at',
'latest_replies',
'active_participant_count',
'deleted_at',
'last_message_at',
'participant_count',
'reply_count',
'read',
'thread_participants',
'created_by',
'parent_message',
] as const;
// TODO: remove this once we move to API v2
export const THREAD_RESPONSE_RESERVED_KEYS: Record<keyof ThreadResponse, true> = {
channel: true,
channel_cid: true,
created_at: true,
created_by_user_id: true,
parent_message_id: true,
title: true,
updated_at: true,
latest_replies: true,
active_participant_count: true,
deleted_at: true,
last_message_at: true,
participant_count: true,
reply_count: true,
read: true,
thread_participants: true,
created_by: true,
parent_message: true,
};

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

for (const key in threadData) {
if (THREAD_RESERVED_KEYS.includes(key as keyof ThreadResponse<SCG>)) {
if (THREAD_RESPONSE_RESERVED_KEYS[key as keyof ThreadResponse]) {
continue;
}

Expand Down Expand Up @@ -145,7 +146,7 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
replyCount: threadData.reply_count ?? 0,
updatedAt: threadData.updated_at ? new Date(threadData.updated_at) : null,
title: threadData.title,
custom: constructCustomDataObject<SCG>(threadData),
custom: constructCustomDataObject(threadData),
});

this.id = threadData.parent_message_id;
Expand Down Expand Up @@ -253,7 +254,7 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {
updatedAt: new Date(threadData.updated_at),
deletedAt: threadData.deleted_at ? new Date(threadData.deleted_at) : null,
// TODO: use threadData.custom once we move to API v2
custom: constructCustomDataObject<SCG>(threadData),
custom: constructCustomDataObject(threadData),
});
}).unsubscribe;
};
Expand Down
3 changes: 3 additions & 0 deletions test/unit/threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ThreadResponse,
THREAD_MANAGER_INITIAL_STATE,
} from '../../src';
import { THREAD_RESPONSE_RESERVED_KEYS } from '../../src/thread';

const TEST_USER_ID = 'observer';

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

const stateBefore = thread.state.getLatestValue();

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

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

const stateAfter = thread.state.getLatestValue();

expect(stateAfter.custom).to.not.have.keys(Object.keys(THREAD_RESPONSE_RESERVED_KEYS));
expect(stateAfter.custom).to.not.have.property(customKey2);
expect(stateAfter.custom[customKey1]).to.equal(2);
});
Expand Down

0 comments on commit ab96dab

Please sign in to comment.