Skip to content

Commit 0f44002

Browse files
feat(loading): Show intermediate states while application is loading.
1 parent de83a96 commit 0f44002

File tree

13 files changed

+787
-611
lines changed

13 files changed

+787
-611
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"prestart": "yarn",
88
"pretest": "yarn",
99
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
10-
"start": "mastarm build",
10+
"start": "mastarm build --serve --env development",
1111
"test": "mastarm lint"
1212
},
1313
"repository": {
@@ -25,7 +25,7 @@
2525
},
2626
"homepage": "https://github.com/conveyal/taui",
2727
"devDependencies": {
28-
"mastarm": "^3.4.0",
28+
"mastarm": "^3.5.1",
2929
"semantic-release": "^6.3.2"
3030
},
3131
"dependencies": {

src/actions/browsochrones.js

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,104 @@
11
import lonlat from '@conveyal/lonlat'
2+
import {
3+
decrementFetches,
4+
incrementFetches
5+
} from '@conveyal/woonerf/fetch'
26
import Browsochrones from 'browsochrones'
37
import fetch from 'isomorphic-fetch'
48

59
import {getAsObject as getHash} from '../utils/hash'
610
import {geocode} from '../utils/mapbox-geocoder'
711
import messages from '../utils/messages'
812

9-
import {addActionLogItem, setBrowsochronesInstances, setDestination, updateOrigin} from '../actions'
13+
import {
14+
addActionLogItem,
15+
fetchAllBrowsochrones,
16+
setAccessibilityFor,
17+
setBrowsochronesInstances,
18+
setEnd,
19+
setEndLabel,
20+
setStart,
21+
setStartLabel
22+
} from '../actions'
1023

11-
export default async function initialize ({
24+
export default function initialize ({
1225
browsochrones,
1326
geocoder,
1427
map
1528
}) {
16-
const actions = []
17-
const {grids, gridsUrl, origins} = browsochrones
18-
const fetchGrids = grids.map(async (name) => {
29+
const {origins} = browsochrones
30+
const qs = getHash()
31+
return [
32+
incrementFetches(),
33+
setStartLabel(qs.start), // may not exist
34+
setEndLabel(qs.end), // may not exist
35+
...browsochrones.origins
36+
.map((_, index) =>
37+
setAccessibilityFor({accessibility: -1, index})),
38+
geocodeQs({geocoder, qs})
39+
.then(([start, end]) => {
40+
const actions = []
41+
if (start) actions.push(setStart(start))
42+
if (end) actions.push(setEnd(end))
43+
actions.push(fetchGrids(browsochrones)
44+
.then((grids) => loadAllOrigins({grids, origins}))
45+
.then((instances) => [
46+
setBrowsochronesInstances(instances),
47+
start && fetchAllBrowsochrones({
48+
browsochronesInstances: instances,
49+
endLatlng: end && end.latlng,
50+
latlng: start.latlng,
51+
zoom: map.zoom
52+
}),
53+
addActionLogItem(messages.Strings.ApplicationReady),
54+
decrementFetches()
55+
])
56+
)
57+
return actions
58+
})
59+
]
60+
}
61+
62+
function geocodeQs ({
63+
geocoder,
64+
qs
65+
}) {
66+
return Promise
67+
.all(['start', 'end']
68+
.map(async (p) => {
69+
if (qs[p]) {
70+
const results = await geocode({
71+
boundary: geocoder.boundary,
72+
focusLatlng: geocoder.focusLatlng,
73+
text: qs[p]
74+
})
75+
if (results.features.length > 0) {
76+
return {
77+
label: results.features[0].place_name,
78+
latlng: lonlat(results.features[0].geometry.coordinates)
79+
}
80+
}
81+
}
82+
}))
83+
}
84+
85+
function fetchGrids ({
86+
grids,
87+
gridsUrl
88+
}) {
89+
return Promise.all(grids.map(async (name) => {
1990
const res = await fetch(`${gridsUrl}/${name}.grid`)
2091
const grid = await res.arrayBuffer()
2192
grid.name = name
2293
return grid
23-
})
24-
const fetchedGrids = await Promise.all(fetchGrids)
25-
26-
const instances = await Promise.all(origins.map((origin) => load(origin, fetchedGrids)))
27-
actions.push(setBrowsochronesInstances(instances))
28-
29-
const qs = getHash()
30-
if (qs.start) {
31-
actions.push(...(await loadFromQueryString({
32-
instances,
33-
geocoder,
34-
map,
35-
qs
36-
})))
37-
}
94+
}))
95+
}
3896

39-
return [...actions, addActionLogItem(messages.Strings.ApplicationReady)]
97+
function loadAllOrigins ({
98+
grids,
99+
origins
100+
}) {
101+
return Promise.all(origins.map((origin) => load(origin, grids)))
40102
}
41103

42104
async function load (url, grids) {
@@ -57,38 +119,3 @@ async function load (url, grids) {
57119

58120
return bs
59121
}
60-
61-
async function loadFromQueryString ({
62-
instances,
63-
geocoder,
64-
map,
65-
qs
66-
}) {
67-
try {
68-
const [startResults, endResults] = await Promise.all(['start', 'end']
69-
.filter((d) => !!qs[d])
70-
.map((d) => geocode({
71-
boundary: geocoder.boundary,
72-
focusLatlng: geocoder.focusLatlng,
73-
text: qs[d]
74-
})))
75-
if (startResults.features.length > 0) {
76-
const destination = endResults && endResults.features.length > 0
77-
? { latlng: lonlat(endResults.features[0].geometry.coordinates), label: endResults.features[0].place_name }
78-
: {}
79-
return [
80-
updateOrigin({
81-
browsochronesInstances: instances,
82-
label: startResults.features[0].place_name,
83-
destinationLatlng: destination.latlng,
84-
latlng: lonlat(startResults.features[0].geometry.coordinates),
85-
zoom: map.zoom
86-
}),
87-
setDestination(destination)
88-
]
89-
}
90-
} catch (e) {
91-
console.error(e)
92-
return []
93-
}
94-
}

0 commit comments

Comments
 (0)