This commit is contained in:
Boki 2025-06-25 11:38:23 -04:00
parent 3a7254708e
commit b63e58784c
41 changed files with 5762 additions and 4477 deletions

View file

@ -1,11 +1,14 @@
import type { EventHandler, EventSubscription, EventBusMessage } from './types';
import type { EventBusMessage, EventHandler, EventSubscription } from './types';
/**
* Simple in-memory event bus for testing
*/
export class SimpleEventBus {
private subscriptions = new Map<string, Set<{ id: string; handler: EventHandler }>>();
private subscriptionById = new Map<string, { id: string; channel: string; handler: EventHandler }>();
private subscriptionById = new Map<
string,
{ id: string; channel: string; handler: EventHandler }
>();
private nextId = 1;
subscribe(channel: string, handler: EventHandler): EventSubscription {
@ -27,7 +30,7 @@ export class SimpleEventBus {
if (!subscription) {
return false;
}
const channelSubs = this.subscriptions.get(subscription.channel);
if (channelSubs) {
channelSubs.forEach(sub => {
@ -39,7 +42,7 @@ export class SimpleEventBus {
this.subscriptions.delete(subscription.channel);
}
}
this.subscriptionById.delete(idOrSubscription);
return true;
} else {
@ -133,7 +136,7 @@ export class SimpleEventBus {
once(event: string, handler: EventHandler): EventSubscription {
let subId: string;
const wrappedHandler: EventHandler = async (message) => {
const wrappedHandler: EventHandler = async message => {
await handler(message);
this.unsubscribe(subId);
};
@ -145,7 +148,7 @@ export class SimpleEventBus {
subId = key;
}
});
return subscription;
}
@ -198,4 +201,4 @@ export class SimpleEventBus {
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
return regex.test(event);
}
}
}