format
This commit is contained in:
parent
d858222af7
commit
7d9044ab29
202 changed files with 10755 additions and 10972 deletions
|
|
@ -1,12 +1,7 @@
|
|||
import { EventEmitter } from 'eventemitter3';
|
||||
import Redis from 'ioredis';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type {
|
||||
EventBusConfig,
|
||||
EventBusMessage,
|
||||
EventHandler,
|
||||
EventSubscription,
|
||||
} from './types';
|
||||
import type { EventBusConfig, EventBusMessage, EventHandler, EventSubscription } from './types';
|
||||
|
||||
/**
|
||||
* Lightweight Event Bus for inter-service communication
|
||||
|
|
@ -52,7 +47,7 @@ export class EventBus extends EventEmitter {
|
|||
this.isConnected = true;
|
||||
});
|
||||
|
||||
this.publisher.on('error', (error) => {
|
||||
this.publisher.on('error', error => {
|
||||
this.logger.error('Publisher Redis error:', error);
|
||||
});
|
||||
|
||||
|
|
@ -63,7 +58,7 @@ export class EventBus extends EventEmitter {
|
|||
this.resubscribeAll();
|
||||
});
|
||||
|
||||
this.subscriber.on('error', (error) => {
|
||||
this.subscriber.on('error', error => {
|
||||
this.logger.error('Subscriber Redis error:', error);
|
||||
});
|
||||
|
||||
|
|
@ -89,7 +84,7 @@ export class EventBus extends EventEmitter {
|
|||
// Call registered handler if exists
|
||||
const subscription = this.subscriptions.get(eventType);
|
||||
if (subscription?.handler) {
|
||||
Promise.resolve(subscription.handler(eventMessage)).catch((error) => {
|
||||
Promise.resolve(subscription.handler(eventMessage)).catch(error => {
|
||||
this.logger.error(`Handler error for event ${eventType}:`, error);
|
||||
});
|
||||
}
|
||||
|
|
@ -103,11 +98,7 @@ export class EventBus extends EventEmitter {
|
|||
/**
|
||||
* Publish an event
|
||||
*/
|
||||
async publish<T = any>(
|
||||
type: string,
|
||||
data: T,
|
||||
metadata?: Record<string, any>
|
||||
): Promise<void> {
|
||||
async publish<T = any>(type: string, data: T, metadata?: Record<string, any>): Promise<void> {
|
||||
const message: EventBusMessage<T> = {
|
||||
id: this.generateId(),
|
||||
type,
|
||||
|
|
@ -199,11 +190,11 @@ export class EventBus extends EventEmitter {
|
|||
*/
|
||||
async waitForConnection(timeout: number = 5000): Promise<void> {
|
||||
const startTime = Date.now();
|
||||
|
||||
|
||||
while (!this.isConnected && Date.now() - startTime < timeout) {
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
|
||||
|
||||
if (!this.isConnected) {
|
||||
throw new Error(`Failed to connect to Redis within ${timeout}ms`);
|
||||
}
|
||||
|
|
@ -220,10 +211,7 @@ export class EventBus extends EventEmitter {
|
|||
this.removeAllListeners();
|
||||
|
||||
// Close Redis connections
|
||||
await Promise.all([
|
||||
this.publisher.quit(),
|
||||
this.subscriber.quit(),
|
||||
]);
|
||||
await Promise.all([this.publisher.quit(), this.subscriber.quit()]);
|
||||
|
||||
this.logger.info('Event bus closed');
|
||||
}
|
||||
|
|
@ -248,4 +236,4 @@ export class EventBus extends EventEmitter {
|
|||
get service(): string {
|
||||
return this.serviceName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue