import type { MglMap } from '@/components'; import type { ValidLanguages } from '@/types'; import type { Map as MaplibreMap } from 'maplibre-gl'; import { reactive, type ShallowRef } from 'vue'; export interface MapInstance { component?: InstanceType; map?: MaplibreMap; isMounted: boolean; isLoaded: boolean; language: ValidLanguages | undefined; } const instances = new Map(), defaultKey = Symbol('default'); // useMap returns reactive version of MapInstance export function useMap(key: symbol | string = defaultKey): MapInstance { let component = instances.get(key); if (!component) { component = reactive({ isLoaded: false, isMounted: false, language: undefined }); instances.set(key, component); } return component; } export function registerMap(instance: InstanceType, map: ShallowRef, key: symbol | string = defaultKey): MapInstance { let component = instances.get(key); if (!component) { component = reactive({ isLoaded: false, isMounted: false, language: undefined }); instances.set(key, component); } component.component = instance; component.map = map.value; component.isLoaded = map.value?.loaded() || false; component.isMounted = false; return component; }