commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -98,7 +98,11 @@ 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 = unknown>(
|
||||
type: string,
|
||||
data: T,
|
||||
metadata?: Record<string, unknown>
|
||||
): Promise<void> {
|
||||
const message: EventBusMessage<T> = {
|
||||
id: this.generateId(),
|
||||
type,
|
||||
|
|
@ -129,9 +133,12 @@ export class EventBus extends EventEmitter {
|
|||
/**
|
||||
* Subscribe to an event
|
||||
*/
|
||||
async subscribe<T = any>(eventType: string, handler: EventHandler<T>): Promise<void> {
|
||||
async subscribe<T = unknown>(eventType: string, handler: EventHandler<T>): Promise<void> {
|
||||
// Register handler
|
||||
this.subscriptions.set(eventType, { channel: `events:${eventType}`, handler });
|
||||
this.subscriptions.set(eventType, {
|
||||
channel: `events:${eventType}`,
|
||||
handler: message => handler(message as EventBusMessage<T>),
|
||||
});
|
||||
|
||||
// Add local listener
|
||||
this.on(eventType, handler);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { defined } from '@stock-bot/invariants';
|
||||
import type { EventBusMessage, EventHandler, EventSubscription } from './types';
|
||||
|
||||
/**
|
||||
|
|
@ -18,7 +19,7 @@ export class SimpleEventBus {
|
|||
if (!this.subscriptions.has(channel)) {
|
||||
this.subscriptions.set(channel, new Set());
|
||||
}
|
||||
this.subscriptions.get(channel)!.add(subscription);
|
||||
defined(this.subscriptions.get(channel)).add(subscription);
|
||||
this.subscriptionById.set(id, { id, channel, handler });
|
||||
|
||||
return { channel, handler };
|
||||
|
|
@ -66,7 +67,7 @@ export class SimpleEventBus {
|
|||
}
|
||||
}
|
||||
|
||||
async publish(event: string, data: any): Promise<void> {
|
||||
async publish(event: string, data: unknown): Promise<void> {
|
||||
const message: EventBusMessage = {
|
||||
id: `msg-${this.nextId++}`,
|
||||
type: event,
|
||||
|
|
@ -93,14 +94,14 @@ export class SimpleEventBus {
|
|||
// Execute all handlers
|
||||
await Promise.all(
|
||||
handlers.map(handler =>
|
||||
Promise.resolve(handler(message)).catch(err => {
|
||||
Promise.resolve(handler(message)).catch(_err => {
|
||||
// Silently catch errors
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
publishSync(event: string, data: any): void {
|
||||
publishSync(event: string, data: unknown): void {
|
||||
const message: EventBusMessage = {
|
||||
id: `msg-${this.nextId++}`,
|
||||
type: event,
|
||||
|
|
@ -188,7 +189,7 @@ export class SimpleEventBus {
|
|||
}
|
||||
|
||||
hasSubscribers(event: string): boolean {
|
||||
return this.subscriptions.has(event) && this.subscriptions.get(event)!.size > 0;
|
||||
return this.subscriptions.has(event) && defined(this.subscriptions.get(event)).size > 0;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export interface EventBusMessage<T = any> {
|
||||
export interface EventBusMessage<T = unknown> {
|
||||
id: string;
|
||||
type: string;
|
||||
source: string;
|
||||
|
|
@ -7,7 +7,7 @@ export interface EventBusMessage<T = any> {
|
|||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface EventHandler<T = any> {
|
||||
export interface EventHandler<T = unknown> {
|
||||
(message: EventBusMessage<T>): Promise<void> | void;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue