fixed build libs

This commit is contained in:
Boki 2025-06-25 08:29:53 -04:00
parent b03231b849
commit 42baadae38
26 changed files with 981 additions and 541 deletions

View file

@ -1,51 +1,77 @@
import type { EventHandler, EventSubscription } from './types';
import type { EventHandler, EventSubscription, EventBusMessage } from './types';
/**
* Simple in-memory event bus for testing
*/
export class SimpleEventBus {
private subscriptions = new Map<string, Set<EventSubscription>>();
private subscriptionById = new Map<string, EventSubscription>();
private subscriptions = new Map<string, Set<{ id: string; handler: EventHandler }>>();
private subscriptionById = new Map<string, { id: string; channel: string; handler: EventHandler }>();
private nextId = 1;
subscribe(event: string, handler: EventHandler): EventSubscription {
const subscription: EventSubscription = {
id: `sub-${this.nextId++}`,
event,
handler,
pattern: event.includes('*'),
};
subscribe(channel: string, handler: EventHandler): EventSubscription {
const id = `sub-${this.nextId++}`;
const subscription = { id, handler };
if (!this.subscriptions.has(event)) {
this.subscriptions.set(event, new Set());
if (!this.subscriptions.has(channel)) {
this.subscriptions.set(channel, new Set());
}
this.subscriptions.get(event)!.add(subscription);
this.subscriptionById.set(subscription.id, subscription);
this.subscriptions.get(channel)!.add(subscription);
this.subscriptionById.set(id, { id, channel, handler });
return subscription;
return { channel, handler };
}
unsubscribe(idOrSubscription: string | EventSubscription): boolean {
const id = typeof idOrSubscription === 'string' ? idOrSubscription : idOrSubscription.id;
const subscription = this.subscriptionById.get(id);
if (!subscription) {
if (typeof idOrSubscription === 'string') {
const subscription = this.subscriptionById.get(idOrSubscription);
if (!subscription) {
return false;
}
const channelSubs = this.subscriptions.get(subscription.channel);
if (channelSubs) {
channelSubs.forEach(sub => {
if (sub.id === idOrSubscription) {
channelSubs.delete(sub);
}
});
if (channelSubs.size === 0) {
this.subscriptions.delete(subscription.channel);
}
}
this.subscriptionById.delete(idOrSubscription);
return true;
} else {
// Unsubscribe by matching handler and channel
const channelSubs = this.subscriptions.get(idOrSubscription.channel);
if (channelSubs) {
let removed = false;
channelSubs.forEach(sub => {
if (sub.handler === idOrSubscription.handler) {
channelSubs.delete(sub);
this.subscriptionById.delete(sub.id);
removed = true;
}
});
if (channelSubs.size === 0) {
this.subscriptions.delete(idOrSubscription.channel);
}
return removed;
}
return false;
}
const eventSubs = this.subscriptions.get(subscription.event);
if (eventSubs) {
eventSubs.delete(subscription);
if (eventSubs.size === 0) {
this.subscriptions.delete(subscription.event);
}
}
this.subscriptionById.delete(id);
return true;
}
async publish(event: string, data: any): Promise<void> {
const message: EventBusMessage = {
id: `msg-${this.nextId++}`,
type: event,
source: 'simple-event-bus',
timestamp: Date.now(),
data,
};
const handlers: EventHandler[] = [];
// Direct matches
@ -64,7 +90,7 @@ export class SimpleEventBus {
// Execute all handlers
await Promise.all(
handlers.map(handler =>
handler(data, event).catch(err => {
Promise.resolve(handler(message)).catch(err => {
// Silently catch errors
})
)
@ -72,6 +98,14 @@ export class SimpleEventBus {
}
publishSync(event: string, data: any): void {
const message: EventBusMessage = {
id: `msg-${this.nextId++}`,
type: event,
source: 'simple-event-bus',
timestamp: Date.now(),
data,
};
const handlers: EventHandler[] = [];
// Direct matches
@ -90,7 +124,7 @@ export class SimpleEventBus {
// Execute all handlers synchronously
handlers.forEach(handler => {
try {
handler(data, event);
handler(message);
} catch {
// Silently catch errors
}
@ -98,12 +132,20 @@ export class SimpleEventBus {
}
once(event: string, handler: EventHandler): EventSubscription {
const wrappedHandler: EventHandler = async (data, evt) => {
await handler(data, evt);
this.unsubscribe(subscription.id);
let subId: string;
const wrappedHandler: EventHandler = async (message) => {
await handler(message);
this.unsubscribe(subId);
};
const subscription = this.subscribe(event, wrappedHandler);
// Find the subscription ID
this.subscriptionById.forEach((value, key) => {
if (value.handler === wrappedHandler) {
subId = key;
}
});
return subscription;
}
@ -121,10 +163,19 @@ export class SimpleEventBus {
// Remove specific handler
const subs = this.subscriptions.get(event);
if (subs) {
const toRemove = Array.from(subs).filter(s => s.handler === handler);
toRemove.forEach(sub => {
subs.delete(sub);
this.subscriptionById.delete(sub.id);
const toRemove: string[] = [];
subs.forEach(sub => {
if (sub.handler === handler) {
toRemove.push(sub.id);
}
});
toRemove.forEach(id => {
subs.forEach(sub => {
if (sub.id === id) {
subs.delete(sub);
}
});
this.subscriptionById.delete(id);
});
if (subs.size === 0) {
this.subscriptions.delete(event);
@ -147,4 +198,4 @@ export class SimpleEventBus {
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
return regex.test(event);
}
}
}