mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor(server): event emits (#10648)
* refactor(server): event emits * refactor: change default priority to 0
This commit is contained in:
parent
7e99394c70
commit
72bf9439b0
25 changed files with 222 additions and 171 deletions
|
|
@ -4,6 +4,23 @@ import { ReleaseNotification, ServerVersionResponseDto } from 'src/dtos/server-i
|
|||
|
||||
export const IEventRepository = 'IEventRepository';
|
||||
|
||||
type MaybePromise<T> = Promise<T> | T;
|
||||
|
||||
const noop = () => {};
|
||||
const dummyHandlers = {
|
||||
onBootstrapEvent: noop as (app: 'api' | 'microservices') => MaybePromise<void>,
|
||||
onShutdownEvent: noop as () => MaybePromise<void>,
|
||||
onConfigUpdateEvent: noop as (update: SystemConfigUpdate) => MaybePromise<void>,
|
||||
onConfigValidateEvent: noop as (update: SystemConfigUpdate) => MaybePromise<void>,
|
||||
};
|
||||
|
||||
export type SystemConfigUpdate = { newConfig: SystemConfig; oldConfig: SystemConfig };
|
||||
export type EventHandlers = typeof dummyHandlers;
|
||||
export type EmitEvent = keyof EventHandlers;
|
||||
export type EmitEventHandler<T extends EmitEvent> = (...args: Parameters<EventHandlers[T]>) => MaybePromise<void>;
|
||||
export const events = Object.keys(dummyHandlers) as EmitEvent[];
|
||||
export type OnEvents = Partial<EventHandlers>;
|
||||
|
||||
export enum ClientEvent {
|
||||
UPLOAD_SUCCESS = 'on_upload_success',
|
||||
USER_DELETE = 'on_user_delete',
|
||||
|
|
@ -44,15 +61,10 @@ export interface ServerEventMap {
|
|||
[ServerEvent.WEBSOCKET_CONNECT]: { userId: string };
|
||||
}
|
||||
|
||||
export enum ServerAsyncEvent {
|
||||
CONFIG_VALIDATE = 'config.validate',
|
||||
}
|
||||
|
||||
export interface ServerAsyncEventMap {
|
||||
[ServerAsyncEvent.CONFIG_VALIDATE]: { newConfig: SystemConfig; oldConfig: SystemConfig };
|
||||
}
|
||||
|
||||
export interface IEventRepository {
|
||||
on<T extends EmitEvent>(event: T, handler: EmitEventHandler<T>): void;
|
||||
emit<T extends EmitEvent>(event: T, ...args: Parameters<EmitEventHandler<T>>): Promise<void>;
|
||||
|
||||
/**
|
||||
* Send to connected clients for a specific user
|
||||
*/
|
||||
|
|
@ -65,8 +77,4 @@ export interface IEventRepository {
|
|||
* Notify listeners in this and connected processes. Subscribe to an event with `@OnServerEvent`
|
||||
*/
|
||||
serverSend<E extends keyof ServerEventMap>(event: E, data: ServerEventMap[E]): boolean;
|
||||
/**
|
||||
* Notify and wait for responses from listeners in this process. Subscribe to an event with `@OnServerEvent`
|
||||
*/
|
||||
serverSendAsync<E extends keyof ServerAsyncEventMap>(event: E, data: ServerAsyncEventMap[E]): Promise<any>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue