Skip to content

Commit f7c01d4

Browse files
committedNov 14, 2024
refactor: remove stale subscriber count logic and types refactor
1 parent 3efe254 commit f7c01d4

File tree

4 files changed

+15
-96
lines changed

4 files changed

+15
-96
lines changed
 

‎package/src/components/MessageList/hooks/useMessageList.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { ChannelState, MessageResponse } from 'stream-chat';
22

3-
import {
4-
ChannelContextValue,
5-
useChannelContext,
6-
} from '../../../contexts/channelContext/ChannelContext';
3+
import { useChannelContext } from '../../../contexts/channelContext/ChannelContext';
74
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
85
import {
96
DeletedMessagesVisibilityType,
@@ -61,7 +58,7 @@ export const useMessageList = <
6158
const { threadMessages } = useThreadContext<StreamChatGenerics>();
6259

6360
const messageList = threadList ? threadMessages : messages;
64-
const readList: ChannelContextValue<StreamChatGenerics>['read'] | undefined = threadList
61+
const readList: ChannelState<StreamChatGenerics>['read'] | undefined = threadList
6562
? undefined
6663
: read;
6764

‎package/src/components/MessageList/utils/getReadStates.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext';
1+
import { ChannelState } from 'stream-chat';
22
import type { PaginatedMessageListContextValue } from '../../../contexts/paginatedMessageListContext/PaginatedMessageListContext';
33
import type { ThreadContextValue } from '../../../contexts/threadContext/ThreadContext';
44
import type { DefaultStreamChatGenerics } from '../../../types/types';
@@ -10,7 +10,7 @@ export const getReadStates = <
1010
messages:
1111
| PaginatedMessageListContextValue<StreamChatGenerics>['messages']
1212
| ThreadContextValue<StreamChatGenerics>['threadMessages'],
13-
read?: ChannelContextValue<StreamChatGenerics>['read'],
13+
read?: ChannelState<StreamChatGenerics>['read'],
1414
) => {
1515
const readData: Record<string, number> = {};
1616

‎package/src/contexts/channelsStateContext/ChannelsStateContext.tsx

+6-63
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import React, {
77
useReducer,
88
useRef,
99
} from 'react';
10+
import { ChannelState as StreamChannelState } from 'stream-chat';
1011

1112
import type { DefaultStreamChatGenerics } from '../../types/types';
1213
import { ActiveChannelsProvider } from '../activeChannelsRefContext/ActiveChannelsRefContext';
1314

14-
import type { ChannelContextValue } from '../channelContext/ChannelContext';
1515
import type { PaginatedMessageListContextValue } from '../paginatedMessageListContext/PaginatedMessageListContext';
1616
import type { ThreadContextValue } from '../threadContext/ThreadContext';
1717
import type { TypingContextValue } from '../typingContext/TypingContext';
@@ -22,14 +22,13 @@ import { isTestEnvironment } from '../utils/isTestEnvironment';
2222
export type ChannelState<
2323
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2424
> = {
25-
members: ChannelContextValue<StreamChatGenerics>['members'];
25+
members: StreamChannelState<StreamChatGenerics>['members'];
2626
messages: PaginatedMessageListContextValue<StreamChatGenerics>['messages'];
27-
read: ChannelContextValue<StreamChatGenerics>['read'];
28-
subscriberCount: number;
27+
read: StreamChannelState<StreamChatGenerics>['read'];
2928
threadMessages: ThreadContextValue<StreamChatGenerics>['threadMessages'];
3029
typing: TypingContextValue<StreamChatGenerics>['typing'];
31-
watcherCount: ChannelContextValue<StreamChatGenerics>['watcherCount'];
32-
watchers: ChannelContextValue<StreamChatGenerics>['watchers'];
30+
watcherCount: number;
31+
watchers: StreamChannelState<StreamChatGenerics>['watchers'];
3332
};
3433

3534
type ChannelsState<
@@ -56,25 +55,12 @@ type SetStateAction<
5655
type: 'SET_STATE';
5756
};
5857

59-
type IncreaseSubscriberCountAction = {
60-
payload: { cid: string };
61-
type: 'INCREASE_SUBSCRIBER_COUNT';
62-
};
63-
type DecreaseSubscriberCountAction = {
64-
payload: { cid: string };
65-
type: 'DECREASE_SUBSCRIBER_COUNT';
66-
};
67-
6858
type Action<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
69-
| SetStateAction<StreamChatGenerics>
70-
| IncreaseSubscriberCountAction
71-
| DecreaseSubscriberCountAction;
59+
SetStateAction<StreamChatGenerics>;
7260

7361
export type ChannelsStateContextValue<
7462
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
7563
> = {
76-
decreaseSubscriberCount: (value: { cid: string }) => void;
77-
increaseSubscriberCount: (value: { cid: string }) => void;
7864
setState: (value: Payload<Keys, StreamChatGenerics>) => void;
7965
state: ChannelsState<StreamChatGenerics>;
8066
};
@@ -95,39 +81,6 @@ function reducer(state: ChannelsState, action: Action) {
9581
},
9682
};
9783

98-
case 'INCREASE_SUBSCRIBER_COUNT': {
99-
const currentCount = state[action.payload.cid]?.subscriberCount ?? 0;
100-
return {
101-
...state,
102-
[action.payload.cid]: {
103-
...(state[action.payload.cid] || {}),
104-
subscriberCount: currentCount + 1,
105-
},
106-
};
107-
}
108-
109-
case 'DECREASE_SUBSCRIBER_COUNT': {
110-
const currentCount = state[action.payload.cid]?.subscriberCount ?? 0;
111-
112-
// If there last subscribed Channel component unsubscribes, we clear the channel state.
113-
if (currentCount <= 1) {
114-
const stateShallowCopy = {
115-
...state,
116-
};
117-
118-
delete stateShallowCopy[action.payload.cid];
119-
120-
return stateShallowCopy;
121-
}
122-
123-
return {
124-
...state,
125-
[action.payload.cid]: {
126-
...(state[action.payload.cid] || {}),
127-
subscriberCount: currentCount - 1,
128-
},
129-
};
130-
}
13184
default:
13285
throw new Error();
13386
}
@@ -150,18 +103,8 @@ export const ChannelsStateProvider = <
150103
dispatch({ payload, type: 'SET_STATE' });
151104
}, []);
152105

153-
const increaseSubscriberCount = useCallback((payload: { cid: string }) => {
154-
dispatch({ payload, type: 'INCREASE_SUBSCRIBER_COUNT' });
155-
}, []);
156-
157-
const decreaseSubscriberCount = useCallback((payload: { cid: string }) => {
158-
dispatch({ payload, type: 'DECREASE_SUBSCRIBER_COUNT' });
159-
}, []);
160-
161106
const value = useMemo(
162107
() => ({
163-
decreaseSubscriberCount,
164-
increaseSubscriberCount,
165108
setState,
166109
state,
167110
}),

‎package/src/contexts/channelsStateContext/useChannelState.ts

+5-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useMemo } from 'react';
1+
import { useCallback, useMemo } from 'react';
22

33
import type { Channel as ChannelType } from 'stream-chat';
44

@@ -11,15 +11,12 @@ import type { DefaultStreamChatGenerics } from '../../types/types';
1111
type StateManagerParams<
1212
Key extends Keys,
1313
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
14-
> = Omit<
15-
ChannelsStateContextValue<StreamChatGenerics>,
16-
'increaseSubscriberCount' | 'decreaseSubscriberCount'
17-
> & {
14+
> = ChannelsStateContextValue<StreamChatGenerics> & {
1815
cid: string;
1916
key: Key;
2017
};
2118

22-
/*
19+
/*
2320
This hook takes care of creating a useState-like interface which can be used later to call
2421
updates to the ChannelsStateContext reducer. It receives the cid and key which it wants to update
2522
and perform the state updates. Also supports a initialState.
@@ -28,15 +25,7 @@ function useStateManager<
2825
Key extends Keys,
2926
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
3027
>(
31-
{
32-
cid,
33-
key,
34-
setState,
35-
state,
36-
}: Omit<
37-
StateManagerParams<Key, StreamChatGenerics>,
38-
'increaseSubscriberCount' | 'decreaseSubscriberCount'
39-
>,
28+
{ cid, key, setState, state }: StateManagerParams<Key, StreamChatGenerics>,
4029
initialValue?: ChannelState<StreamChatGenerics>[Key],
4130
) {
4231
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -79,17 +68,7 @@ export function useChannelState<
7968
threadId?: string,
8069
): UseChannelStateValue<StreamChatGenerics> {
8170
const cid = channel?.id || 'id'; // in case channel is not initialized, use generic id string for indexing
82-
const { decreaseSubscriberCount, increaseSubscriberCount, setState, state } =
83-
useChannelsStateContext<StreamChatGenerics>();
84-
85-
// Keeps track of how many Channel components are subscribed to this Channel state (Channel vs Thread concurrency)
86-
useEffect(() => {
87-
increaseSubscriberCount({ cid });
88-
return () => {
89-
decreaseSubscriberCount({ cid });
90-
};
91-
// eslint-disable-next-line react-hooks/exhaustive-deps
92-
}, []);
71+
const { setState, state } = useChannelsStateContext<StreamChatGenerics>();
9372

9473
const [members, setMembers] = useStateManager(
9574
{

0 commit comments

Comments
 (0)