-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support network request aborting and refetching (#34)
* feat: support network request aborting * fix: also include alert() in DataQuery renderprop * fix: don't expose imperative abort, refactor useDataQuery * feat: add ability to refetch data, upgrade react, tests * chore: run yarn autoclean for all type deps * chore: avoid Object Rest Spread in reduce callback * docs: update README to reference required polyfills
- Loading branch information
Showing
10 changed files
with
196 additions
and
59 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
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 @@ | ||
@types/**/node_modules |
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
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 |
---|---|---|
@@ -1,29 +1,62 @@ | ||
import { useState, useContext, useEffect } from 'react' | ||
import { useState, useContext, useEffect, useCallback } from 'react' | ||
import { DataContext } from '../components/DataContext' | ||
import { QueryState, QueryMap } from '../types/Query' | ||
import { | ||
QueryState, | ||
QueryMap, | ||
RefetchCallback, | ||
QueryRenderInput, | ||
} from '../types/Query' | ||
import { ContextType } from '../types/Context' | ||
|
||
export const useDataQuery = (query: QueryMap): QueryState => { | ||
const reduceResponses = (responses: any[], names: string[]) => | ||
responses.reduce((out, response, idx) => { | ||
out[names[idx]] = response | ||
}, {}) | ||
|
||
const fetchData = ( | ||
context: ContextType, | ||
query: QueryMap, | ||
signal: AbortSignal | ||
) => { | ||
const names = Object.keys(query) | ||
const requests = names.map(name => query[name]) | ||
|
||
const context = useContext(DataContext) | ||
const requestPromises = requests.map(q => | ||
context.fetch(q, { | ||
signal: signal, | ||
}) | ||
) | ||
|
||
return Promise.all(requestPromises).then(responses => | ||
reduceResponses(responses, names) | ||
) | ||
} | ||
|
||
export const useDataQuery = (query: QueryMap): QueryRenderInput => { | ||
const context = useContext(DataContext) | ||
const [state, setState] = useState<QueryState>({ loading: true }) | ||
const [refetchCount, setRefetchCount] = useState(0) | ||
const refetch: RefetchCallback = useCallback( | ||
() => setRefetchCount(count => count + 1), | ||
[] | ||
) | ||
|
||
useEffect(() => { | ||
Promise.all(requests.map(q => context.fetch(q))) | ||
.then(responses => | ||
responses.reduce( | ||
(out, response, idx) => ({ | ||
...out, | ||
[names[idx]]: response, | ||
}), | ||
[] | ||
) | ||
) | ||
.then(data => setState({ loading: false, data })) | ||
.catch(error => setState({ loading: false, error })) | ||
}, []) | ||
|
||
return state | ||
const controller = new AbortController() | ||
const abort = () => controller.abort() | ||
|
||
fetchData(context, query, controller.signal) | ||
.then(data => { | ||
!controller.signal.aborted && setState({ loading: false, data }) | ||
}) | ||
.catch(error => { | ||
!controller.signal.aborted && | ||
setState({ loading: false, error }) | ||
}) | ||
|
||
// Cleanup inflight requests | ||
return abort | ||
}, [context, query, refetchCount]) | ||
|
||
return { refetch, ...state } | ||
} |
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
Oops, something went wrong.