-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* avoid setting state same as previous * fire effect by query state change * move problematic fetch code to react-query like hook * memoize SearchBox
- Loading branch information
1 parent
606c1e4
commit 1f91cf0
Showing
9 changed files
with
140 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
export interface SearchClient<Entity> { | ||
search: ( | ||
query: string, | ||
abortController?: AbortController | ||
) => Promise<Entity[]>; | ||
search: (query: string, signal?: AbortSignal) => Promise<Entity[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { PromiseState } from "../state/usePromiseState"; | ||
import CancelablePromise from "cancelable-promise"; | ||
import { cancelableWithAbortController } from "../state/cancelablePromise"; | ||
|
||
export const useQuery = <Data>( | ||
getData: (signal: AbortSignal) => Promise<Data> | ||
) => { | ||
const [state, setState] = useState<PromiseState<Data>>({ | ||
type: "pending", | ||
}); | ||
const lastPromise = useRef<CancelablePromise | null>(null); | ||
const lastState = useRef(state); | ||
|
||
const reload = useCallback(async () => { | ||
const abortController = new AbortController(); | ||
lastPromise.current?.cancel(); | ||
lastState.current.type !== "pending" && setState({ type: "pending" }); // avoid extra render, read state by ref | ||
try { | ||
lastPromise.current = cancelableWithAbortController( | ||
abortController, | ||
getData(abortController.signal) | ||
); | ||
|
||
const fetchedResult = await lastPromise.current; | ||
setState({ type: "loaded", data: fetchedResult }); | ||
} catch (e) { | ||
if (e instanceof DOMException && e.name === "AbortError") { | ||
// ignore this error, we are already in loading state due to the new fetch | ||
} else { | ||
setState({ | ||
type: "error", | ||
message: String(e), | ||
}); | ||
} | ||
} | ||
}, [getData]); | ||
|
||
useEffect(() => { | ||
void reload(); | ||
|
||
return () => { | ||
lastPromise.current?.cancel(); | ||
}; | ||
}, [reload]); | ||
|
||
lastState.current = state; | ||
|
||
return { state, reload }; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 19 additions & 19 deletions
38
src/features/repositories/pages/search/useRepositorySearchProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
import { useEffect, useState } from "react"; | ||
import { PromiseState } from "../../../../common/state/usePromiseState"; | ||
import { useCallback, useState } from "react"; | ||
import { Repository } from "../../types"; | ||
import { SearchClient } from "../../../../common/api/clients/SearchClient"; | ||
import { useSearchClient } from "../../../../common/services/useSearchClient"; | ||
import { useQuery } from "../../../../common/services/useQuery"; | ||
|
||
export const useRepositorySearchProps = ( | ||
searchClient: SearchClient<Repository> | ||
) => { | ||
const [query, setQuery] = useState("react"); | ||
const [repositories, setRepositories] = useState<PromiseState<Repository[]>>({ | ||
type: "pending", | ||
}); | ||
|
||
const searchRepositories = useSearchClient( | ||
repositories, | ||
setRepositories, | ||
searchClient | ||
const getData = useCallback( | ||
(signal: AbortSignal) => searchClient.search(query, signal), | ||
[query, searchClient] | ||
); | ||
|
||
useEffect(() => { | ||
void searchRepositories.search(query); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
const repositories = useQuery(getData); | ||
|
||
const onQueryChange = useCallback( | ||
(newQuery: string) => { | ||
if (query === newQuery) { | ||
void repositories.reload(); | ||
} else { | ||
setQuery(newQuery); | ||
} | ||
}, | ||
[query, repositories] | ||
); | ||
|
||
return { | ||
query, | ||
repositories, | ||
onQueryChange: (query: string) => { | ||
setQuery(query); | ||
void searchRepositories.search(query); | ||
}, | ||
repositories: repositories.state, | ||
onQueryChange, | ||
}; | ||
}; |