prefer type-only imports in testing utilities

This commit is contained in:
Hazelnoot 2025-11-09 00:51:34 -05:00
parent e7015d4f8e
commit b013649a41
8 changed files with 27 additions and 25 deletions

View file

@ -3,8 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import * as Redis from 'ioredis';
import { Inject, Injectable, type OnApplicationShutdown } from '@nestjs/common';
import {
MemoryKVCache,
MemorySingleCache,
@ -26,6 +25,7 @@ import { DI } from '@/di-symbols.js';
import { TimeService, type TimerHandle } from '@/global/TimeService.js';
import { InternalEventService } from '@/global/InternalEventService.js';
import { callAllOn } from '@/misc/call-all.js';
import type * as Redis from 'ioredis';
// This is the one place that's *supposed* to new() up caches.
/* eslint-disable no-restricted-syntax */

View file

@ -4,13 +4,14 @@
*/
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import type { GlobalEvents, InternalEventTypes } from '@/core/GlobalEventService.js';
import type { Config } from '@/config.js';
import { bindThis } from '@/decorators.js';
import type Redis from 'ioredis';
export type Listener<K extends keyof InternalEventTypes> = (value: InternalEventTypes[K], key: K, isLocal: boolean) => void | Promise<void>;
export type EventTypes = InternalEventTypes;
export type Listener<K extends keyof EventTypes> = (value: EventTypes[K], key: K, isLocal: boolean) => void | Promise<void>;
export interface ListenerProps {
ignoreLocal?: boolean,
@ -19,7 +20,7 @@ export interface ListenerProps {
@Injectable()
export class InternalEventService implements OnApplicationShutdown {
private readonly listeners = new Map<keyof InternalEventTypes, Map<Listener<keyof InternalEventTypes>, ListenerProps>>();
private readonly listeners = new Map<keyof EventTypes, Map<Listener<keyof EventTypes>, ListenerProps>>();
constructor(
@Inject(DI.redisForSub)
@ -35,7 +36,7 @@ export class InternalEventService implements OnApplicationShutdown {
}
@bindThis
public on<K extends keyof InternalEventTypes>(type: K, listener: Listener<K>, props?: ListenerProps): void {
public on<K extends keyof EventTypes>(type: K, listener: Listener<K>, props?: ListenerProps): void {
let set = this.listeners.get(type);
if (!set) {
set = new Map();
@ -43,16 +44,16 @@ export class InternalEventService implements OnApplicationShutdown {
}
// Functionally, this is just a set with metadata on the values.
set.set(listener as Listener<keyof InternalEventTypes>, props ?? {});
set.set(listener as Listener<keyof EventTypes>, props ?? {});
}
@bindThis
public off<K extends keyof InternalEventTypes>(type: K, listener: Listener<K>): void {
this.listeners.get(type)?.delete(listener as Listener<keyof InternalEventTypes>);
public off<K extends keyof EventTypes>(type: K, listener: Listener<K>): void {
this.listeners.get(type)?.delete(listener as Listener<keyof EventTypes>);
}
@bindThis
public async emit<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K]): Promise<void> {
public async emit<K extends keyof EventTypes>(type: K, value: EventTypes[K]): Promise<void> {
await this.emitInternal(type, value, true);
await this.redisForPub.publish(this.config.host, JSON.stringify({
channel: 'internal',
@ -61,7 +62,7 @@ export class InternalEventService implements OnApplicationShutdown {
}
@bindThis
private async emitInternal<K extends keyof InternalEventTypes>(type: K, value: InternalEventTypes[K], isLocal: boolean): Promise<void> {
private async emitInternal<K extends keyof EventTypes>(type: K, value: EventTypes[K], isLocal: boolean): Promise<void> {
const listeners = this.listeners.get(type);
if (!listeners) {
return;
@ -84,7 +85,7 @@ export class InternalEventService implements OnApplicationShutdown {
if (obj.channel === 'internal') {
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
if (!isLocalInternalEvent(body) || body._pid !== process.pid) {
await this.emitInternal(type, body as InternalEventTypes[keyof InternalEventTypes], false);
await this.emitInternal(type, body as EventTypes[keyof EventTypes], false);
}
}
}

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
import { Injectable, type OnApplicationShutdown } from '@nestjs/common';
import { bindThis } from '@/decorators.js';
const timerTokenSymbol = Symbol('timerToken');

View file

@ -3,9 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import * as Redis from 'ioredis';
import type * as Redis from 'ioredis';
import { bindThis } from '@/decorators.js';
import { TimeService } from '@/global/TimeService.js';
import type { TimeService } from '@/global/TimeService.js';
export interface RedisCacheServices extends MemoryCacheServices {
readonly redisClient: Redis.Redis