Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groups: fix query invalidation loop #3007

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ui/src/logic/useReactQuerySubscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function useReactQuerySubscription<T>({
scry,
scryApp = app,
priority = 3,
onEvent,
options,
}: {
queryKey: QueryKey;
Expand All @@ -24,6 +25,7 @@ export default function useReactQuerySubscription<T>({
scry: string;
scryApp?: string;
priority?: number;
onEvent?: (data: Event) => void;
options?: UseQueryOptions<T>;
}) {
const queryClient = useQueryClient();
Expand All @@ -50,9 +52,9 @@ export default function useReactQuerySubscription<T>({
api.subscribe({
app,
path,
event: invalidate.current,
event: onEvent ? onEvent : invalidate.current,
});
}, [app, path, queryClient, queryKey]);
}, [app, path, queryClient, queryKey, onEvent]);

return useQuery(queryKey, fetchData, {
staleTime: 60 * 1000,
Expand Down
37 changes: 30 additions & 7 deletions ui/src/state/groups/groups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { useParams } from 'react-router';
import { useCallback, useEffect, useMemo } from 'react';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import create from 'zustand';
import {
MutationFunction,
Expand Down Expand Up @@ -248,18 +248,41 @@ const defGang = {

export function useGangs() {
const queryClient = useQueryClient();
const queryKey = ['gangs'];

const invalidate = useRef(
_.debounce(
() => {
queryClient.invalidateQueries(queryKey);
},
300,
{ leading: true, trailing: true }
)
);

const { data, ...rest } = useReactQuerySubscription({
queryKey: ['gangs'],
queryKey,
app: 'groups',
path: `/gangs/updates`,
scry: `/gangs`,
onEvent: (event) => {
// right now for long group joins, the gang is initially removed but no fact about the corresponding created
// group is emitted until the join completes. This is a blunt hack to ensure our view of existing groups remains up to date.
// Once this is fixed, we should remove this and use the default useReactQuerySubscription event handler
const currGangCount = Object.keys(
queryClient.getQueryData<Gangs>(queryKey) || {}
).length;
const newGangCount = Object.keys(event || {}).length;
const gangWasRemoved =
currGangCount && newGangCount && newGangCount < currGangCount;
if (gangWasRemoved) {
queryClient.invalidateQueries(['groups']);
}

invalidate.current();
},
options: {
refetchOnMount: false,
onSuccess: () => {
// TEMPORARY: right now for long group joins, the gang is initially removed but no fact about the corresponding created
// group is emitted until the join completes. This is a blunt hack to ensure our view of existing groups remains up to date
queryClient.invalidateQueries(['groups']);
},
},
});

Expand Down
Loading