Skip to content
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

Add Promise support to async route functions #3719

Merged
merged 7 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/PromiseUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isPromise(obj) {
return obj && typeof obj.then === 'function'
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno if this is worth throwing in a module. Maybe move it to AsyncUtils at least?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay for now I think.

17 changes: 17 additions & 0 deletions modules/__tests__/Router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,23 @@ describe('Router', function () {
})
})

it('should support getComponent returning a Promise', function (done) {
const Component = () => <div />

const getComponent = () => new Promise(resolve => resolve(Component))

render((
<Router history={createHistory('/')} render={renderSpy}>
<Route path="/" getComponent={getComponent} />
</Router>
), node, function () {
setTimeout(function () {
expect(componentSpy).toHaveBeenCalledWith([ Component ])
done()
})
})
})

})

describe('error handling', function () {
Expand Down
28 changes: 28 additions & 0 deletions modules/__tests__/matchRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,34 @@ describe('matchRoutes', function () {
describeRoutes()
})

describe('a Promise-based route config', function () {
function makeAsyncRouteConfig(routes) {
routes.forEach(function (route) {
const { childRoutes, indexRoute } = route

if (childRoutes) {
delete route.childRoutes

route.getChildRoutes = () => new Promise(resolve => resolve(childRoutes))

makeAsyncRouteConfig(childRoutes)
}

if (indexRoute) {
delete route.indexRoute

route.getIndexRoute = () => new Promise(resolve => resolve(indexRoute))
}
})
}

beforeEach(function () {
makeAsyncRouteConfig(routes)
})

describeRoutes()
})

describe('an asynchronous JSX route config', function () {
let getChildRoutes, getIndexRoute, jsxRoutes

Expand Down
9 changes: 8 additions & 1 deletion modules/getComponents.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mapAsync } from './AsyncUtils'
import { isPromise } from './PromiseUtils'

function getComponentsForRoute(nextState, route, callback) {
if (route.component || route.components) {
Expand All @@ -8,7 +9,13 @@ function getComponentsForRoute(nextState, route, callback) {

const getComponent = route.getComponent || route.getComponents
if (getComponent) {
getComponent.call(route, nextState, callback)
const componentReturn = getComponent.call(route, nextState, callback)
if (isPromise(componentReturn))
componentReturn
.then(
component => callback(null, component),
callback
)
} else {
callback()
}
Expand Down
19 changes: 17 additions & 2 deletions modules/matchRoutes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { loopAsync } from './AsyncUtils'
import { isPromise } from './PromiseUtils'
import { matchPattern } from './PatternUtils'
import warning from './routerWarning'
import { createRoutes } from './RouteUtils'
Expand All @@ -18,7 +19,7 @@ function getChildRoutes(route, location, paramNames, paramValues, callback) {
params: createParams(paramNames, paramValues)
}

route.getChildRoutes(partialNextState, (error, childRoutes) => {
const childRoutesReturn = route.getChildRoutes(partialNextState, (error, childRoutes) => {
childRoutes = !error && createRoutes(childRoutes)
if (sync) {
result = [ error, childRoutes ]
Expand All @@ -28,6 +29,13 @@ function getChildRoutes(route, location, paramNames, paramValues, callback) {
callback(error, childRoutes)
})

if (isPromise(childRoutesReturn))
childRoutesReturn
.then(
childRoutes => callback(null, createRoutes(childRoutes)),
callback
)

sync = false
return result // Might be undefined.
}
Expand All @@ -41,9 +49,16 @@ function getIndexRoute(route, location, paramNames, paramValues, callback) {
params: createParams(paramNames, paramValues)
}

route.getIndexRoute(partialNextState, (error, indexRoute) => {
const indexRoutesReturn = route.getIndexRoute(partialNextState, (error, indexRoute) => {
callback(error, !error && createRoutes(indexRoute)[0])
})

if (isPromise(indexRoutesReturn))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what we should do in cases like this where the function returns a non-promise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably what we do right now: Nothing. We can treat them as thunks, but why would anyone realistically do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually used that pattern to return a slightly different component resolved synchronously based on the query. I just invoked the cb synchronously though. Heh.

indexRoutesReturn
.then(
indexRoute => callback(null, createRoutes(indexRoute)[0]),
callback
)
} else if (route.childRoutes) {
const pathless = route.childRoutes.filter(childRoute => !childRoute.path)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-istanbul": "^1.0.3",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-1": "^6.13.0",
Expand Down
1 change: 1 addition & 0 deletions tests.webpack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env mocha */

import 'babel-polyfill'
import expect from 'expect'

import { _resetWarned } from './modules/routerWarning'
Expand Down