mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
refactor(server): app module (#13193)
This commit is contained in:
parent
7ee0221c8e
commit
5d0a4bb1a5
18 changed files with 126 additions and 134 deletions
|
|
@ -1,62 +0,0 @@
|
|||
import { ModuleRef, Reflector } from '@nestjs/core';
|
||||
import _ from 'lodash';
|
||||
import { EventConfig } from 'src/decorators';
|
||||
import { MetadataKey } from 'src/enum';
|
||||
import { EmitEvent, EmitHandler, IEventRepository } from 'src/interfaces/event.interface';
|
||||
import { services } from 'src/services';
|
||||
|
||||
type Item<T extends EmitEvent> = {
|
||||
event: T;
|
||||
handler: EmitHandler<T>;
|
||||
priority: number;
|
||||
server: boolean;
|
||||
label: string;
|
||||
};
|
||||
|
||||
export class ImmichStartupError extends Error {}
|
||||
export const isStartUpError = (error: unknown): error is ImmichStartupError => error instanceof ImmichStartupError;
|
||||
|
||||
export const setupEventHandlers = (moduleRef: ModuleRef) => {
|
||||
const reflector = moduleRef.get(Reflector, { strict: false });
|
||||
const repository = moduleRef.get<IEventRepository>(IEventRepository);
|
||||
const items: Item<EmitEvent>[] = [];
|
||||
|
||||
// discovery
|
||||
for (const Service of services) {
|
||||
const instance = moduleRef.get<any>(Service);
|
||||
const ctx = Object.getPrototypeOf(instance);
|
||||
for (const property of Object.getOwnPropertyNames(ctx)) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(ctx, property);
|
||||
if (!descriptor || descriptor.get || descriptor.set) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const handler = instance[property];
|
||||
if (typeof handler !== 'function') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const event = reflector.get<EventConfig>(MetadataKey.EVENT_CONFIG, handler);
|
||||
if (!event) {
|
||||
continue;
|
||||
}
|
||||
|
||||
items.push({
|
||||
event: event.name,
|
||||
priority: event.priority || 0,
|
||||
server: event.server ?? false,
|
||||
handler: handler.bind(instance),
|
||||
label: `${Service.name}.${handler.name}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const handlers = _.orderBy(items, ['priority'], ['asc']);
|
||||
|
||||
// register by priority
|
||||
for (const handler of handlers) {
|
||||
repository.on(handler);
|
||||
}
|
||||
|
||||
return handlers;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue