Skip to content

Commit

Permalink
feat: add keepPreviousData to config
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Jul 12, 2021
1 parent 525f297 commit fae88b7
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 369 deletions.
23 changes: 10 additions & 13 deletions src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,11 @@ import {
} from './config/constants';
import configureHooks from './hooks';
import configureMutations from './mutations';
import type { QueryClientConfig } from './types';
import configureWebSockets from './ws';

export type Notifier = (e: any) => any;

type QueryClientConfig = {
API_HOST: string;
S3_FILES_HOST?: string;
SHOW_NOTIFICATIONS?: boolean;
WS_HOST?: string;
enableWebsocket?: boolean;
notifier?: Notifier;
staleTime?: number;
cacheTime?: number;
};

// Query client retry function decides when and how many times a request should be retried
const retry = (failureCount: any, error: { name: string }) => {
// do not retry if the request was not authorized
Expand All @@ -46,6 +36,7 @@ export default (config: Partial<QueryClientConfig>) => {
config?.SHOW_NOTIFICATIONS ||
process.env.REACT_APP_SHOW_NOTIFICATIONS === 'true' ||
false,
keepPreviousData: config?.keepPreviousData || false,
};

// define config for query client
Expand All @@ -57,7 +48,7 @@ export default (config: Partial<QueryClientConfig>) => {
process.env.REACT_APP_WS_HOST ||
`${baseConfig.API_HOST.replace('http', 'ws')}/ws`,
// whether websocket support should be enabled
enableWebsocket: config?.enableWebsocket ?? false,
enableWebsocket: config?.enableWebsocket || false,
notifier: config?.notifier,
// time until data in cache considered stale if cache not invalidated
staleTime: config?.staleTime ?? STALE_TIME_MILLISECONDS,
Expand All @@ -67,7 +58,13 @@ export default (config: Partial<QueryClientConfig>) => {
};

// create queryclient with given config
const queryClient = new QueryClient();
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: config?.refetchOnWindowFocus || false,
},
},
});

// set up mutations given config
// mutations are attached to queryClient
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type QueryClientConfig = {
staleTime: number;
cacheTime: number;
retry: RetryValue<any>;
refetchOnWindowFocus?: boolean;
keepPreviousData?: boolean;
};

// Graasp Core Types
Expand Down
Loading

0 comments on commit fae88b7

Please sign in to comment.