From 06ac12ba2915a01a8d458b105e63e94f3e106c81 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Fri, 25 Feb 2022 14:50:17 +0000 Subject: [PATCH] Move tileUpdateInterval handling to LiveAtlasTileLayer --- src/components/map/layer/MapLayer.vue | 34 +++------------------ src/leaflet/tileLayer/LiveAtlasTileLayer.ts | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/components/map/layer/MapLayer.vue b/src/components/map/layer/MapLayer.vue index cd91a88..4568b30 100644 --- a/src/components/map/layer/MapLayer.vue +++ b/src/components/map/layer/MapLayer.vue @@ -41,47 +41,23 @@ export default defineComponent({ const store = useStore(), active = computed(() => props.map === store.state.currentMap); - let refreshTimeout: null | ReturnType = null, - layer: LiveAtlasTileLayer; - - const refresh = () => { - if(active.value) { - layer.refresh(); - } - - refreshTimeout = setTimeout(refresh, props.map.tileUpdateInterval); - }; + let layer: LiveAtlasTileLayer; layer = store.state.currentMapProvider!.createTileLayer({ errorTileUrl: 'images/blank.png', mapSettings: Object.freeze(JSON.parse(JSON.stringify(props.map))), }); - const enableLayer = () => { - props.leaflet.addLayer(layer); - }, + const enableLayer = () => props.leaflet.addLayer(layer), + disableLayer = () => layer.remove(); - disableLayer = () => { - layer.remove(); - }; - - watch(active, (newValue) => newValue ? enableLayer() : disableLayer()); + watch(active, newValue => newValue ? enableLayer() : disableLayer()); if(active.value) { enableLayer(); } - if(props.map.tileUpdateInterval) { - refreshTimeout = setTimeout(refresh, props.map.tileUpdateInterval); - } - - onUnmounted(() => { - disableLayer(); - - if(refreshTimeout) { - clearTimeout(refreshTimeout); - } - }); + onUnmounted(() => disableLayer()); }, render() { diff --git a/src/leaflet/tileLayer/LiveAtlasTileLayer.ts b/src/leaflet/tileLayer/LiveAtlasTileLayer.ts index 1ca1e73..3fd80c0 100644 --- a/src/leaflet/tileLayer/LiveAtlasTileLayer.ts +++ b/src/leaflet/tileLayer/LiveAtlasTileLayer.ts @@ -15,7 +15,7 @@ */ import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition"; -import {Coords, DomUtil, DoneCallback, TileLayer, TileLayerOptions, Util} from "leaflet"; +import {Map as LeafletMap, Coords, DomUtil, DoneCallback, TileLayer, TileLayerOptions, Util} from "leaflet"; import {LiveAtlasInternalTiles, LiveAtlasTileElement} from "@/index"; import falseFn = Util.falseFn; @@ -33,6 +33,7 @@ export abstract class LiveAtlasTileLayer extends TileLayer { private readonly tileTemplate: LiveAtlasTileElement; protected readonly loadQueue: LiveAtlasTileElement[] = []; private readonly loadingTiles: Set = Object.seal(new Set()); + private refreshTimeout?: ReturnType; private static genericLoadError = new Error('Tile failed to load'); @@ -60,10 +61,6 @@ export abstract class LiveAtlasTileLayer extends TileLayer { options.minZoom = 0; Util.setOptions(this, options); - - if (options.mapSettings === null) { - throw new TypeError("mapSettings missing"); - } } // @method createTile(coords: Object, done?: Function): HTMLElement @@ -210,4 +207,28 @@ export abstract class LiveAtlasTileLayer extends TileLayer { // @ts-ignore super._removeTile(key); } + + onAdd(map: LeafletMap): this { + if(this._mapSettings.tileUpdateInterval) { + this.refreshTimeout = setTimeout(() => this.handlePeriodicRefresh(), this._mapSettings.tileUpdateInterval); + } + + return super.onAdd(map); + } + + remove() { + if(this.refreshTimeout) { + clearTimeout(this.refreshTimeout); + } + + return super.remove(); + } + + private handlePeriodicRefresh() { + if(this._map) { + this.refresh(); + } + + this.refreshTimeout = setTimeout(() => this.handlePeriodicRefresh(), this._mapSettings.tileUpdateInterval); + } }