Skip to content

Commit

Permalink
fix events typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Aug 19, 2024
1 parent e758ce3 commit 6f12b0b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
6 changes: 3 additions & 3 deletions apps/examples/src/examples/Example-MapEvents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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))
})
</script>

Expand Down
14 changes: 8 additions & 6 deletions packages/core/lib/model/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 4 additions & 1 deletion packages/openlayers/lib/map/register-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
});
});
26 changes: 14 additions & 12 deletions packages/openlayers/lib/map/register-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -121,24 +120,24 @@ function registerFeatureHoverEvent(map: Map) {
map.set(FeaturesHoverEventType, true);
}

export function listen(
export function listen<T extends keyof MapEventsByType>(
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:
Expand All @@ -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}`);
}
}

0 comments on commit 6f12b0b

Please sign in to comment.