From d1ecaf75277be7bfdaa7e14f70539acb9e3367a6 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Mon, 25 Oct 2021 21:08:44 +0200 Subject: [PATCH] Force cache clearing when a new point is added, fix #522 --- .../Actors/SaveTileToLocalStorageActor.ts | 26 ++++++++++++++++--- Logic/FeatureSource/FeaturePipeline.ts | 12 +++++++-- .../TiledFromLocalStorageSource.ts | 1 - 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Logic/FeatureSource/Actors/SaveTileToLocalStorageActor.ts b/Logic/FeatureSource/Actors/SaveTileToLocalStorageActor.ts index 092c1a402..83a269b89 100644 --- a/Logic/FeatureSource/Actors/SaveTileToLocalStorageActor.ts +++ b/Logic/FeatureSource/Actors/SaveTileToLocalStorageActor.ts @@ -4,13 +4,14 @@ * Technically, more an Actor then a featuresource, but it fits more neatly this ay */ import {FeatureSourceForLayer} from "../FeatureSource"; +import {Tiles} from "../../../Models/TileRange"; export default class SaveTileToLocalStorageActor { public static readonly storageKey: string = "cached-features"; public static readonly formatVersion: string = "2" constructor(source: FeatureSourceForLayer, tileIndex: number) { - + source.features.addCallbackAndRunD(features => { const key = `${SaveTileToLocalStorageActor.storageKey}-${source.layer.layerDef.id}-${tileIndex}` const now = new Date() @@ -28,13 +29,30 @@ export default class SaveTileToLocalStorageActor { } - public static MarkVisited(layerId: string, tileId: number, freshness: Date){ + public static MarkVisited(layerId: string, tileId: number, freshness: Date) { const key = `${SaveTileToLocalStorageActor.storageKey}-${layerId}-${tileId}` - try{ + try { localStorage.setItem(key + "-time", JSON.stringify(freshness.getTime())) localStorage.setItem(key + "-format", SaveTileToLocalStorageActor.formatVersion) - }catch(e){ + } catch (e) { console.error("Could not mark tile ", key, "as visited") } } + + public static poison(layers: string[], lon: number, lat: number) { + for (let z = 0; z < 25; z++) { + + const {x, y} = Tiles.embedded_tile(lat, lon, z) + const tileId = Tiles.tile_index(z, x, y) + + for (const layerId of layers) { + + const key = `${SaveTileToLocalStorageActor.storageKey}-${layerId}-${tileId}` + localStorage.removeItem(key + "-time"); + localStorage.removeItem(key + "-format") + localStorage.removeItem(key) + } + } + + } } \ No newline at end of file diff --git a/Logic/FeatureSource/FeaturePipeline.ts b/Logic/FeatureSource/FeaturePipeline.ts index a96f1e278..bcda3f688 100644 --- a/Logic/FeatureSource/FeaturePipeline.ts +++ b/Logic/FeatureSource/FeaturePipeline.ts @@ -91,14 +91,22 @@ export default class FeaturePipeline { const self = this const expiryInSeconds = Math.min(...state.layoutToUse.layers.map(l => l.maxAgeOfCache)) - this.oldestAllowedDate = new Date(new Date().getTime() - expiryInSeconds); for (const layer of state.layoutToUse.layers) { TiledFromLocalStorageSource.cleanCacheForLayer(layer) } + this.oldestAllowedDate = new Date(new Date().getTime() - expiryInSeconds); this.osmSourceZoomLevel = state.osmApiTileSize.data; - // milliseconds const useOsmApi = state.locationControl.map(l => l.zoom > (state.overpassMaxZoom.data ?? 12)) this.relationTracker = new RelationsTracker() + + state.changes.allChanges.addCallbackAndRun(allChanges => { + allChanges.filter(ch => ch.id < 0) + .map(ch => ch.changes) + .filter(coor => coor["lat"] !== undefined && coor["lon"] !== undefined) + .forEach(coor => { + SaveTileToLocalStorageActor.poison(state.layoutToUse.layers.map(l => l.id), coor["lon"], coor["lat"]) + }) + }) this.sufficientlyZoomed = state.locationControl.map(location => { diff --git a/Logic/FeatureSource/TiledFeatureSource/TiledFromLocalStorageSource.ts b/Logic/FeatureSource/TiledFeatureSource/TiledFromLocalStorageSource.ts index 7f69fedc4..900393c1e 100644 --- a/Logic/FeatureSource/TiledFeatureSource/TiledFromLocalStorageSource.ts +++ b/Logic/FeatureSource/TiledFeatureSource/TiledFromLocalStorageSource.ts @@ -47,7 +47,6 @@ export default class TiledFromLocalStorageSource implements TileHierarchy