From 6f12b0b59e2794a24b27940c9c62dd279e2120e8 Mon Sep 17 00:00:00 2001 From: Olivia Date: Mon, 19 Aug 2024 14:36:33 +0200 Subject: [PATCH] fix events typing --- .../src/examples/Example-MapEvents.vue | 6 ++--- packages/core/lib/model/events.ts | 14 +++++----- .../lib/map/register-events.test.ts | 5 +++- .../openlayers/lib/map/register-events.ts | 26 ++++++++++--------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/apps/examples/src/examples/Example-MapEvents.vue b/apps/examples/src/examples/Example-MapEvents.vue index ba311e1..fdfa0fe 100644 --- a/apps/examples/src/examples/Example-MapEvents.vue +++ b/apps/examples/src/examples/Example-MapEvents.vue @@ -2,7 +2,7 @@ import { onMounted, ref } from 'vue' import Map from 'ol/Map' import { createMapFromContext, listen } from '@geospatial-sdk/openlayers' -import type { FeaturesHoverEvent, MapClickEvent, MapContext } from '@geospatial-sdk/core' +import type { MapContext } from '@geospatial-sdk/core' import { DEFAULT_CONTEXT } from '@/constants' import Panel from '@/components/Panel.vue' import type { Feature } from 'geojson' @@ -30,8 +30,8 @@ let clickCoordinates = ref<[number, number] | null>(null) onMounted(async () => { map = await createMapFromContext(context, mapRoot.value) - listen(map, 'features-hover', (event: FeaturesHoverEvent) => (features.value = event.features)) - listen(map, 'map-click', (event: MapClickEvent) => (clickCoordinates.value = event.coordinate)) + listen(map, 'features-hover', (event) => (features.value = event.features)) + listen(map, 'map-click', (event) => (clickCoordinates.value = event.coordinate)) }) diff --git a/packages/core/lib/model/events.ts b/packages/core/lib/model/events.ts index 2383494..b3b0074 100644 --- a/packages/core/lib/model/events.ts +++ b/packages/core/lib/model/events.ts @@ -2,22 +2,24 @@ import { Feature } from "geojson"; export const FeaturesClickEventType = "features-click"; export interface FeaturesClickEvent { + type: typeof FeaturesClickEventType; features: Feature[]; } export const FeaturesHoverEventType = "features-hover"; export interface FeaturesHoverEvent { + type: typeof FeaturesHoverEventType; features: Feature[]; } export const MapClickEventType = "map-click"; export interface MapClickEvent { + type: typeof MapClickEventType; coordinate: [number, number]; // expressed in lon/lat } -export type MapEvent = FeaturesClickEvent | FeaturesHoverEvent | MapClickEvent; - -export type MapEventType = - | typeof FeaturesClickEventType - | typeof FeaturesHoverEventType - | typeof MapClickEventType; +export interface MapEventsByType { + [FeaturesClickEventType]: FeaturesClickEvent; + [FeaturesHoverEventType]: FeaturesHoverEvent; + [MapClickEventType]: MapClickEvent; +} diff --git a/packages/openlayers/lib/map/register-events.test.ts b/packages/openlayers/lib/map/register-events.test.ts index e97fe41..03977c9 100644 --- a/packages/openlayers/lib/map/register-events.test.ts +++ b/packages/openlayers/lib/map/register-events.test.ts @@ -190,7 +190,10 @@ describe("event registration", () => { map.dispatchEvent(createMapEvent(map, "click")); }); it("registers the event on the map", () => { - expect(callback).toHaveBeenCalledWith({ coordinate: toLonLat([10, 10]) }); + expect(callback).toHaveBeenCalledWith({ + coordinate: toLonLat([10, 10]), + type: "map-click", + }); }); }); }); diff --git a/packages/openlayers/lib/map/register-events.ts b/packages/openlayers/lib/map/register-events.ts index 5045e6f..e2e0f99 100644 --- a/packages/openlayers/lib/map/register-events.ts +++ b/packages/openlayers/lib/map/register-events.ts @@ -3,8 +3,7 @@ import { FeaturesClickEventType, FeaturesHoverEventType, MapClickEventType, - MapEvent, - MapEventType, + MapEventsByType, } from "@geospatial-sdk/core"; import { toLonLat } from "ol/proj"; import GeoJSON from "ol/format/GeoJSON"; @@ -121,24 +120,24 @@ function registerFeatureHoverEvent(map: Map) { map.set(FeaturesHoverEventType, true); } -export function listen( +export function listen( map: Map, - event: MapEventType, - callback: (event: MapEvent) => void, + eventType: T, + callback: (event: MapEventsByType[T]) => void, ) { - switch (event) { + switch (eventType) { case FeaturesClickEventType: registerFeatureClickEvent(map); // we're using a custom event type here so we need to cast to unknown first - map.on(event as unknown as MapObjectEventTypes, (event) => { - callback(event as unknown as MapEvent); + map.on(eventType as unknown as MapObjectEventTypes, (event) => { + (callback as (event: unknown) => void)(event); }); break; case FeaturesHoverEventType: registerFeatureHoverEvent(map); // see comment above - map.on(event as unknown as MapObjectEventTypes, (event) => { - callback(event as unknown as MapEvent); + map.on(eventType as unknown as MapObjectEventTypes, (event) => { + (callback as (event: unknown) => void)(event); }); break; case MapClickEventType: @@ -147,10 +146,13 @@ export function listen( event.pixel, map.getView().getProjection(), ) as [number, number]; - callback({ coordinate }); + (callback as (event: unknown) => void)({ + type: "map-click", + coordinate, + }); }); break; default: - throw new Error(`Unrecognized event type: ${event}`); + throw new Error(`Unrecognized event type: ${eventType}`); } }