feat: add onMany to BaseEventManager

This commit is contained in:
midzelis
2026-01-24 18:29:13 +00:00
parent d6c5a382f8
commit a9a5ad4efb
4 changed files with 21 additions and 6 deletions

View File

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

View File

@@ -23,8 +23,10 @@ class MemoryStoreSvelte {
#loading: Promise<void> | undefined;
constructor() {
eventManager.on('AuthLogout', () => this.clearCache());
eventManager.on('AuthUserLoaded', () => this.initialize());
eventManager.onMany([
['AuthLogout', () => this.clearCache()],
['AuthUserLoaded', () => this.initialize()],
]);
}
ready() {

View File

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

View File

@@ -30,6 +30,15 @@ export class BaseEventManager<Events extends EventMap> {
};
}
onMany<T extends keyof Events>(subscriptions: Array<[T, EventCallback<Events, T>?]>) {
const cleanups = subscriptions.map(([event, callback]) => this.on(event, callback));
return () => {
for (const cleanup of cleanups) {
cleanup();
}
};
}
emit<T extends keyof Events>(event: T, ...params: Events[T]) {
const listeners = this.getListeners(event);
for (const listener of listeners) {