refactor: event manager (#25565)

This commit is contained in:
Jason Rasmussen
2026-01-29 08:52:18 -05:00
committed by GitHub
parent 8db61d341f
commit 10b53b525d
21 changed files with 84 additions and 68 deletions
@@ -1,6 +1,6 @@
<script lang="ts">
import { assetViewerManager, type Events } from '$lib/managers/asset-viewer-manager.svelte';
import type { EventCallback } from '$lib/utils/base-event-manager.svelte';
import type { EventCallback, EventMap } from '$lib/utils/base-event-manager.svelte';
import { onMount } from 'svelte';
type Props = {
@@ -10,22 +10,17 @@
const props: Props = $props();
onMount(() => {
const unsubscribes: Array<() => void> = [];
const events: EventMap<Events> = {};
for (const name of Object.keys(props)) {
const event = name.slice(2) as keyof Events;
const listener = props[name as keyof Props] as EventCallback<Events, typeof event> | undefined;
if (!listener) {
continue;
for (const [name, listener] of Object.entries(props)) {
if (listener) {
const event = name.slice(2) as keyof Events;
events[event] = listener as EventCallback<Events, typeof event>;
}
unsubscribes.push(assetViewerManager.on(event, listener));
}
return () => {
for (const unsubscribe of unsubscribes) {
unsubscribe();
}
};
return assetViewerManager.on(events);
});
</script>
const event = name.slice(2) as keyof Events;
+7 -16
View File
@@ -1,5 +1,6 @@
<script lang="ts">
import { eventManager, type Events } from '$lib/managers/event-manager.svelte';
import type { EventCallback, EventMap } from '$lib/utils/base-event-manager.svelte';
import { onMount } from 'svelte';
type Props = {
@@ -9,25 +10,15 @@
const props: Props = $props();
onMount(() => {
const unsubscribes: Array<() => void> = [];
const events: EventMap<Events> = {};
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;
for (const [name, listener] of Object.entries(props)) {
if (listener) {
const event = name.slice(2) as keyof Events;
events[event] = listener as EventCallback<Events, typeof event>;
}
const args = [event, listener as (...args: Events[typeof event]) => void] as const;
unsubscribes.push(eventManager.on(...args));
}
return () => {
for (const unsubscribe of unsubscribes) {
unsubscribe();
}
};
return eventManager.on(events);
});
</script>