forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetComponents.js
More file actions
37 lines (33 loc) · 1.04 KB
/
getComponents.js
File metadata and controls
37 lines (33 loc) · 1.04 KB
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
36
37
import { mapAsync } from './AsyncUtils'
import { isPromise } from './PromiseUtils'
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) {
const componentReturn = getComponent.call(route, nextState, callback)
if (isPromise(componentReturn))
componentReturn
.then(
component => callback(null, component),
callback
)
} else {
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