Skip to content

Commit 51361c1

Browse files
committed
Merge branch 'develop' into V7
# Conflicts: # package/expo-package/yarn.lock # package/native-package/yarn.lock # package/src/components/Channel/Channel.tsx
2 parents b947266 + 6cca07d commit 51361c1

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -731,14 +731,33 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
731731
channel,
732732
});
733733

734-
/**
735-
* Since we copy the current channel state all together, we need to find the greatest time among the below two and apply it as the throttling time for copying the channel state.
736-
* This is done until we remove the newMessageStateUpdateThrottleInterval prop.
737-
*/
738-
const copyChannelStateThrottlingTime =
739-
newMessageStateUpdateThrottleInterval > stateUpdateThrottleInterval
740-
? newMessageStateUpdateThrottleInterval
741-
: stateUpdateThrottleInterval;
734+
const setReadThrottled = useMemo(
735+
() =>
736+
throttle(
737+
() => {
738+
if (channel) {
739+
setRead(channel);
740+
}
741+
},
742+
stateUpdateThrottleInterval,
743+
throttleOptions,
744+
),
745+
[channel, stateUpdateThrottleInterval, setRead],
746+
);
747+
748+
const copyMessagesStateFromChannelThrottled = useMemo(
749+
() =>
750+
throttle(
751+
() => {
752+
if (channel) {
753+
copyMessagesStateFromChannel(channel);
754+
}
755+
},
756+
newMessageStateUpdateThrottleInterval,
757+
throttleOptions,
758+
),
759+
[channel, newMessageStateUpdateThrottleInterval, copyMessagesStateFromChannel],
760+
);
742761

743762
const copyChannelState = useMemo(
744763
() =>
@@ -749,10 +768,10 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
749768
copyMessagesStateFromChannel(channel);
750769
}
751770
},
752-
copyChannelStateThrottlingTime,
771+
stateUpdateThrottleInterval,
753772
throttleOptions,
754773
),
755-
[channel, copyChannelStateThrottlingTime, copyMessagesStateFromChannel, copyStateFromChannel],
774+
[stateUpdateThrottleInterval, channel, copyStateFromChannel, copyMessagesStateFromChannel],
756775
);
757776

758777
const handleEvent: EventHandler = (event) => {
@@ -809,6 +828,16 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
809828

810829
// only update channel state if the events are not the previously subscribed useEffect's subscription events
811830
if (channel && channel.initialized) {
831+
if (event.type === 'message.new') {
832+
copyMessagesStateFromChannelThrottled();
833+
return;
834+
}
835+
836+
if (event.type === 'message.read' || event.type === 'notification.mark_read') {
837+
setReadThrottled();
838+
return;
839+
}
840+
812841
copyChannelState();
813842
}
814843
}
@@ -819,7 +848,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
819848
const initChannel = async () => {
820849
setLastRead(new Date());
821850
const unreadCount = channel.countUnread();
822-
if (!channel || !shouldSyncChannel || channel.offlineMode) {
851+
if (!channel || !shouldSyncChannel) {
823852
return;
824853
}
825854
let errored = false;
@@ -890,20 +919,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
890919
return unsubscribe;
891920
}, [channel?.cid, client]);
892921

893-
/**
894-
* Subscription to the Notification mark_read event.
895-
*/
896-
useEffect(() => {
897-
const handleEvent: EventHandler = (event) => {
898-
if (channel.cid === event.cid) {
899-
setRead(channel);
900-
}
901-
};
902-
903-
const { unsubscribe } = client.on('notification.mark_read', handleEvent);
904-
return unsubscribe;
905-
}, [channel, client, setRead]);
906-
907922
const threadPropsExists = !!threadProps;
908923

909924
useEffect(() => {

package/src/components/Channel/__tests__/Channel.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ describe('Channel initial load useEffect', () => {
356356
cleanup();
357357
});
358358

359-
it('should not call channel.watch if channel is not initialized', async () => {
359+
it('should still call channel.watch if we are online and DB channels are loaded', async () => {
360360
const messages = Array.from({ length: 10 }, (_, i) => generateMessage({ id: i }));
361361
const mockedChannel = generateChannelResponse({
362362
messages,
@@ -366,13 +366,18 @@ describe('Channel initial load useEffect', () => {
366366
const channel = chatClient.channel('messaging', mockedChannel.id);
367367
await channel.watch();
368368
channel.offlineMode = true;
369-
channel.state = channelInitialState;
369+
channel.state = {
370+
...channelInitialState,
371+
messagePagination: {
372+
hasPrev: true,
373+
},
374+
};
370375
const watchSpy = jest.fn();
371376
channel.watch = watchSpy;
372377

373378
renderComponent({ channel });
374379

375-
await waitFor(() => expect(watchSpy).not.toHaveBeenCalled());
380+
await waitFor(() => expect(watchSpy).toHaveBeenCalledTimes(1));
376381
});
377382

378383
it("should call channel.watch if channel is initialized and it's not in offline mode", async () => {

0 commit comments

Comments
 (0)