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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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>

View File

@ -30,7 +30,7 @@ class AuthManager {
globalThis.location.href = redirectUri;
}
} finally {
eventManager.emit('auth.logout');
eventManager.emit('AuthLogout');
}
}
}

View File

@ -1,6 +1,15 @@
import type { ThemeSetting } from '$lib/managers/theme-manager.svelte';
import type { LoginResponseDto } from '@immich/sdk';
export type Events = {
AppInit: [];
UserLogin: [];
AuthLogin: [LoginResponseDto];
AuthLogout: [];
LanguageChange: [{ name: string; code: string; rtl?: boolean }];
ThemeChange: [ThemeSetting];
};
type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void;
class EventManager<EventMap extends Record<string, unknown[]>> {
@ -51,11 +60,4 @@ class EventManager<EventMap extends Record<string, unknown[]>> {
}
}
export const eventManager = new EventManager<{
'app.init': [];
'user.login': [];
'auth.login': [LoginResponseDto];
'auth.logout': [];
'language.change': [{ name: string; code: string; rtl?: boolean }];
'theme.change': [ThemeSetting];
}>();
export const eventManager = new EventManager<Events>();

View File

@ -4,7 +4,7 @@ import { lang } from '$lib/stores/preferences.store';
class LanguageManager {
constructor() {
eventManager.on('app.init', () => lang.subscribe((lang) => this.setLanguage(lang)));
eventManager.on('AppInit', () => lang.subscribe((lang) => this.setLanguage(lang)));
}
rtl = $state(false);
@ -19,7 +19,7 @@ class LanguageManager {
document.body.setAttribute('dir', item.rtl ? 'rtl' : 'ltr');
eventManager.emit('language.change', item);
eventManager.emit('LanguageChange', item);
}
}

View File

@ -36,7 +36,7 @@ class ThemeManager {
isDark = $derived(this.value === Theme.DARK);
constructor() {
eventManager.on('app.init', () => this.#onAppInit());
eventManager.on('AppInit', () => this.#onAppInit());
}
setSystem(system: boolean) {
@ -71,7 +71,7 @@ class ThemeManager {
this.#theme.current = theme;
eventManager.emit('theme.change', theme);
eventManager.emit('ThemeChange', theme);
}
}

View File

@ -6,7 +6,7 @@ class UploadManager {
mediaTypes = $state<ServerMediaTypesResponseDto>({ image: [], sidecar: [], video: [] });
constructor() {
eventManager.on('app.init', () => void this.#loadExtensions()).on('auth.logout', () => void this.reset());
eventManager.on('AppInit', () => void this.#loadExtensions()).on('AuthLogout', () => void this.reset());
}
reset() {

View File

@ -19,7 +19,7 @@ class FoldersStore {
private assets = $state<AssetCache>({});
constructor() {
eventManager.on('auth.logout', () => this.clearCache());
eventManager.on('AuthLogout', () => this.clearCache());
}
async fetchTree(): Promise<TreeNode> {

View File

@ -21,7 +21,7 @@ export type MemoryAsset = MemoryIndex & {
class MemoryStoreSvelte {
constructor() {
eventManager.on('auth.logout', () => this.clearCache());
eventManager.on('AuthLogout', () => this.clearCache());
}
memories = $state<MemoryResponseDto[]>([]);

View File

@ -9,8 +9,8 @@ class NotificationStore {
notifications = $state<NotificationDto[]>([]);
constructor() {
eventManager.on('auth.login', () => handlePromiseError(this.refresh()));
eventManager.on('auth.logout', () => this.clear());
eventManager.on('AuthLogin', () => handlePromiseError(this.refresh()));
eventManager.on('AuthLogout', () => this.clear());
}
async refresh() {

View File

@ -5,7 +5,7 @@ class SearchStore {
isSearchEnabled = $state(false);
constructor() {
eventManager.on('auth.logout', () => this.clearCache());
eventManager.on('AuthLogout', () => this.clearCache());
}
clearCache() {

View File

@ -16,4 +16,4 @@ export const resetSavedUser = () => {
purchaseStore.setPurchaseStatus(false);
};
eventManager.on('auth.logout', () => resetSavedUser());
eventManager.on('AuthLogout', () => resetSavedUser());

View File

@ -26,4 +26,4 @@ const reset = () => {
Object.assign(userInteraction, defaultUserInteraction);
};
eventManager.on('auth.logout', () => reset());
eventManager.on('AuthLogout', () => reset());

View File

@ -58,7 +58,7 @@
// if the browser theme changes, changes the Immich theme too
});
eventManager.emit('app.init');
eventManager.emit('AppInit');
beforeNavigate(({ from, to }) => {
if (isAssetViewerRoute(from) && isAssetViewerRoute(to)) {

View File

@ -27,7 +27,7 @@
const onSuccess = async (user: LoginResponseDto) => {
await goto(data.continueUrl, { invalidateAll: true });
eventManager.emit('auth.login', user);
eventManager.emit('AuthLogin', user);
};
const onFirstLogin = () => goto(AppRoute.AUTH_CHANGE_PASSWORD);