-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathresolver.js
94 lines (80 loc) · 2.57 KB
/
resolver.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import isPromise from 'is-promise';
import {
accumulateRouteValues,
checkResolved,
getComponents,
getRouteMatches,
getRouteValue,
isResolved,
} from './ResolverUtils';
import createElements from './createElements';
function getRouteGetData(route) {
return route.getData;
}
function getRouteData(route) {
return route.data;
}
export default {
async *resolveElements(match) {
const routeMatches = getRouteMatches(match);
const Components = getComponents(routeMatches);
const data = this.getData(match, routeMatches);
const earlyComponents = Components.some(isPromise)
? await Promise.all(Components.map(checkResolved))
: Components;
const earlyData = data.some(isPromise)
? await Promise.all(data.map(checkResolved))
: data;
let fetchedComponents;
let fetchedData;
if (!earlyComponents.every(isResolved) || !earlyData.every(isResolved)) {
const pendingElements = createElements(
routeMatches,
earlyComponents,
earlyData,
);
yield pendingElements.every((element) => element !== undefined)
? pendingElements
: undefined;
fetchedComponents = await Promise.all(Components);
fetchedData = await Promise.all(data);
} else {
fetchedComponents = earlyComponents;
fetchedData = earlyData;
}
yield createElements(routeMatches, fetchedComponents, fetchedData);
},
/**
* Generate route data according to their getters, respecting the order of
* promises per the `defer` flag on routes.
*/
getData(match, routeMatches) {
return accumulateRouteValues(
routeMatches,
match.routeIndices,
({ ancestorRouteData, prevParentPromise }, routeMatch) => {
// For a deferred route, the parent promise is the previous promise.
// Otherwise, it's the previous parent promise.
const parentPromise = routeMatch.route.defer
? Promise.all(ancestorRouteData)
: prevParentPromise;
// If there is a parent promise, execute after it resolves.
const routeData = parentPromise
? parentPromise.then(() =>
getRouteValue(routeMatch, getRouteGetData, getRouteData),
)
: getRouteValue(routeMatch, getRouteGetData, getRouteData);
return {
routeData,
ancestorRouteData: [...ancestorRouteData, routeData],
prevParentPromise: parentPromise,
};
},
{
routeData: null,
ancestorRouteData: [],
prevParentPromise: null,
},
).map(({ routeData }) => routeData);
},
};