mirror of
https://github.com/immich-app/immich.git
synced 2026-02-04 08:49:01 +03:00
feat: add onMany to BaseEventManager (#25492)
Use a map of events instead of array of tuples for better ergonomics.
This commit is contained in:
@@ -6,8 +6,10 @@ class UploadManager {
|
|||||||
mediaTypes = $state<ServerMediaTypesResponseDto>({ image: [], sidecar: [], video: [] });
|
mediaTypes = $state<ServerMediaTypesResponseDto>({ image: [], sidecar: [], video: [] });
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('AppInit', () => this.#loadExtensions());
|
eventManager.onMany({
|
||||||
eventManager.on('AuthLogout', () => this.reset());
|
AppInit: () => this.#loadExtensions(),
|
||||||
|
AuthLogout: () => this.reset(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
|
|||||||
@@ -23,8 +23,10 @@ class MemoryStoreSvelte {
|
|||||||
#loading: Promise<void> | undefined;
|
#loading: Promise<void> | undefined;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('AuthLogout', () => this.clearCache());
|
eventManager.onMany({
|
||||||
eventManager.on('AuthUserLoaded', () => this.initialize());
|
AuthLogout: () => this.clearCache(),
|
||||||
|
AuthUserLoaded: () => this.initialize(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ready() {
|
ready() {
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ class NotificationStore {
|
|||||||
notifications = $state<NotificationDto[]>([]);
|
notifications = $state<NotificationDto[]>([]);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('AuthLogin', () => this.refresh());
|
eventManager.onMany({
|
||||||
eventManager.on('AuthLogout', () => this.clear());
|
AuthLogin: () => this.refresh(),
|
||||||
|
AuthLogout: () => this.clear(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async refresh() {
|
async refresh() {
|
||||||
|
|||||||
@@ -30,6 +30,17 @@ export class BaseEventManager<Events extends EventMap> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMany(subscriptions: { [T in keyof Events]?: EventCallback<Events, T> }) {
|
||||||
|
const cleanups = Object.entries(subscriptions).map(([event, callback]) =>
|
||||||
|
this.on(event as keyof Events, callback as EventCallback<Events, keyof Events>),
|
||||||
|
);
|
||||||
|
return () => {
|
||||||
|
for (const cleanup of cleanups) {
|
||||||
|
cleanup();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
emit<T extends keyof Events>(event: T, ...params: Events[T]) {
|
emit<T extends keyof Events>(event: T, ...params: Events[T]) {
|
||||||
const listeners = this.getListeners(event);
|
const listeners = this.getListeners(event);
|
||||||
for (const listener of listeners) {
|
for (const listener of listeners) {
|
||||||
|
|||||||
Reference in New Issue
Block a user