Skip to content

Commit

Permalink
fix: change subscribeWithSelector API declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
isekovanic committed Oct 25, 2024
1 parent 71153c8 commit 5f8a45d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
14 changes: 12 additions & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,23 @@ export class StateStore<T extends Record<string, unknown>> {
};
};

public subscribeWithSelector = <O extends readonly unknown[]>(selector: (nextValue: T) => O, handler: Handler<O>) => {
public subscribeWithSelector = <O extends Readonly<Record<string, unknown>> & { length?: never }>(
selector: (nextValue: T) => O,
handler: Handler<O>,
) => {
// begin with undefined to reduce amount of selector calls
let selectedValues: O | undefined;

const wrappedHandler: Handler<T> = (nextValue) => {
const newlySelectedValues = selector(nextValue);
const hasUpdatedValues = selectedValues?.some((value, index) => value !== newlySelectedValues[index]) ?? true;

let hasUpdatedValues = !selectedValues;

for (const key in selectedValues) {
if (selectedValues[key] === newlySelectedValues[key]) continue;
hasUpdatedValues = true;
break;
}

if (!hasUpdatedValues) return;

Expand Down
11 changes: 7 additions & 4 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {

private subscribeMarkActiveThreadRead = () => {
return this.state.subscribeWithSelector(
(nextValue) => [nextValue.active, ownUnreadCountSelector(this.client.userID)(nextValue)],
([active, unreadMessageCount]) => {
(nextValue) => ({
active: nextValue.active,
unreadMessageCount: ownUnreadCountSelector(this.client.userID)(nextValue),
}),
({ active, unreadMessageCount }) => {
if (!active || !unreadMessageCount) return;
this.throttledMarkAsRead();
},
Expand All @@ -207,8 +210,8 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {

private subscribeReloadActiveStaleThread = () =>
this.state.subscribeWithSelector(
(nextValue) => [nextValue.active, nextValue.isStateStale],
([active, isStateStale]) => {
(nextValue) => ({ active: nextValue.active, isStateStale: nextValue.isStateStale }),
({ active, isStateStale }) => {
if (active && isStateStale) {
this.reload();
}
Expand Down
10 changes: 5 additions & 5 deletions src/thread_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ export class ThreadManager<SCG extends ExtendableGenerics = DefaultGenerics> {

private subscribeManageThreadSubscriptions = () =>
this.state.subscribeWithSelector(
(nextValue) => [nextValue.threads] as const,
([nextThreads], prev) => {
const [prevThreads = []] = prev ?? [];
(nextValue) => ({ threads: nextValue.threads } as const),
({ threads: nextThreads }, prev) => {
const { threads: prevThreads = [] } = prev ?? {};
// Thread instance was removed if there's no thread with the given id at all,
// or it was replaced with a new instance
const removedThreads = prevThreads.filter((thread) => thread !== this.threadsById[thread.id]);
Expand All @@ -133,8 +133,8 @@ export class ThreadManager<SCG extends ExtendableGenerics = DefaultGenerics> {

private subscribeReloadOnActivation = () =>
this.state.subscribeWithSelector(
(nextValue) => [nextValue.active],
([active]) => {
(nextValue) => ({ active: nextValue.active }),
({ active }) => {
if (active) this.reload();
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,10 @@ export type CreateChannelOptions<StreamChatGenerics extends ExtendableGenerics =
reminders?: boolean;
replies?: boolean;
search?: boolean;
skip_last_msg_update_for_system_msgs?: boolean;
typing_events?: boolean;
uploads?: boolean;
url_enrichment?: boolean;
skip_last_msg_update_for_system_msgs?: boolean;
};

export type CreateCommandOptions<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
Expand Down
9 changes: 6 additions & 3 deletions test/unit/threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,14 +1322,17 @@ describe('Threads 2.0', () => {
},
}));
const spy = sinon.spy();
threadManager.state.subscribeWithSelector((nextValue) => [nextValue.pagination.isLoadingNext], spy);
threadManager.state.subscribeWithSelector(
(nextValue) => ({ isLoadingNext: nextValue.pagination.isLoadingNext }),
spy,
);
spy.resetHistory();

await threadManager.loadNextPage();

expect(spy.callCount).to.equal(2);
expect(spy.firstCall.calledWith([true])).to.be.true;
expect(spy.lastCall.calledWith([false])).to.be.true;
expect(spy.firstCall.calledWith({ isLoadingNext: true })).to.be.true;
expect(spy.lastCall.calledWith({ isLoadingNext: false })).to.be.true;
});

it('updates thread list and pagination', async () => {
Expand Down

0 comments on commit 5f8a45d

Please sign in to comment.