refactor(core): Use type-safe event emitters (no-changelog) (#10234)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-07-30 13:23:01 +02:00
committed by GitHub
parent 99dc56c7a1
commit 1fca3af335
12 changed files with 124 additions and 89 deletions

View File

@@ -1,5 +1,3 @@
import EventEmitter from 'node:events';
import Container, { Service } from 'typedi';
import { caching } from 'cache-manager';
import { ApplicationError, jsonStringify } from 'n8n-workflow';
@@ -10,14 +8,20 @@ import { MalformedRefreshValueError } from '@/errors/cache-errors/malformed-refr
import type {
TaggedRedisCache,
TaggedMemoryCache,
CacheEvent,
MaybeHash,
Hash,
} from '@/services/cache/cache.types';
import { TIME } from '@/constants';
import { TypedEmitter } from '@/TypedEmitter';
type CacheEvents = {
'metrics.cache.hit': never;
'metrics.cache.miss': never;
'metrics.cache.update': never;
};
@Service()
export class CacheService extends EventEmitter {
export class CacheService extends TypedEmitter<CacheEvents> {
private cache: TaggedRedisCache | TaggedMemoryCache;
async init() {
@@ -66,10 +70,6 @@ export class CacheService extends EventEmitter {
await this.cache.store.reset();
}
emit(event: CacheEvent, ...args: unknown[]) {
return super.emit(event, ...args);
}
isRedis() {
return this.cache.kind === 'redis';
}

View File

@@ -8,5 +8,3 @@ export type TaggedMemoryCache = MemoryCache & { kind: 'memory' };
export type Hash<T = unknown> = Record<string, T>;
export type MaybeHash<T> = Hash<T> | undefined;
export type CacheEvent = `metrics.cache.${'hit' | 'miss' | 'update'}`;