Fix issue where FetchAllFluxServices did not return services on await

Co-authored-by: Jared Scheib <jared.scheib@gmail.com>
pull/4082/head
Alirie Gray 2018-08-02 13:44:35 -07:00
parent 10d24997d0
commit 82263fe3a1
1 changed files with 22 additions and 10 deletions

View File

@ -136,17 +136,27 @@ export type FetchAllFluxServicesAsync = (
export const fetchAllFluxServicesAsync: FetchAllFluxServicesAsync = sources => async (
dispatch
): Promise<void> => {
const allServices: Service[] = []
sources.forEach(async source => {
try {
const services = await getServicesAJAX(source.links.services)
const fluxServices = services.filter(s => s.type === 'flux')
allServices.push(...fluxServices)
} catch (err) {
dispatch(notify(couldNotGetServices))
const allServicesForSources: Array<Promise<Service[]>> = sources.map(
async source => {
try {
return getServicesAJAX(source.links.services)
} catch (err) {
dispatch(notify(couldNotGetServices))
}
}
})
dispatch(loadServices(allServices))
)
try {
const sourceServices = await Promise.all(allServicesForSources)
const flat = sourceServices.reduce((acc, cur) => {
return [...acc, ...cur.filter(s => s.type === 'flux')]
})
dispatch(loadServices(flat))
} catch (err) {
console.error(err.data)
dispatch(notify(couldNotGetServices))
throw err.data
}
}
export type FetchFluxServicesForSourceAsync = (
@ -160,7 +170,9 @@ export const fetchFluxServicesForSourceAsync: FetchFluxServicesForSourceAsync =
const fluxServices = services.filter(s => s.type === 'flux')
dispatch(loadServices(fluxServices))
} catch (err) {
console.error(err.data)
dispatch(notify(couldNotGetServices))
throw err.data
}
}