forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetComponents.js
35 lines (29 loc) · 1.01 KB
/
getComponents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { mapAsync } from './AsyncUtils'
import makeStateWithLocation from './makeStateWithLocation'
function getComponentsForRoute(nextState, route, callback) {
if (route.component || route.components) {
callback(null, route.component || route.components)
return
}
const getComponent = route.getComponent || route.getComponents
if (!getComponent) {
callback()
return
}
const { location } = nextState
const nextStateWithLocation = makeStateWithLocation(nextState, location)
getComponent.call(route, nextStateWithLocation, callback)
}
/**
* Asynchronously fetches all components needed for the given router
* state and calls callback(error, components) when finished.
*
* Note: This operation may finish synchronously if no routes have an
* asynchronous getComponents method.
*/
function getComponents(nextState, callback) {
mapAsync(nextState.routes, function (route, index, callback) {
getComponentsForRoute(nextState, route, callback)
}, callback)
}
export default getComponents