2020-12-16 16:54:41 +00:00
|
|
|
/*
|
2022-01-15 00:07:25 +00:00
|
|
|
* Copyright 2022 James Lyne
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
|
|
|
* Some portions of this file were taken from https://github.com/webbukkit/dynmap.
|
|
|
|
* These portions are Copyright 2020 Dynmap Contributors.
|
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2020-12-16 16:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
import {useStore} from "@/store";
|
|
|
|
import {ActionTypes} from "@/store/action-types";
|
|
|
|
import {DynmapMarkerUpdate} from "@/dynmap";
|
|
|
|
import {computed, watch} from "@vue/runtime-core";
|
|
|
|
import {ComputedRef} from "@vue/reactivity";
|
2020-12-14 00:27:49 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export type LiveAtlasMarkerUpdateCallback = ((update: DynmapMarkerUpdate) => void);
|
2021-01-26 14:32:48 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export enum LiveAtlasMarkerType {
|
|
|
|
POINT,
|
|
|
|
AREA,
|
|
|
|
LINE,
|
|
|
|
CIRCLE
|
|
|
|
}
|
|
|
|
|
|
|
|
let updateFrame = 0;
|
|
|
|
let pendingUpdates: ComputedRef;
|
|
|
|
|
|
|
|
const setHandlers: { [key:string]: Set<LiveAtlasMarkerUpdateCallback>} = {};
|
|
|
|
const typeHandlers: { [key:string]: Map<LiveAtlasMarkerType, Set<LiveAtlasMarkerUpdateCallback>>} = {};
|
|
|
|
|
|
|
|
export const startUpdateHandling = () => {
|
|
|
|
const store = useStore();
|
|
|
|
|
|
|
|
pendingUpdates = computed(() => store.state.pendingMarkerUpdates.length);
|
|
|
|
|
|
|
|
watch(pendingUpdates, (newValue, oldValue) => {
|
|
|
|
if(newValue && newValue > 0 && oldValue === 0 && !updateFrame) {
|
|
|
|
updateFrame = requestAnimationFrame(() => handlePendingUpdates());
|
2021-12-07 20:42:24 +00:00
|
|
|
}
|
2021-05-25 22:31:03 +00:00
|
|
|
});
|
2022-01-15 00:07:25 +00:00
|
|
|
}
|
2021-05-25 22:31:03 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export const stopUpdateHandling = () => {
|
|
|
|
if(updateFrame) {
|
|
|
|
cancelAnimationFrame(updateFrame);
|
|
|
|
updateFrame = 0;
|
2021-01-26 14:32:48 +00:00
|
|
|
}
|
2022-01-15 00:07:25 +00:00
|
|
|
}
|
2021-01-26 14:32:48 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export const registerUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string) => {
|
|
|
|
if(!setHandlers[set]) {
|
|
|
|
setHandlers[set] = new Set();
|
2020-12-14 00:27:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
setHandlers[set].add(callback);
|
|
|
|
}
|
2020-12-16 11:59:09 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export const registerTypeUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string, type: LiveAtlasMarkerType) => {
|
|
|
|
if(!typeHandlers[set]) {
|
|
|
|
typeHandlers[set] = new Map();
|
2020-12-16 11:59:09 +00:00
|
|
|
}
|
2020-12-14 00:27:49 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
if(typeHandlers[set].has(type)) {
|
|
|
|
typeHandlers[set].get(type)!.add(callback);
|
|
|
|
} else {
|
|
|
|
typeHandlers[set].set(type, new Set([callback]));
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 18:01:37 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export const unregisterUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string) => {
|
|
|
|
if(!setHandlers[set]) {
|
|
|
|
return;
|
2020-12-18 18:01:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
setHandlers[set].delete(callback);
|
|
|
|
}
|
2021-01-26 14:32:48 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
export const unregisterTypeUpdateHandler = (callback: LiveAtlasMarkerUpdateCallback, set: string, type: LiveAtlasMarkerType) => {
|
|
|
|
if(typeHandlers[set]) {
|
|
|
|
return;
|
2021-01-26 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
if(typeHandlers[set].has(type)) {
|
|
|
|
typeHandlers[set].get(type)!.delete(callback);
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 14:32:48 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
const handlePendingUpdates = async () => {
|
|
|
|
const store = useStore(),
|
|
|
|
updates = await store.dispatch(ActionTypes.POP_MARKER_UPDATES, 10);
|
2021-01-26 14:32:48 +00:00
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
for(const update of updates) {
|
|
|
|
if(setHandlers[update.set]) {
|
|
|
|
setHandlers[update.set].forEach(callback => callback(update));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(typeHandlers[update.set] && typeHandlers[update.set].has(update.type)) {
|
|
|
|
typeHandlers[update.set].get(update.type)!.forEach(callback => callback(update));
|
|
|
|
}
|
2021-01-26 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:07:25 +00:00
|
|
|
if(pendingUpdates.value) {
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
updateFrame = requestAnimationFrame(() => handlePendingUpdates());
|
|
|
|
} else {
|
|
|
|
updateFrame = 0;
|
|
|
|
}
|
|
|
|
};
|