make GlobalEventService depend on InternalEventService (instead of the other way around) so that they can eventually be separated

This commit is contained in:
Hazelnoot 2025-09-30 22:10:24 -04:00
parent 48cc7e21e3
commit ab213f2146
2 changed files with 20 additions and 7 deletions

View file

@ -26,6 +26,8 @@ import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { bindThis } from '@/decorators.js';
import { Serialized } from '@/types.js';
import { InternalEventService } from '@/core/InternalEventService.js';
import { trackPromise } from '@/misc/promise-tracker.js';
import type Emitter from 'strict-event-emitter-types';
import type { EventEmitter } from 'events';
@ -350,6 +352,8 @@ export class GlobalEventService {
@Inject(DI.redisForPub)
private redisForPub: Redis.Redis,
private readonly internalEventService: InternalEventService,
) {
}
@ -365,14 +369,16 @@ export class GlobalEventService {
}));
}
/** @deprecated use InternalEventService instead */
@bindThis
public publishInternalEvent<K extends keyof InternalEventTypes>(type: K, value?: InternalEventTypes[K]): void {
this.publish('internal', type, typeof value === 'undefined' ? null : value);
public publishInternalEvent<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K]): void {
trackPromise(this.internalEventService.emit(type, value));
}
/** @deprecated use InternalEventService instead */
@bindThis
public async publishInternalEventAsync<K extends keyof InternalEventTypes>(type: K, value?: InternalEventTypes[K]): Promise<void> {
await this.publish('internal', type, typeof value === 'undefined' ? null : value);
public async publishInternalEventAsync<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K]): Promise<void> {
await this.internalEventService.emit(type, value);
}
@bindThis

View file

@ -6,8 +6,8 @@
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import type { GlobalEvents, InternalEventTypes } from '@/core/GlobalEventService.js';
import type { Config } from '@/config.js';
import { bindThis } from '@/decorators.js';
export type Listener<K extends keyof InternalEventTypes> = (value: InternalEventTypes[K], key: K, isLocal: boolean) => void | Promise<void>;
@ -25,7 +25,11 @@ export class InternalEventService implements OnApplicationShutdown {
@Inject(DI.redisForSub)
private readonly redisForSub: Redis.Redis,
private readonly globalEventService: GlobalEventService,
@Inject(DI.redis)
private readonly redisForPub: Redis.Redis,
@Inject(DI.config)
private readonly config: Pick<Config, 'host'>,
) {
this.redisForSub.on('message', this.onMessage);
}
@ -50,7 +54,10 @@ export class InternalEventService implements OnApplicationShutdown {
@bindThis
public async emit<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K]): Promise<void> {
await this.emitInternal(type, value, true);
await this.globalEventService.publishInternalEventAsync(type, { ...value, _pid: process.pid });
await this.redisForPub.publish(this.config.host, JSON.stringify({
channel: 'internal',
message: { type: type, body: value },
}));
}
@bindThis