feat(web): event handler component (#23763)

This commit is contained in:
Daniel Dietzler
2025-11-10 17:49:46 +01:00
committed by GitHub
parent 493cde9d55
commit dd393c8346
14 changed files with 58 additions and 23 deletions

View File

@@ -0,0 +1,33 @@
<script lang="ts">
import { eventManager, type Events } from '$lib/managers/event-manager.svelte';
import { onMount } from 'svelte';
type Props = Partial<{
[K in keyof Events as `on${K}`]: (...args: Events[K]) => void;
}>;
const props: Props = $props();
const unsubscribes: Array<() => void> = [];
onMount(() => {
for (const name of Object.keys(props)) {
const event = name.slice(2) as keyof Events;
const listener = props[name as keyof Props];
if (!listener) {
continue;
}
const args = [event, listener as (...args: Events[typeof event]) => void] as const;
eventManager.on(...args);
unsubscribes.push(() => eventManager.off(...args));
}
return () => {
for (const unsubscribe of unsubscribes) {
unsubscribe();
}
};
});
</script>