@@ -21,7 +21,8 @@ pub struct MapModel {
21
21
22
22
// TODO Wasteful, can share some
23
23
pub router_original : Router ,
24
- pub router_current : Router ,
24
+ // Calculated lazily
25
+ pub router_current : Option < Router > ,
25
26
26
27
// TODO Keep edits / state here or not?
27
28
pub modal_filters : BTreeMap < RoadID , ModalFilter > ,
@@ -138,7 +139,8 @@ impl MapModel {
138
139
}
139
140
140
141
fn after_edited ( & mut self ) {
141
- self . router_current = Router :: new ( & self . roads , & self . intersections , & self . modal_filters ) ;
142
+ // Invalidate it
143
+ self . router_current = None ;
142
144
}
143
145
144
146
pub fn add_many_modal_filters (
@@ -308,14 +310,23 @@ impl MapModel {
308
310
Ok ( ( ) )
309
311
}
310
312
311
- pub fn compare_route ( & self , pt1 : Coord , pt2 : Coord ) -> GeoJson {
313
+ // Lazily builds the router if needed.
314
+ pub fn compare_route ( & mut self , pt1 : Coord , pt2 : Coord ) -> GeoJson {
315
+ if self . router_current . is_none ( ) {
316
+ self . router_current = Some ( Router :: new (
317
+ & self . roads ,
318
+ & self . intersections ,
319
+ & self . modal_filters ,
320
+ ) ) ;
321
+ }
322
+
312
323
let mut features = Vec :: new ( ) ;
313
324
if let Some ( linestring) = self . router_original . route ( self , pt1, pt2) {
314
325
let mut f = Feature :: from ( Geometry :: from ( & self . mercator . to_wgs84 ( & linestring) ) ) ;
315
326
f. set_property ( "kind" , "before" ) ;
316
327
features. push ( f) ;
317
328
}
318
- if let Some ( linestring) = self . router_current . route ( self , pt1, pt2) {
329
+ if let Some ( linestring) = self . router_current . as_ref ( ) . unwrap ( ) . route ( self , pt1, pt2) {
319
330
let mut f = Feature :: from ( Geometry :: from ( & self . mercator . to_wgs84 ( & linestring) ) ) ;
320
331
f. set_property ( "kind" , "after" ) ;
321
332
features. push ( f) ;
0 commit comments