forked from MapComplete/MapComplete
		
	More refactoring
This commit is contained in:
		
							parent
							
								
									6890c5189e
								
							
						
					
					
						commit
						c2d477c97a
					
				
					 12 changed files with 91 additions and 73 deletions
				
			
		|  | @ -62,11 +62,35 @@ export default class FeaturePipeline { | |||
|         /** | ||||
|          * Maps tileid onto last download moment | ||||
|          */ | ||||
|         const tileFreshnesses = new Map<number, Date>() | ||||
|         const tileFreshnesses = new UIEventSource<Map<number, Date>>(new Map<number, Date>()) | ||||
|         const osmSourceZoomLevel = 14 | ||||
|         const useOsmApi = state.locationControl.map(l => l.zoom > (state.overpassMaxZoom.data ?? 12)) | ||||
|         this.relationTracker = new RelationsTracker() | ||||
| 
 | ||||
|         console.log("Tilefreshnesses are", tileFreshnesses.data) | ||||
|         const oldestAllowedDate = new Date(new Date().getTime() - (60 * 60 * 24 * 30 * 1000)); | ||||
|         const neededTilesFromOsm = state.currentBounds.map(bbox => { | ||||
|             if (bbox === undefined) { | ||||
|                 return | ||||
|             } | ||||
|             const range = bbox.containingTileRange(osmSourceZoomLevel) | ||||
|             const tileIndexes = [] | ||||
|             if (range.total > 100) { | ||||
|                 // Too much tiles!
 | ||||
|                 return [] | ||||
|             } | ||||
|             Tiles.MapRange(range, (x, y) => { | ||||
|                 const i = Tiles.tile_index(osmSourceZoomLevel, x, y); | ||||
|                 if (tileFreshnesses.data.get(i) > oldestAllowedDate) { | ||||
|                     console.debug("Skipping tile", osmSourceZoomLevel, x, y, "as a decently fresh one is available") | ||||
|                     // The cached tiles contain decently fresh data
 | ||||
|                     return; | ||||
|                 } | ||||
|                 tileIndexes.push(i) | ||||
|             }) | ||||
|             return tileIndexes | ||||
|         }, [tileFreshnesses]) | ||||
|          | ||||
|         const updater = new OverpassFeatureSource(state, | ||||
|             { | ||||
|                 relationTracker: this.relationTracker, | ||||
|  | @ -75,8 +99,9 @@ export default class FeaturePipeline { | |||
|                     // This callback contains metadata of the overpass call
 | ||||
|                     const range = bbox.containingTileRange(osmSourceZoomLevel) | ||||
|                     Tiles.MapRange(range, (x, y) => { | ||||
|                         tileFreshnesses.set(Tiles.tile_index(osmSourceZoomLevel, x, y), freshness) | ||||
|                         tileFreshnesses.data.set(Tiles.tile_index(osmSourceZoomLevel, x, y), freshness) | ||||
|                     }) | ||||
|                     tileFreshnesses.ping(); | ||||
| 
 | ||||
|                 } | ||||
|             }); | ||||
|  | @ -137,17 +162,17 @@ export default class FeaturePipeline { | |||
|                     }, state) | ||||
| 
 | ||||
|                 localStorage.tileFreshness.forEach((value, key) => { | ||||
|                     if (tileFreshnesses.has(key)) { | ||||
|                         const previous = tileFreshnesses.get(key) | ||||
|                     if (tileFreshnesses.data.has(key)) { | ||||
|                         const previous = tileFreshnesses.data.get(key) | ||||
|                         if (value < previous) { | ||||
|                             tileFreshnesses.set(key, value) | ||||
|                             tileFreshnesses.data.set(key, value) | ||||
|                         } | ||||
|                     } else { | ||||
|                         tileFreshnesses.set(key, value) | ||||
|                         tileFreshnesses.data.set(key, value) | ||||
|                     } | ||||
|                     tileFreshnesses.ping() | ||||
|                 }) | ||||
| 
 | ||||
| 
 | ||||
|                 continue | ||||
|             } | ||||
| 
 | ||||
|  | @ -178,30 +203,7 @@ export default class FeaturePipeline { | |||
|             } | ||||
|         } | ||||
| 
 | ||||
|         console.log("Tilefreshnesses are", tileFreshnesses) | ||||
|         const oldestAllowedDate = new Date(new Date().getTime() - (60 * 60 * 24 * 30 * 1000)); | ||||
| 
 | ||||
|         const neededTilesFromOsm = state.currentBounds.map(bbox => { | ||||
|             if (bbox === undefined) { | ||||
|                 return | ||||
|             } | ||||
|             const range = bbox.containingTileRange(osmSourceZoomLevel) | ||||
|             const tileIndexes = [] | ||||
|             if (range.total > 100) { | ||||
|                 // Too much tiles!
 | ||||
|                 return [] | ||||
|             } | ||||
|             Tiles.MapRange(range, (x, y) => { | ||||
|                 const i = Tiles.tile_index(osmSourceZoomLevel, x, y); | ||||
|                 if (tileFreshnesses.get(i) > oldestAllowedDate) { | ||||
|                     console.debug("Skipping tile", osmSourceZoomLevel, x, y, "as a decently fresh one is available") | ||||
|                     // The cached tiles contain decently fresh data
 | ||||
|                     return; | ||||
|                 } | ||||
|                 tileIndexes.push(i) | ||||
|             }) | ||||
|             return tileIndexes | ||||
|         }) | ||||
|          | ||||
| 
 | ||||
|        const osmFeatureSource = new OsmFeatureSource({ | ||||
|             isActive: useOsmApi, | ||||
|  |  | |||
|  | @ -28,8 +28,9 @@ export default class FilteringFeatureSource implements FeatureSourceForLayer, Ti | |||
| 
 | ||||
|         this.layer = upstream.layer; | ||||
|         const layer = upstream.layer; | ||||
| 
 | ||||
|          | ||||
|         function update() { | ||||
|              | ||||
|             const features: { feature: any; freshness: Date }[] = upstream.features.data; | ||||
|             const newFeatures = features.filter((f) => { | ||||
|                 if ( | ||||
|  | @ -60,11 +61,6 @@ export default class FilteringFeatureSource implements FeatureSourceForLayer, Ti | |||
|                 } | ||||
| 
 | ||||
| 
 | ||||
|                 if (!layer.isDisplayed) { | ||||
|                     // The layer itself is either disabled or hidden due to zoom constraints
 | ||||
|                     // We should return true, but it might still match some other layer
 | ||||
|                     return false; | ||||
|                 } | ||||
|                 return true; | ||||
|             }); | ||||
| 
 | ||||
|  | @ -75,20 +71,8 @@ export default class FilteringFeatureSource implements FeatureSourceForLayer, Ti | |||
|             update(); | ||||
|         }); | ||||
| 
 | ||||
|         layer.isDisplayed.addCallback(isShown => { | ||||
|             if (isShown) { | ||||
|                 update(); | ||||
|             } else { | ||||
|                 self.features.setData([]) | ||||
|             } | ||||
|         }); | ||||
| 
 | ||||
|         layer.appliedFilters.addCallback(_ => { | ||||
|             if (!layer.isDisplayed.data) { | ||||
|                 // Currently not shown.
 | ||||
|                 // Note that a change in 'isSHown' will trigger an update as well, so we don't have to watch it another time
 | ||||
|                 return; | ||||
|             } | ||||
|             update() | ||||
|         }) | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue