fix: add missing event bus

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2024-10-25 21:42:25 +08:00
parent d1d3fcf493
commit 33b3ac4c53
3 changed files with 19 additions and 7 deletions
@@ -0,0 +1,10 @@
import { createEventBus } from "@follow/utils/event-bus"
export const ModalEventBus = createEventBus<{
MODAL_DISPATCH: ModalDisposeEvent
}>()
export type ModalDisposeEvent = {
type: "dismiss"
id: string
}
+1 -1
View File
@@ -295,7 +295,7 @@ const TrendingEntries = ({ data }: { data: Models.TrendingEntry[] }) => {
return (
<li
key={entry.id}
className="relative truncate whitespace-nowrap py-0.5 pr-10 marker:text-accent"
className="relative w-[calc(100%-2.5rem)] truncate whitespace-nowrap py-0.5 marker:text-accent"
>
<a
href={entry.url}
+8 -6
View File
@@ -11,12 +11,13 @@ class EventBusEvent extends Event {
super(EventBusEvent.type)
}
}
class EventBusStatic {
dispatch<T extends keyof EventBusMap>(event: T, data: EventBusMap[T]) {
window.dispatchEvent(new EventBusEvent(event, data))
type AnyObject = Record<string, any>
class EventBusStatic<E extends AnyObject> {
dispatch<T extends keyof E>(event: T, data: E[T]) {
window.dispatchEvent(new EventBusEvent(event as string, data))
}
subscribe<T extends keyof EventBusMap>(event: T, callback: (data: EventBusMap[T]) => void) {
subscribe<T extends keyof E>(event: T, callback: (data: E[T]) => void) {
const handler = (e: any) => {
if (e instanceof EventBusEvent && e._type === event) {
callback(e.data)
@@ -24,7 +25,7 @@ class EventBusStatic {
}
window.addEventListener(EventBusEvent.type, handler)
return this.unsubscribe.bind(this, event, handler)
return this.unsubscribe.bind(this, event as string, handler)
}
unsubscribe(event: string, handler: (e: any) => void) {
@@ -32,4 +33,5 @@ class EventBusStatic {
}
}
export const EventBus = new EventBusStatic()
export const EventBus = new EventBusStatic<EventBusMap>()
export const createEventBus = <E extends AnyObject>() => new EventBusStatic<E>()