-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support network request aborting and refetching #34
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dc09d93
feat: support network request aborting
amcgee d09cb64
Merge branch 'master' into feat/abort
amcgee b01ae74
fix: also include alert() in DataQuery renderprop
amcgee 2a4ae6b
Merge remote-tracking branch 'origin/master' into feat/abort
amcgee 9c05fd0
fix: don't expose imperative abort, refactor useDataQuery
amcgee 940802f
feat: add ability to refetch data, upgrade react, tests
amcgee 63098f9
chore: run yarn autoclean for all type deps
amcgee 08d1e64
chore: avoid Object Rest Spread in reduce callback
amcgee a3ef41a
Merge branch 'master' into feat/abort
amcgee 4250f36
docs: update README to reference required polyfills
amcgee 9def5b1
Merge branch 'master' into feat/abort
amcgee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IE11 lacks support for the
AbortController
. From DHIS 2 version 2.33 we will drop support for IE 11, so most apps using this hook won't suffer from this problem. However, there may be some exceptions:My preferred solution would be to do the following:
Fetch
,AbortController
andAbortSignal
APIs which are unsupported in IE11This way each app can handle things as it sees fit.