Skip to content

Commit 0d7c3af

Browse files
committed
Improved ws query invalidation
1 parent 2040445 commit 0d7c3af

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

src/Exceptionless.Web/ClientApp/src/lib/features/events/api.svelte.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
import type { WebSocketMessageValue } from '$features/websockets/models';
2+
13
import { accessToken } from '$features/auth/index.svelte';
24
import { type ProblemDetails, useFetchClient } from '@exceptionless/fetchclient';
3-
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
5+
import { createQuery, QueryClient, useQueryClient } from '@tanstack/svelte-query';
46

57
import type { PersistentEvent } from './models';
68

9+
export async function invalidatePersistentEventQueries(queryClient: QueryClient, message: WebSocketMessageValue<'PersistentEventChanged'>) {
10+
const { id, stack_id } = message;
11+
if (id) {
12+
await queryClient.invalidateQueries({ queryKey: queryKeys.id(id) });
13+
}
14+
15+
if (stack_id) {
16+
await queryClient.invalidateQueries({ queryKey: queryKeys.stacks(stack_id) });
17+
}
18+
19+
if (!id && !stack_id) {
20+
await queryClient.invalidateQueries({ queryKey: queryKeys.all });
21+
}
22+
}
23+
724
export const queryKeys = {
825
all: ['PersistentEvent'] as const,
926
allWithFilters: (filters: string) => [...queryKeys.all, { filters }] as const,

src/Exceptionless.Web/ClientApp/src/lib/features/organizations/api.svelte.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import type { WebSocketMessageValue } from '$features/websockets/models';
2+
import type { QueryClient } from '@tanstack/svelte-query';
3+
14
import { accessToken } from '$features/auth/index.svelte';
25
import { type ProblemDetails, useFetchClient } from '@exceptionless/fetchclient';
36
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
47

58
import type { ViewOrganization } from './models';
69

10+
export async function invalidateOrganizationQueries(queryClient: QueryClient, message: WebSocketMessageValue<'OrganizationChanged'>) {
11+
const { id } = message;
12+
if (id) {
13+
await queryClient.invalidateQueries({ queryKey: queryKeys.id(id) });
14+
} else {
15+
await queryClient.invalidateQueries({ queryKey: queryKeys.all });
16+
}
17+
}
18+
719
export const queryKeys = {
820
all: ['Organization'] as const,
921
allWithMode: (mode: 'stats' | undefined) => [...queryKeys.all, { mode }] as const,

src/Exceptionless.Web/ClientApp/src/lib/features/projects/api.svelte.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
import type { WebSocketMessageValue } from '$features/websockets/models';
2+
13
import { accessToken } from '$features/auth/index.svelte';
24
import { type ProblemDetails, useFetchClient } from '@exceptionless/fetchclient';
3-
import { createMutation, createQuery, useQueryClient } from '@tanstack/svelte-query';
5+
import { createMutation, createQuery, QueryClient, useQueryClient } from '@tanstack/svelte-query';
46

57
import type { ViewProject } from './models';
68

9+
export async function invalidateProjectQueries(queryClient: QueryClient, message: WebSocketMessageValue<'ProjectChanged'>) {
10+
const { id, organization_id } = message;
11+
if (id) {
12+
await queryClient.invalidateQueries({ queryKey: queryKeys.id(id) });
13+
}
14+
15+
if (organization_id) {
16+
await queryClient.invalidateQueries({ queryKey: queryKeys.organization(organization_id) });
17+
}
18+
19+
if (!id && !organization_id) {
20+
await queryClient.invalidateQueries({ queryKey: queryKeys.all });
21+
}
22+
}
23+
724
export const queryKeys = {
825
all: ['Project'] as const,
926
allWithFilters: (filters: string) => [...queryKeys.all, { filters }] as const,

src/Exceptionless.Web/ClientApp/src/lib/features/stacks/api.svelte.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import type { WebSocketMessageValue } from '$features/websockets/models';
2+
13
import { accessToken } from '$features/auth/index.svelte';
24
import { type ProblemDetails, useFetchClient } from '@exceptionless/fetchclient';
3-
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
5+
import { createQuery, QueryClient, useQueryClient } from '@tanstack/svelte-query';
46

57
import type { Stack } from './models';
68

9+
//
10+
export async function invalidateStackQueries(queryClient: QueryClient, message: WebSocketMessageValue<'StackChanged'>) {
11+
const { id } = message;
12+
if (id) {
13+
await queryClient.invalidateQueries({ queryKey: queryKeys.id(id) });
14+
} else {
15+
await queryClient.invalidateQueries({ queryKey: queryKeys.all });
16+
}
17+
}
18+
719
export const queryKeys = {
820
all: ['Stack'] as const,
921
allWithFilters: (filters: string) => [...queryKeys.all, { filters }] as const,

src/Exceptionless.Web/ClientApp/src/lib/features/users/api.svelte.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
import type { WebSocketMessageValue } from '$features/websockets/models';
2+
13
import { accessToken } from '$features/auth/index.svelte';
24
import { ProblemDetails, useFetchClient } from '@exceptionless/fetchclient';
3-
import { createMutation, createQuery, useQueryClient } from '@tanstack/svelte-query';
5+
import { createMutation, createQuery, QueryClient, useQueryClient } from '@tanstack/svelte-query';
46

57
import { UpdateEmailAddressResult, type UpdateUser, User } from './models';
68

9+
export async function invalidateUserQueries(queryClient: QueryClient, message: WebSocketMessageValue<'UserChanged'>) {
10+
const { id } = message;
11+
if (id) {
12+
await queryClient.invalidateQueries({ queryKey: queryKeys.id(id) });
13+
14+
const currentUser = queryClient.getQueryData<User>(queryKeys.me());
15+
if (currentUser?.id === id) {
16+
queryClient.invalidateQueries({ queryKey: queryKeys.me() });
17+
}
18+
} else {
19+
await queryClient.invalidateQueries({ queryKey: queryKeys.all });
20+
}
21+
}
22+
723
export const queryKeys = {
824
all: ['User'] as const,
925
id: (id: string | undefined) => [...queryKeys.all, id] as const,

src/Exceptionless.Web/ClientApp/src/routes/(app)/+layout.svelte

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import { page } from '$app/stores';
55
import { useSidebar } from '$comp/ui/sidebar';
66
import { accessToken, gotoLogin } from '$features/auth/index.svelte';
7-
import { getMeQuery } from '$features/users/api.svelte';
7+
import { invalidatePersistentEventQueries } from '$features/events/api.svelte';
8+
import { invalidateOrganizationQueries } from '$features/organizations/api.svelte';
9+
import { invalidateProjectQueries } from '$features/projects/api.svelte';
10+
import { invalidateStackQueries } from '$features/stacks/api.svelte';
11+
import { getMeQuery, invalidateUserQueries } from '$features/users/api.svelte';
812
import { isEntityChangedType, type WebSocketMessageType } from '$features/websockets/models';
913
import { WebSocketClient } from '$features/websockets/WebSocketClient.svelte';
1014
import { validate } from '$shared/validation';
@@ -63,7 +67,26 @@
6367
);
6468
6569
if (isEntityChangedType(data)) {
66-
await queryClient.invalidateQueries({ queryKey: [data.message.type] });
70+
switch (data.type) {
71+
case 'OrganizationChanged':
72+
await invalidateOrganizationQueries(queryClient, data.message);
73+
break;
74+
case 'PersistentEventChanged':
75+
await invalidatePersistentEventQueries(queryClient, data.message);
76+
break;
77+
case 'ProjectChanged':
78+
await invalidateProjectQueries(queryClient, data.message);
79+
break;
80+
case 'StackChanged':
81+
await invalidateStackQueries(queryClient, data.message);
82+
break;
83+
case 'UserChanged':
84+
await invalidateUserQueries(queryClient, data.message);
85+
break;
86+
default:
87+
await queryClient.invalidateQueries({ queryKey: [data.message.type] });
88+
break;
89+
}
6790
}
6891
6992
// This event is fired when a user is added or removed from an organization.

0 commit comments

Comments
 (0)