Created
June 29, 2015 04:24
-
-
Save JosephBlythe/135adbfbde5beef2082c to your computer and use it in GitHub Desktop.
ReactFetcher.js
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
// https://markdalgleish.github.io/presentation-dawn-of-the-progressive-single-page-app | |
import fetchData from './lib/fetchData'; | |
import Fetch from './lib/Fetch'; | |
export default { | |
fetchData, | |
Fetch | |
}; | |
// fetchData.js | |
import Promise from 'bluebird'; | |
export default (routes, ...args) => { | |
const promises = routes | |
// Get route fetchers | |
.map(route => route.handler.fetchers) | |
// Filter out missing fetchers | |
.filter(fetchers => fetchers) | |
// Ensure fetchers are in an array | |
.map(fetchers => Array.isArray(fetchers) ? fetchers : [fetchers]) | |
// Generate an array of fetcher promises | |
.map(fetchers => Promise.all(fetchers.map(fetcher => fetcher(...args)))); | |
return Promise.all(promises); | |
}; | |
// Fetch.js | |
import React from 'react'; | |
export default (fetchers) => (ComposedComponent) => class extends React.Component { | |
static fetchers = fetchers; | |
render() { | |
return <ComposedComponent {...this.props} /> | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment