removed doodoo projects, getting ready for restructuring
This commit is contained in:
parent
674112af05
commit
5c64b1ccf8
82 changed files with 5 additions and 8917 deletions
|
|
@ -1,131 +0,0 @@
|
|||
import Redis from 'ioredis';
|
||||
import { Event } from '@stock-bot/types';
|
||||
|
||||
export type EventHandler<T extends Event = Event> = (event: T) => Promise<void> | void;
|
||||
|
||||
export interface EventBusConfig {
|
||||
redisHost: string;
|
||||
redisPort: number;
|
||||
redisPassword?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event Bus for publishing and subscribing to events in the system
|
||||
* Provides reliable message delivery and pattern-based subscriptions
|
||||
*/
|
||||
export class EventBus {
|
||||
private publisher: Redis;
|
||||
private subscriber: Redis;
|
||||
private handlers: Map<string, EventHandler[]>;
|
||||
private isConnected: boolean = false;
|
||||
|
||||
constructor(private config: EventBusConfig) {
|
||||
this.publisher = new Redis({
|
||||
host: config.redisHost,
|
||||
port: config.redisPort,
|
||||
password: config.redisPassword,
|
||||
});
|
||||
|
||||
this.subscriber = new Redis({
|
||||
host: config.redisHost,
|
||||
port: config.redisPort,
|
||||
password: config.redisPassword,
|
||||
});
|
||||
|
||||
this.handlers = new Map<string, EventHandler[]>();
|
||||
this.setupConnectionHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up Redis connection event handlers
|
||||
*/
|
||||
private setupConnectionHandlers(): void {
|
||||
this.publisher.on('connect', () => {
|
||||
console.log('Publisher connected to Redis');
|
||||
this.isConnected = true;
|
||||
});
|
||||
|
||||
this.publisher.on('error', (err) => {
|
||||
console.error('Publisher Redis error', err);
|
||||
this.isConnected = false;
|
||||
});
|
||||
|
||||
this.subscriber.on('connect', () => {
|
||||
console.log('Subscriber connected to Redis');
|
||||
});
|
||||
|
||||
this.subscriber.on('error', (err) => {
|
||||
console.error('Subscriber Redis error', err);
|
||||
});
|
||||
|
||||
this.subscriber.on('message', (channel, message) => {
|
||||
try {
|
||||
const event = JSON.parse(message) as Event;
|
||||
this.processEvent(channel, event);
|
||||
} catch (err) {
|
||||
console.error('Error processing event', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an incoming event by calling all registered handlers
|
||||
*/
|
||||
private processEvent(channel: string, event: Event): void {
|
||||
const handlers = this.handlers.get(channel) || [];
|
||||
|
||||
handlers.forEach(handler => {
|
||||
try {
|
||||
handler(event);
|
||||
} catch (err) {
|
||||
console.error(`Error in event handler for ${channel}:`, err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish an event to the specified channel
|
||||
*/
|
||||
public async publish(channel: string, event: Event): Promise<void> {
|
||||
if (!this.isConnected) {
|
||||
throw new Error('Not connected to Redis');
|
||||
}
|
||||
|
||||
await this.publisher.publish(channel, JSON.stringify(event));
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to events on a specific channel
|
||||
*/
|
||||
public subscribe(channel: string, handler: EventHandler): () => void {
|
||||
if (!this.handlers.has(channel)) {
|
||||
this.handlers.set(channel, []);
|
||||
this.subscriber.subscribe(channel);
|
||||
}
|
||||
|
||||
const handlers = this.handlers.get(channel)!;
|
||||
handlers.push(handler);
|
||||
|
||||
// Return unsubscribe function
|
||||
return () => {
|
||||
const index = handlers.indexOf(handler);
|
||||
if (index >= 0) {
|
||||
handlers.splice(index, 1);
|
||||
}
|
||||
|
||||
if (handlers.length === 0) {
|
||||
this.handlers.delete(channel);
|
||||
this.subscriber.unsubscribe(channel);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Close connections
|
||||
*/
|
||||
public async close(): Promise<void> {
|
||||
await this.publisher.quit();
|
||||
await this.subscriber.quit();
|
||||
this.isConnected = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
import { EventBus, EventBusConfig, EventHandler } from './EventBus';
|
||||
|
||||
export { EventBus, EventBusConfig, EventHandler };
|
||||
|
||||
// Convenience function to create an event bus with the default configuration
|
||||
export function createEventBus(config: EventBusConfig): EventBus {
|
||||
return new EventBus(config);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue