Skip to content
Snippets Groups Projects
Unverified Commit 8e155d42 authored by gabriellsh's avatar gabriellsh Committed by Diego Sampaio
Browse files

fix: Threads breaking after sending messages too fast (#30622)

parent d2003372
No related branches found
No related tags found
No related merge requests found
---
"@rocket.chat/meteor": patch
---
fixed threads breaking when sending messages too fast
import { isThreadMainMessage } from '@rocket.chat/core-typings';
import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings'; import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings';
import { useStream } from '@rocket.chat/ui-contexts'; import { useStream } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query'; import type { UseQueryResult } from '@tanstack/react-query';
import { useQueryClient, useQuery } from '@tanstack/react-query'; import { useQueryClient, useQuery } from '@tanstack/react-query';
import { useCallback, useEffect, useRef } from 'react'; import { useCallback, useEffect, useRef } from 'react';
import { withDebouncing } from '../../../../../../lib/utils/highOrderFunctions';
import type { FieldExpression, Query } from '../../../../../lib/minimongo'; import type { FieldExpression, Query } from '../../../../../lib/minimongo';
import { createFilterFromQuery } from '../../../../../lib/minimongo'; import { createFilterFromQuery } from '../../../../../lib/minimongo';
import { onClientMessageReceived } from '../../../../../lib/onClientMessageReceived';
import { useRoom } from '../../../contexts/RoomContext'; import { useRoom } from '../../../contexts/RoomContext';
import { useGetMessageByID } from './useGetMessageByID'; import { useGetMessageByID } from './useGetMessageByID';
...@@ -87,19 +86,22 @@ export const useThreadMainMessageQuery = ( ...@@ -87,19 +86,22 @@ export const useThreadMainMessageQuery = (
}, [tmid]); }, [tmid]);
return useQuery(['rooms', room._id, 'threads', tmid, 'main-message'] as const, async ({ queryKey }) => { return useQuery(['rooms', room._id, 'threads', tmid, 'main-message'] as const, async ({ queryKey }) => {
const message = await getMessage(tmid); const mainMessage = await getMessage(tmid);
const mainMessage = (await onClientMessageReceived(message)) || message; if (!mainMessage) {
if (!mainMessage && !isThreadMainMessage(mainMessage)) {
throw new Error('Invalid main message'); throw new Error('Invalid main message');
} }
const debouncedInvalidate = withDebouncing({ wait: 10000 })(() => {
queryClient.invalidateQueries(queryKey, { exact: true });
});
unsubscribeRef.current = unsubscribeRef.current =
unsubscribeRef.current || unsubscribeRef.current ||
subscribeToMessage(mainMessage, { subscribeToMessage(mainMessage, {
onMutate: () => { onMutate: (message) => {
queryClient.invalidateQueries(queryKey, { exact: true }); queryClient.setQueryData(queryKey, () => message);
debouncedInvalidate();
}, },
onDelete: () => { onDelete: () => {
onDelete?.(); onDelete?.();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment