Skip to content

Commit 9692f1a

Browse files
authored
refactor: remove stale subscriber count logic and types refactor (#2832)
* refactor: remove stale subscriber count logic and types refactor (#2782) * fix: lint issues
1 parent de095b0 commit 9692f1a

File tree

5 files changed

+25
-98
lines changed

5 files changed

+25
-98
lines changed

package/src/components/Message/Message.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,15 @@ export type MessagePropsWithContext<
125125
> = Pick<ChannelContextValue<StreamChatGenerics>, 'channel' | 'enforceUniqueReaction' | 'members'> &
126126
Pick<KeyboardContextValue, 'dismissKeyboard'> &
127127
Partial<
128-
Omit<MessageContextValue<StreamChatGenerics>, 'groupStyles' | 'handleReaction' | 'message' | 'isMessageAIGenerated'>
128+
Omit<
129+
MessageContextValue<StreamChatGenerics>,
130+
'groupStyles' | 'handleReaction' | 'message' | 'isMessageAIGenerated'
131+
>
132+
> &
133+
Pick<
134+
MessageContextValue<StreamChatGenerics>,
135+
'groupStyles' | 'message' | 'isMessageAIGenerated'
129136
> &
130-
Pick<MessageContextValue<StreamChatGenerics>, 'groupStyles' | 'message' | 'isMessageAIGenerated'> &
131137
Pick<
132138
MessagesContextValue<StreamChatGenerics>,
133139
| 'sendReaction'

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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext';
1+
import { ChannelState } from 'stream-chat';
2+
23
import type { PaginatedMessageListContextValue } from '../../../contexts/paginatedMessageListContext/PaginatedMessageListContext';
34
import type { ThreadContextValue } from '../../../contexts/threadContext/ThreadContext';
45
import type { DefaultStreamChatGenerics } from '../../../types/types';
@@ -10,7 +11,7 @@ export const getReadStates = <
1011
messages:
1112
| PaginatedMessageListContextValue<StreamChatGenerics>['messages']
1213
| ThreadContextValue<StreamChatGenerics>['threadMessages'],
13-
read?: ChannelContextValue<StreamChatGenerics>['read'],
14+
read?: ChannelState<StreamChatGenerics>['read'],
1415
) => {
1516
const readData: Record<string, number> = {};
1617

package/src/contexts/channelsStateContext/ChannelsStateContext.tsx

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import React, {
88
useRef,
99
} from 'react';
1010

11+
import { ChannelState as StreamChannelState } from 'stream-chat';
12+
1113
import type { DefaultStreamChatGenerics } from '../../types/types';
1214
import { ActiveChannelsProvider } from '../activeChannelsRefContext/ActiveChannelsRefContext';
1315

14-
import type { ChannelContextValue } from '../channelContext/ChannelContext';
1516
import type { PaginatedMessageListContextValue } from '../paginatedMessageListContext/PaginatedMessageListContext';
1617
import type { ThreadContextValue } from '../threadContext/ThreadContext';
1718
import type { TypingContextValue } from '../typingContext/TypingContext';
@@ -22,14 +23,13 @@ import { isTestEnvironment } from '../utils/isTestEnvironment';
2223
export type ChannelState<
2324
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2425
> = {
25-
members: ChannelContextValue<StreamChatGenerics>['members'];
26+
members: StreamChannelState<StreamChatGenerics>['members'];
2627
messages: PaginatedMessageListContextValue<StreamChatGenerics>['messages'];
27-
read: ChannelContextValue<StreamChatGenerics>['read'];
28-
subscriberCount: number;
28+
read: StreamChannelState<StreamChatGenerics>['read'];
2929
threadMessages: ThreadContextValue<StreamChatGenerics>['threadMessages'];
3030
typing: TypingContextValue<StreamChatGenerics>['typing'];
31-
watcherCount: ChannelContextValue<StreamChatGenerics>['watcherCount'];
32-
watchers: ChannelContextValue<StreamChatGenerics>['watchers'];
31+
watcherCount: number;
32+
watchers: StreamChannelState<StreamChatGenerics>['watchers'];
3333
};
3434

3535
type ChannelsState<
@@ -56,25 +56,12 @@ type SetStateAction<
5656
type: 'SET_STATE';
5757
};
5858

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-
6859
type Action<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> =
69-
| SetStateAction<StreamChatGenerics>
70-
| IncreaseSubscriberCountAction
71-
| DecreaseSubscriberCountAction;
60+
SetStateAction<StreamChatGenerics>;
7261

7362
export type ChannelsStateContextValue<
7463
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
7564
> = {
76-
decreaseSubscriberCount: (value: { cid: string }) => void;
77-
increaseSubscriberCount: (value: { cid: string }) => void;
7865
setState: (value: Payload<Keys, StreamChatGenerics>) => void;
7966
state: ChannelsState<StreamChatGenerics>;
8067
};
@@ -95,39 +82,6 @@ function reducer(state: ChannelsState, action: Action) {
9582
},
9683
};
9784

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-
}
13185
default:
13286
throw new Error();
13387
}
@@ -150,18 +104,8 @@ export const ChannelsStateProvider = <
150104
dispatch({ payload, type: 'SET_STATE' });
151105
}, []);
152106

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-
161107
const value = useMemo(
162108
() => ({
163-
decreaseSubscriberCount,
164-
increaseSubscriberCount,
165109
setState,
166110
state,
167111
}),

package/src/contexts/channelsStateContext/useChannelState.ts

Lines changed: 5 additions & 26 deletions
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)