mirror of
https://github.com/streamyfin/streamyfin.git
synced 2025-08-20 18:37:18 +02:00
feat: focus search bar on second tab press (#558)
This commit is contained in:
committed by
GitHub
parent
5590c2f784
commit
af2bd030e9
26
utils/eventBus.ts
Normal file
26
utils/eventBus.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
type Listener<T = void> = (data?: T) => void;
|
||||
|
||||
class EventBus {
|
||||
private listeners: Record<string, Listener<any>[]> = {};
|
||||
|
||||
on<T = void>(event: string, callback: Listener<T>): () => void {
|
||||
if (!this.listeners[event]) {
|
||||
this.listeners[event] = [];
|
||||
}
|
||||
this.listeners[event].push(callback);
|
||||
return () => this.off(event, callback);
|
||||
}
|
||||
|
||||
off<T = void>(event: string, callback: Listener<T>): void {
|
||||
if (!this.listeners[event]) return;
|
||||
this.listeners[event] = this.listeners[event].filter(
|
||||
(fn) => fn !== callback
|
||||
);
|
||||
}
|
||||
|
||||
emit<T = void>(event: string, data?: T): void {
|
||||
this.listeners[event]?.forEach((callback) => callback(data));
|
||||
}
|
||||
}
|
||||
|
||||
export const eventBus = new EventBus();
|
||||
Reference in New Issue
Block a user