refactor: fixed unsafe roles dialog and hook logic, added tests

This commit is contained in:
Dan Ditomaso
2025-03-10 20:30:04 -04:00
parent 97f2abb582
commit f1a58f0434
15 changed files with 460 additions and 274 deletions
+44
View File
@@ -0,0 +1,44 @@
export type EventMap = {
'dialog:unsafeRoles': {
action: 'confirm' | 'dismiss';
};
// add more events as required
};
export type EventName = keyof EventMap;
export type EventCallback<T extends EventName> = (data: EventMap[T]) => void;
class EventBus {
private listeners: { [K in EventName]?: Array<EventCallback<K>> } = {};
public on<T extends EventName>(event: T, callback: EventCallback<T>): () => void {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event]?.push(callback as any);
return () => {
this.off(event, callback);
};
}
public off<T extends EventName>(event: T, callback: EventCallback<T>): void {
if (!this.listeners[event]) return;
const callbackIndex = this.listeners[event]?.indexOf(callback as any);
if (callbackIndex !== undefined && callbackIndex > -1) {
this.listeners[event]?.splice(callbackIndex, 1);
}
}
public emit<T extends EventName>(event: T, data: EventMap[T]): void {
if (!this.listeners[event]) return;
this.listeners[event]?.forEach(callback => {
callback(data);
});
}
}
export const eventBus = new EventBus();