diff --git a/src/repositories/BaseRedisRepository.ts b/src/repositories/BaseRedisRepository.ts index aa142de..70c2b1f 100644 --- a/src/repositories/BaseRedisRepository.ts +++ b/src/repositories/BaseRedisRepository.ts @@ -1,7 +1,8 @@ import { createClient, RedisClientType } from 'redis'; import { REDIS_RECONNECT_INTERVAL } from "../environment"; +import { EventEmitter } from 'stream'; -export abstract class BaseRedisRepository { +export abstract class BaseRedisRepository extends EventEmitter { protected redisClient; constructor( @@ -14,6 +15,7 @@ export abstract class BaseRedisRepository { }, }), ) { + super(); this.redisClient = redisClient; this.redisClient.on('error', (err) => { console.error(err.stack); diff --git a/src/repositories/notifications/RedisNotificationRepository.ts b/src/repositories/notifications/RedisNotificationRepository.ts index 2c7609e..d4b2e0f 100644 --- a/src/repositories/notifications/RedisNotificationRepository.ts +++ b/src/repositories/notifications/RedisNotificationRepository.ts @@ -9,7 +9,7 @@ import { import { BaseRedisRepository } from "../BaseRedisRepository"; export class RedisNotificationRepository extends BaseRedisRepository implements NotificationRepository { - private listeners: Listener[] = []; + private notificationListeners: Listener[] = []; private readonly NOTIFICATION_KEY_PREFIX = 'notification:'; private getNotificationKey = (shuttleId: string, stopId: string): string => { @@ -23,7 +23,7 @@ export class RedisNotificationRepository extends BaseRedisRepository implements await this.redisClient.hSet(key, deviceId, secondsThreshold.toString()); - this.listeners.forEach((listener: Listener) => { + this.notificationListeners.forEach((listener: Listener) => { const event: NotificationEvent = { event: 'addOrUpdate', notification @@ -46,7 +46,7 @@ export class RedisNotificationRepository extends BaseRedisRepository implements await this.redisClient.del(key); } - this.listeners.forEach((listener) => { + this.notificationListeners.forEach((listener) => { const event: NotificationEvent = { event: 'delete', notification: { @@ -94,20 +94,20 @@ export class RedisNotificationRepository extends BaseRedisRepository implements }; public subscribeToNotificationChanges = (listener: Listener): void => { - const index = this.listeners.findIndex( + const index = this.notificationListeners.findIndex( (existingListener) => existingListener === listener ); if (index < 0) { - this.listeners.push(listener); + this.notificationListeners.push(listener); } }; public unsubscribeFromNotificationChanges = (listener: Listener): void => { - const index = this.listeners.findIndex( + const index = this.notificationListeners.findIndex( (existingListener) => existingListener === listener ); if (index >= 0) { - this.listeners.splice(index, 1); + this.notificationListeners.splice(index, 1); } }; } diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index 98a3fcb..96c99a5 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -1,8 +1,5 @@ -import EventEmitter from "node:events"; -import { createClient } from 'redis'; import { ShuttleGetterSetterRepository } from "./ShuttleGetterSetterRepository"; import { IEta, IOrderedStop, IRoute, IShuttle, IStop, shuttleHasArrivedAtStop } from "../../entities/ShuttleRepositoryEntities"; -import { REDIS_RECONNECT_INTERVAL } from "../../environment"; import { ShuttleRepositoryEvent, ShuttleRepositoryEventListener, @@ -12,27 +9,9 @@ import { ShuttleTravelTimeDataIdentifier, ShuttleTravelTimeDateFilterArguments, } from "./ShuttleGetterRepository"; +import { BaseRedisRepository } from "../BaseRedisRepository"; -export class RedisShuttleRepository extends EventEmitter implements ShuttleGetterSetterRepository { - protected redisClient; - - constructor( - redisClient = createClient({ - url: process.env.REDIS_URL, - socket: { - tls: process.env.NODE_ENV === 'production', - rejectUnauthorized: false, - reconnectStrategy: REDIS_RECONNECT_INTERVAL, - }, - }), - ) { - super(); - this.redisClient = redisClient; - this.redisClient.on('error', (err) => { - console.error(err.stack); - }); - } - +export class RedisShuttleRepository extends BaseRedisRepository implements ShuttleGetterSetterRepository { get isReady() { return this.redisClient.isReady; } diff --git a/src/repositories/shuttle/eta/RedisExternalSourceETARepository.ts b/src/repositories/shuttle/eta/RedisExternalSourceETARepository.ts index 1d1371d..a551dfb 100644 --- a/src/repositories/shuttle/eta/RedisExternalSourceETARepository.ts +++ b/src/repositories/shuttle/eta/RedisExternalSourceETARepository.ts @@ -37,33 +37,6 @@ export class RedisExternalSourceETARepository extends BaseRedisRepository implem removeAllListeners(eventName?: string | symbol | undefined): this { throw new Error("Method not implemented."); } - setMaxListeners(n: number): this { - throw new Error("Method not implemented."); - } - getMaxListeners(): number { - throw new Error("Method not implemented."); - } - listeners(eventName: string | symbol): Function[] { - throw new Error("Method not implemented."); - } - rawListeners(eventName: string | symbol): Function[] { - throw new Error("Method not implemented."); - } - emit(eventName: string | symbol, ...args: any[]): boolean { - throw new Error("Method not implemented."); - } - listenerCount(eventName: string | symbol, listener?: Function | undefined): number { - throw new Error("Method not implemented."); - } - prependListener(eventName: string | symbol, listener: (...args: any[]) => void): this { - throw new Error("Method not implemented."); - } - prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): this { - throw new Error("Method not implemented."); - } - eventNames(): (string | symbol)[] { - throw new Error("Method not implemented."); - } clearAllData(): Promise { throw new Error("Method not implemented."); } diff --git a/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts b/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts index 7ab93ab..d9aac9f 100644 --- a/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts +++ b/src/repositories/shuttle/eta/RedisSelfUpdatingETARepository.ts @@ -53,32 +53,4 @@ export class RedisSelfUpdatingETARepository extends BaseRedisRepository implemen removeAllListeners(eventName?: string | symbol | undefined): this { throw new Error("Method not implemented."); } - setMaxListeners(n: number): this { - throw new Error("Method not implemented."); - } - getMaxListeners(): number { - throw new Error("Method not implemented."); - } - listeners(eventName: string | symbol): Function[] { - throw new Error("Method not implemented."); - } - rawListeners(eventName: string | symbol): Function[] { - throw new Error("Method not implemented."); - } - emit(eventName: string | symbol, ...args: any[]): boolean { - throw new Error("Method not implemented."); - } - listenerCount(eventName: string | symbol, listener?: Function | undefined): number { - throw new Error("Method not implemented."); - } - prependListener(eventName: string | symbol, listener: (...args: any[]) => void): this { - throw new Error("Method not implemented."); - } - prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): this { - throw new Error("Method not implemented."); - } - eventNames(): (string | symbol)[] { - throw new Error("Method not implemented."); - } - }