Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 6d63b4b

Browse files
feat: debounce api calls
1 parent b687335 commit 6d63b4b

File tree

10 files changed

+43
-5
lines changed

10 files changed

+43
-5
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"@electron/remote": "^2.0.8",
116116
"@emotion/react": "^11.10.0",
117117
"@emotion/styled": "^11.10.0",
118+
"apollo-link-debounce": "^3.0.0",
118119
"bootstrap": "^5.1.3",
119120
"buffer": "^6.0.3",
120121
"electron-debug": "^3.2.0",

src/renderer/graphql/client.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
1+
import {
2+
ApolloClient,
3+
InMemoryCache,
4+
HttpLink,
5+
ApolloLink,
6+
} from '@apollo/client';
27
import { setContext } from '@apollo/client/link/context';
8+
import DebounceLink from 'apollo-link-debounce';
39
import { API_URL, TOKEN } from 'renderer/lib/config';
410

5-
const httpLink = new HttpLink({
6-
uri: API_URL,
7-
});
11+
const DEFAULT_DEBOUNCE_TIMEOUT = 500;
12+
13+
const Link = ApolloLink.from([
14+
new DebounceLink(DEFAULT_DEBOUNCE_TIMEOUT),
15+
new HttpLink({
16+
uri: API_URL,
17+
}),
18+
]);
819

920
const authLink = setContext((_, { headers }) => {
1021
// get the authentication token from local storage if it exists
@@ -19,7 +30,7 @@ const authLink = setContext((_, { headers }) => {
1930
});
2031

2132
const client = new ApolloClient({
22-
link: authLink.concat(httpLink),
33+
link: authLink.concat(Link),
2334
cache: new InMemoryCache(),
2435
});
2536

src/renderer/pages/FavoriteCookbooks.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export default function FavoriteCookbooks() {
2020
...GET_USER_SUBSCRIBED_COOKBOOKS_VARIABLES,
2121
name: filters.searchTerm,
2222
},
23+
context: {
24+
debounceKey: 'favorite-cookbooks',
25+
},
2326
});
2427

2528
const userCookbooks = data?.user?.cookbooks || [];

src/renderer/pages/FavoriteSnippets.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export default function MySnippets() {
2020
...GET_USER_SUBSCRIBED_RECIPES_VARIABLES,
2121
name: filters.searchTerm,
2222
},
23+
context: {
24+
debounceKey: 'favorite-snippets',
25+
},
2326
});
2427

2528
const userFavoriteRecipes = data?.user?.recipes || [];

src/renderer/pages/Home.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export default function Home() {
3030
onlyPublic: filters.privacy === 'public' ? true : null,
3131
onlySubscribed: filters.isSubscribed || null,
3232
},
33+
context: {
34+
debounceKey: 'search',
35+
},
3336
});
3437

3538
const results = data?.searchResults || [];

src/renderer/pages/MyCookbooks.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export default function MyCookbooks() {
2020
...GET_USER_COOKBOOKS_VARIABLES,
2121
name: filters.searchTerm,
2222
},
23+
context: {
24+
debounceKey: 'my-cookbooks',
25+
},
2326
});
2427

2528
const userCookbooks = data?.user?.cookbooks || [];

src/renderer/pages/MySnippets.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export default function MySnippets() {
2424
filters.language !== Language.ALL_LANGUAGES ? filters.language : null,
2525
tag: filters.tags,
2626
},
27+
context: {
28+
debounceKey: 'my-snippets',
29+
},
2730
});
2831

2932
const userRecipes = data?.user?.recipes || [];

src/renderer/pages/TeamCookbooks.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export default function TeamCookbooks() {
2020
...GET_SHARED_COOKBOOKS_VARIABLES,
2121
name: filters.searchTerm,
2222
},
23+
context: {
24+
debounceKey: 'team-cookbooks',
25+
},
2326
});
2427

2528
const userCookbooks = data?.cookbooks || [];

src/renderer/pages/TeamSnippets.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export default function TeamSnippets() {
2424
filters.language !== Language.ALL_LANGUAGES ? [filters.language] : null,
2525
tag: filters.tags,
2626
},
27+
context: {
28+
debounceKey: 'team-snippets',
29+
},
2730
});
2831

2932
const teamRecipes = data?.recipes || [];

0 commit comments

Comments
 (0)