|
1 | | -import React, { PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react'; |
| 1 | +import React, { PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState } from 'react'; |
2 | 2 | import { KeyboardAvoidingViewProps, StyleSheet, Text, View } from 'react-native'; |
3 | 3 |
|
4 | 4 | import debounce from 'lodash/debounce'; |
@@ -749,24 +749,34 @@ const ChannelWithContext = < |
749 | 749 | ? newMessageStateUpdateThrottleInterval |
750 | 750 | : stateUpdateThrottleInterval; |
751 | 751 |
|
752 | | - const copyChannelState = useRef( |
753 | | - throttle( |
754 | | - () => { |
755 | | - if (channel) { |
756 | | - copyStateFromChannel(channel); |
757 | | - copyMessagesStateFromChannel(channel); |
758 | | - } |
759 | | - }, |
760 | | - copyChannelStateThrottlingTime, |
761 | | - throttleOptions, |
762 | | - ), |
763 | | - ).current; |
| 752 | + const copyChannelState = useMemo( |
| 753 | + () => |
| 754 | + throttle( |
| 755 | + () => { |
| 756 | + if (channel) { |
| 757 | + copyStateFromChannel(channel); |
| 758 | + copyMessagesStateFromChannel(channel); |
| 759 | + } |
| 760 | + }, |
| 761 | + copyChannelStateThrottlingTime, |
| 762 | + throttleOptions, |
| 763 | + ), |
| 764 | + [channel, copyChannelStateThrottlingTime, copyMessagesStateFromChannel, copyStateFromChannel], |
| 765 | + ); |
764 | 766 |
|
765 | 767 | const handleEvent: EventHandler<StreamChatGenerics> = (event) => { |
766 | 768 | if (shouldSyncChannel) { |
767 | | - // Ignore user.watching.start and user.watching.stop events |
768 | | - const ignorableEvents = ['user.watching.start', 'user.watching.stop']; |
769 | | - if (ignorableEvents.includes(event.type)) return; |
| 769 | + /** |
| 770 | + * Ignore user.watching.start and user.watching.stop as we should not copy the entire state when |
| 771 | + * they occur. Also ignore all poll related events since they're being handled in their own |
| 772 | + * reactive state and have no business having an effect on the Channel component. |
| 773 | + */ |
| 774 | + if ( |
| 775 | + event.type.startsWith('poll.') || |
| 776 | + event.type === 'user.watching.start' || |
| 777 | + event.type === 'user.watching.stop' |
| 778 | + ) |
| 779 | + return; |
770 | 780 |
|
771 | 781 | // If the event is typing.start or typing.stop, set the typing state |
772 | 782 | const isTypingEvent = event.type === 'typing.start' || event.type === 'typing.stop'; |
|
0 commit comments