Skip to content

Instantly share code, notes, and snippets.

@JosephBlythe
Created June 29, 2015 04:24
Show Gist options
  • Save JosephBlythe/135adbfbde5beef2082c to your computer and use it in GitHub Desktop.
Save JosephBlythe/135adbfbde5beef2082c to your computer and use it in GitHub Desktop.
ReactFetcher.js
// 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