Skip to content

Commit 7e01f19

Browse files
committed
fix: remove initialized checks where they make absolutely no sense
1 parent 858610c commit 7e01f19

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,17 @@ export type ChannelPropsWithContext = Pick<ChannelContextValue, 'channel'> &
505505
* Tells if channel is rendering a thread list
506506
*/
507507
threadList?: boolean;
508+
/**
509+
* A boolean signifying whether the Channel component should run channel.watch()
510+
* whenever it mounts up a new channel. If set to `false`, it is the integrator's
511+
* responsibility to run channel.watch() if they wish to receive WebSocket events
512+
* for that channel.
513+
*
514+
* Can be particularly useful whenever we are viewing channels in a read-only mode
515+
* or perhaps want them in an ephemeral state (i.e not created until the first message
516+
* is sent).
517+
*/
518+
initializeOnMount?: boolean;
508519
} & Partial<
509520
Pick<
510521
InputMessageInputContextValue,
@@ -733,6 +744,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
733744
VideoThumbnail = VideoThumbnailDefault,
734745
isOnline,
735746
maximumMessageLimit,
747+
initializeOnMount = true,
736748
} = props;
737749

738750
const { thread: threadProps, threadInstance } = threadFromProps;
@@ -899,7 +911,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
899911
}
900912

901913
// only update channel state if the events are not the previously subscribed useEffect's subscription events
902-
if (channel && channel.initialized) {
914+
if (channel) {
903915
// we skip the new message events if we've already done an optimistic update for the new message
904916
if (event.type === 'message.new' || event.type === 'notification.message_new') {
905917
const messageId = event.message?.id ?? '';
@@ -933,13 +945,15 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
933945
}
934946
let errored = false;
935947

936-
if (!channel.initialized || !channel.state.isUpToDate) {
948+
if (!channel.initialized || !channel.state.isUpToDate || !initializeOnMount) {
937949
try {
938950
await channel?.watch();
951+
console.log('WATCHCALLED2');
939952
} catch (err) {
940953
console.warn('Channel watch request failed with error:', err);
941954
setError(true);
942955
errored = true;
956+
channel.offlineMode = true;
943957
}
944958
}
945959

@@ -1096,7 +1110,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
10961110
});
10971111

10981112
const resyncChannel = useStableCallback(async () => {
1099-
if (!channel || syncingChannelRef.current) {
1113+
if (!channel || syncingChannelRef.current || (!channel.initialized && !channel.offlineMode)) {
11001114
return;
11011115
}
11021116
syncingChannelRef.current = true;
@@ -1112,11 +1126,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
11121126

11131127
try {
11141128
if (channelMessagesState?.messages) {
1129+
console.log('WATCHCALLED1');
11151130
await channel?.watch({
11161131
messages: {
11171132
limit: channelMessagesState.messages.length + 30,
11181133
},
11191134
});
1135+
channel.offlineMode = false;
11201136
}
11211137

11221138
if (!thread) {

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
718718

719719
const renderItem = useCallback(
720720
({ index, item: message }: { index: number; item: LocalMessage }) => {
721-
if (!channel || channel.disconnected || (!channel.initialized && !channel.offlineMode)) {
721+
if (!channel || channel.disconnected) {
722722
return null;
723723
}
724724

package/src/components/MessageList/MessageList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
781781

782782
const renderItem = useCallback(
783783
({ index, item: message }: { index: number; item: LocalMessage }) => {
784-
if (!channel || channel.disconnected || (!channel.initialized && !channel.offlineMode)) {
784+
if (!channel || channel.disconnected) {
785785
return null;
786786
}
787787

0 commit comments

Comments
 (0)