Skip to content

Commit ef8cb8f

Browse files
committed
Speed up mode switching by recalculating cells/shortcuts at the appropriate time.
1 parent 5908a5c commit ef8cb8f

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

backend/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl LTN {
150150
&self.neighbourhood.as_ref().unwrap().interior_roads,
151151
FilterKind::from_string(&kind).unwrap(),
152152
);
153+
self.after_edit();
153154
Ok(())
154155
}
155156

@@ -165,19 +166,23 @@ impl LTN {
165166
&self.neighbourhood.as_ref().unwrap().interior_roads,
166167
FilterKind::from_string(&kind).unwrap(),
167168
);
169+
self.after_edit();
168170
Ok(())
169171
}
170172

171173
#[wasm_bindgen(js_name = deleteModalFilter)]
172174
pub fn delete_modal_filter(&mut self, road: usize) {
173175
self.map.delete_modal_filter(RoadID(road));
176+
self.after_edit();
174177
}
175178

176179
pub fn undo(&mut self) {
177180
self.map.undo();
181+
self.after_edit();
178182
}
179183
pub fn redo(&mut self) {
180184
self.map.redo();
185+
self.after_edit();
181186
}
182187

183188
#[wasm_bindgen(js_name = getShortcutsCrossingRoad)]
@@ -218,6 +223,14 @@ impl LTN {
218223
let pt2 = self.map.mercator.pt_to_mercator(Coord { x: x2, y: y2 });
219224
Ok(serde_json::to_string(&self.map.compare_route(pt1, pt2)).map_err(err_to_js)?)
220225
}
226+
227+
// TODO This is also internal to MapModel. But not sure who should own Neighbourhood or how to
228+
// plumb, so duplicting here.
229+
fn after_edit(&mut self) {
230+
if let Some(ref mut n) = self.neighbourhood {
231+
n.after_edit(&self.map);
232+
}
233+
}
221234
}
222235

223236
#[derive(Deserialize)]

backend/src/neighbourhood.rs

+38-13
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ use crate::render_cells::Color;
1010
use crate::{Cell, IntersectionID, MapModel, RenderCells, RoadID, Shortcuts};
1111

1212
pub struct Neighbourhood {
13+
// Immutable once created
1314
pub interior_roads: HashSet<RoadID>,
1415
crosses: HashMap<RoadID, f64>,
1516
pub border_intersections: HashSet<IntersectionID>,
1617
name: String,
1718
pub boundary_polygon: Polygon,
19+
20+
// Updated after mutations
21+
derived: Option<DerivedNeighbourhoodState>,
22+
}
23+
24+
struct DerivedNeighbourhoodState {
25+
render_cells: RenderCells,
26+
shortcuts: Shortcuts,
1827
}
1928

2029
impl Neighbourhood {
@@ -60,32 +69,47 @@ impl Neighbourhood {
6069
bail!("No roads inside the boundary");
6170
}
6271

63-
Ok(Self {
72+
let mut n = Self {
6473
interior_roads,
6574
crosses,
6675
border_intersections,
6776
name,
6877
boundary_polygon,
69-
})
78+
derived: None,
79+
};
80+
n.after_edit(map);
81+
Ok(n)
82+
}
83+
84+
pub fn after_edit(&mut self, map: &MapModel) {
85+
let cells = Cell::find_all(map, self);
86+
let render_cells = RenderCells::new(map, self, &cells);
87+
let shortcuts = Shortcuts::new(map, self);
88+
self.derived = Some(DerivedNeighbourhoodState {
89+
render_cells,
90+
shortcuts,
91+
});
7092
}
7193

7294
pub fn to_gj(&self, map: &MapModel) -> FeatureCollection {
7395
let mut features = Vec::new();
7496

97+
let derived = self.derived.as_ref().unwrap();
98+
7599
// Just one boundary
76100
features.push(map.boundaries.get(&self.name).cloned().unwrap());
77101

78-
// TODO Decide how/where state lives
79-
let cells = Cell::find_all(map, self);
80-
let render_cells = RenderCells::new(map, self, &cells);
81-
let shortcuts = Shortcuts::new(map, self);
82-
83102
for r in &self.interior_roads {
84103
let mut f = map.get_r(*r).to_gj(&map.mercator);
85104
f.set_property("kind", "interior_road");
86105
f.set_property(
87106
"shortcuts",
88-
shortcuts.count_per_road.get(r).cloned().unwrap_or(0),
107+
derived
108+
.shortcuts
109+
.count_per_road
110+
.get(r)
111+
.cloned()
112+
.unwrap_or(0),
89113
);
90114
features.push(f);
91115
}
@@ -101,15 +125,16 @@ impl Neighbourhood {
101125
features.push(f);
102126
}
103127

104-
for (polygons, color) in render_cells
128+
for (polygons, color) in derived
129+
.render_cells
105130
.polygons_per_cell
106-
.into_iter()
107-
.zip(render_cells.colors)
131+
.iter()
132+
.zip(derived.render_cells.colors.iter())
108133
{
109-
let mut f = Feature::from(Geometry::from(&map.mercator.to_wgs84(&polygons)));
134+
let mut f = Feature::from(Geometry::from(&map.mercator.to_wgs84(polygons)));
110135
match color {
111136
Color::Disconnected => f.set_property("cell_color", "disconnected"),
112-
Color::Cell(idx) => f.set_property("cell_color", idx),
137+
Color::Cell(idx) => f.set_property("cell_color", *idx),
113138
}
114139
features.push(f);
115140
}

0 commit comments

Comments
 (0)