From f90626402116abe389baf1147df8fbc7fb3d507c Mon Sep 17 00:00:00 2001 From: James Lyne Date: Sun, 16 Jan 2022 23:21:22 +0000 Subject: [PATCH] Add global marker update handler which handles all marker sets --- src/components/map/marker/Markers.vue | 6 ++--- src/util/markers.ts | 35 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/components/map/marker/Markers.vue b/src/components/map/marker/Markers.vue index def258a..c984c8c 100644 --- a/src/components/map/marker/Markers.vue +++ b/src/components/map/marker/Markers.vue @@ -23,7 +23,7 @@ import {nonReactiveState} from "@/store/state"; import {DynmapMarkerUpdate} from "@/dynmap"; import { createMarkerLayer, - registerUpdateHandler, unregisterUpdateHandler, updateMarkerLayer + registerSetUpdateHandler, unregisterSetUpdateHandler, updateMarkerLayer } from "@/util/markers"; import {Layer} from "leaflet"; @@ -92,10 +92,10 @@ export default defineComponent({ onMounted(() => { createMarkers(); - registerUpdateHandler(handleUpdate, props.set.id); + registerSetUpdateHandler(handleUpdate, props.set.id); }); onUnmounted(() => { - unregisterUpdateHandler(handleUpdate, props.set.id); + unregisterSetUpdateHandler(handleUpdate, props.set.id); }); }, diff --git a/src/util/markers.ts b/src/util/markers.ts index 1d2a4f7..f153202 100644 --- a/src/util/markers.ts +++ b/src/util/markers.ts @@ -50,7 +50,8 @@ export enum LiveAtlasMarkerType { let updateFrame = 0; let pendingUpdates: ComputedRef; -const updateHandlers: { [key:string]: Set} = {}; +const updateHandlers: Set = new Set(); +const setUpdateHandlers: { [key:string]: Set} = {}; export const startUpdateHandling = () => { const store = useStore(); @@ -71,20 +72,28 @@ export const stopUpdateHandling = () => { } } -export const registerUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string) => { - if(!updateHandlers[set]) { - updateHandlers[set] = new Set(); - } - - updateHandlers[set].add(callback); +export const registerUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback) => { + updateHandlers.add(callback); } -export const unregisterUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string) => { - if(!updateHandlers[set]) { +export const unregisterUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback) => { + updateHandlers.delete(callback); +} + +export const registerSetUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string) => { + if(!setUpdateHandlers[set]) { + setUpdateHandlers[set] = new Set(); + } + + setUpdateHandlers[set].add(callback); +} + +export const unregisterSetUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string) => { + if(!setUpdateHandlers[set]) { return; } - updateHandlers[set].delete(callback); + setUpdateHandlers[set].delete(callback); } const handlePendingUpdates = async () => { @@ -92,8 +101,10 @@ const handlePendingUpdates = async () => { updates = await store.dispatch(ActionTypes.POP_MARKER_UPDATES, 10); for(const update of updates) { - if(updateHandlers[update.set]) { - updateHandlers[update.set].forEach(callback => callback(update)); + updateHandlers.forEach(callback => callback(update)); + + if(setUpdateHandlers[update.set]) { + setUpdateHandlers[update.set].forEach(callback => callback(update)); } }