From d091846502bed6b783b69ab8eff7ae36d8e25449 Mon Sep 17 00:00:00 2001 From: Patrick Lafrance Date: Tue, 13 Feb 2024 15:30:31 -0500 Subject: [PATCH] fix: Adding a generic type for the event bus payload (#150) * Adding a generic type for event bus payload * Added changeset file --- .changeset/chatty-hairs-lay.md | 5 +++++ docs/getting-started/deploy.md | 2 +- packages/core/src/messaging/eventBus.ts | 14 +++++++------- .../core/src/messaging/useEventBusDispatcher.ts | 8 ++++---- packages/core/src/messaging/useEventBusListener.ts | 4 ++-- packages/core/src/runtime/useEventBus.ts | 4 ++-- 6 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 .changeset/chatty-hairs-lay.md diff --git a/.changeset/chatty-hairs-lay.md b/.changeset/chatty-hairs-lay.md new file mode 100644 index 000000000..d956912ec --- /dev/null +++ b/.changeset/chatty-hairs-lay.md @@ -0,0 +1,5 @@ +--- +"@squide/core": patch +--- + +Adding a generic type for the event bus payload. diff --git a/docs/getting-started/deploy.md b/docs/getting-started/deploy.md index f10c15f82..8ebd6b851 100644 --- a/docs/getting-started/deploy.md +++ b/docs/getting-started/deploy.md @@ -16,7 +16,7 @@ To enable support for direct page hits, add the following redirect rule to your /* /index.html 200 ``` -For Netlify, it can either be with a `netlify.toml` file at the root of project: +For [Netlify](https://www.netlify.com/), it can either be with a `netlify.toml` file at the root of project: ```netlify.toml [[redirects]] diff --git a/packages/core/src/messaging/eventBus.ts b/packages/core/src/messaging/eventBus.ts index 4046502ff..0dcea1220 100644 --- a/packages/core/src/messaging/eventBus.ts +++ b/packages/core/src/messaging/eventBus.ts @@ -3,7 +3,7 @@ import type { Logger } from "../logging/logger.ts"; export type EventName = string | symbol; -export type EventCallbackFunction = (data?: unknown) => void; +export type EventCallbackFunction = (data?: TPayload) => void; export interface EventBusOptions { logger?: Logger; @@ -17,7 +17,7 @@ export interface RemoveListenerOptions { once?: boolean; } -export class EventBus { +export class EventBus { readonly #eventEmitter: EventEmitter; #logger?: Logger; @@ -26,7 +26,7 @@ export class EventBus { this.#logger = logger; } - addListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: AddListenerOptions = {}) { + addListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: AddListenerOptions = {}) { if (once === true) { this.#eventEmitter.once(eventName, callback); } else { @@ -34,13 +34,13 @@ export class EventBus { } } - removeListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: RemoveListenerOptions = {}) { + removeListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: RemoveListenerOptions = {}) { this.#eventEmitter.removeListener(eventName, callback, once); } - dispatch(eventName: TEventNames, data?: unknown) { - this.#logger?.debug(`[squide] Dispatching event "${String(eventName)}"`, data); + dispatch(eventName: TEventNames, payload?: TPayload) { + this.#logger?.debug(`[squide] Dispatching event "${String(eventName)}"`, payload); - this.#eventEmitter.emit(eventName, data); + this.#eventEmitter.emit(eventName, payload); } } diff --git a/packages/core/src/messaging/useEventBusDispatcher.ts b/packages/core/src/messaging/useEventBusDispatcher.ts index 6ba0857a7..119aa935a 100644 --- a/packages/core/src/messaging/useEventBusDispatcher.ts +++ b/packages/core/src/messaging/useEventBusDispatcher.ts @@ -2,10 +2,10 @@ import { useCallback } from "react"; import { useEventBus } from "../runtime/useEventBus.ts"; import type { EventName } from "./eventBus.ts"; -export function useEventBusDispatcher() { - const eventBus = useEventBus(); +export function useEventBusDispatcher() { + const eventBus = useEventBus(); - return useCallback((eventName: TEventNames, data?: unknown) => { - eventBus.dispatch(eventName, data); + return useCallback((eventName: TEventNames, payload?: TPayload) => { + eventBus.dispatch(eventName, payload); }, [eventBus]); } diff --git a/packages/core/src/messaging/useEventBusListener.ts b/packages/core/src/messaging/useEventBusListener.ts index 8f86cb76e..84b436a9f 100644 --- a/packages/core/src/messaging/useEventBusListener.ts +++ b/packages/core/src/messaging/useEventBusListener.ts @@ -3,8 +3,8 @@ import type { AddListenerOptions, EventCallbackFunction, EventName } from "./eve import { useEffect } from "react"; import { useEventBus } from "../runtime/useEventBus.ts"; -export function useEventBusListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: AddListenerOptions = {}) { - const eventBus = useEventBus(); +export function useEventBusListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: AddListenerOptions = {}) { + const eventBus = useEventBus(); return useEffect(() => { eventBus.addListener(eventName, callback, { once }); diff --git a/packages/core/src/runtime/useEventBus.ts b/packages/core/src/runtime/useEventBus.ts index 3214a297a..4e84e27fc 100644 --- a/packages/core/src/runtime/useEventBus.ts +++ b/packages/core/src/runtime/useEventBus.ts @@ -2,8 +2,8 @@ import type { EventBus, EventName } from "../messaging/eventBus.ts"; import { useRuntime } from "./RuntimeContext.ts"; -export function useEventBus() { +export function useEventBus() { const runtime = useRuntime(); - return runtime.eventBus as unknown as EventBus; + return runtime.eventBus as unknown as EventBus; }