I have an Observable that makes a request that gives back some information that is different each time. This observable is called by a user clicking a button. I want to prevent the user from clicking this button before the request has returned with information, and once it has returned, allow the user to click for an update once again.
This is the request without stopping the user at any point.
const fetchRepos = () =>
fetch( 'https://api.github.com/search/repositories?q=+language:javascript&sort=updated&order=desc')
.then(response => response.json())
export function fetchReposEpic(action$: any) {
return action$.ofType(FETCH_POPULAR_REPOS_REQUEST)
.switchMap(fetchRepos)
.map(data => ({
type: FETCH_POPULAR_REPOS_SUCCESS,
payload: data.items
}))
}
One solution is to throttle the user for 500 milliseconds after the Observable begins
export function fetchReposEpic(action$: any) {
return action$.ofType(FETCH_POPULAR_REPOS_REQUEST)
.throttleTime(500)
.switchMap(fetchRepos)
.map(data => ({
type: FETCH_POPULAR_REPOS_SUCCESS,
payload: data.items
}))
}
The problem here is I am only estimating how long the request might take, worse still, if the request finishes before this time, the user could click the button and the click would be ignored.
The closest to a solution I have is passing the fetch promise to throttle.
export function fetchReposEpic(action$: any) {
return action$.ofType(FETCH_POPULAR_REPOS_REQUEST)
.throttle(fetchRepos)
.map(data => ({
type: FETCH_POPULAR_REPOS_SUCCESS,
payload: []
}))
}
This will wait until the request has finished and will throttle all other actions until then. The problem is that I do not know how to get the returned value of throttle. All throttle examples I could find also show the returned value of throttle being thrown away.
So to summarize, I want a way to wait until the observable has completed before firing it off again.