Skip to content

Commit

Permalink
fix: Adding a generic type for the event bus payload (#150)
Browse files Browse the repository at this point in the history
* Adding a generic type for event bus payload

* Added changeset file
  • Loading branch information
patricklafrance authored Feb 13, 2024
1 parent 9424492 commit d091846
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-hairs-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@squide/core": patch
---

Adding a generic type for the event bus payload.
2 changes: 1 addition & 1 deletion docs/getting-started/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/messaging/eventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TPayload = unknown> = (data?: TPayload) => void;

export interface EventBusOptions {
logger?: Logger;
Expand All @@ -17,7 +17,7 @@ export interface RemoveListenerOptions {
once?: boolean;
}

export class EventBus<TEventNames extends EventName = EventName> {
export class EventBus<TEventNames extends EventName = EventName, TPayload = unknown> {
readonly #eventEmitter: EventEmitter;
#logger?: Logger;

Expand All @@ -26,21 +26,21 @@ export class EventBus<TEventNames extends EventName = EventName> {
this.#logger = logger;
}

addListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: AddListenerOptions = {}) {
addListener(eventName: TEventNames, callback: EventCallbackFunction<TPayload>, { once }: AddListenerOptions = {}) {
if (once === true) {
this.#eventEmitter.once(eventName, callback);
} else {
this.#eventEmitter.addListener(eventName, callback);
}
}

removeListener(eventName: TEventNames, callback: EventCallbackFunction, { once }: RemoveListenerOptions = {}) {
removeListener(eventName: TEventNames, callback: EventCallbackFunction<TPayload>, { 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);
}
}
8 changes: 4 additions & 4 deletions packages/core/src/messaging/useEventBusDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { useCallback } from "react";
import { useEventBus } from "../runtime/useEventBus.ts";
import type { EventName } from "./eventBus.ts";

export function useEventBusDispatcher<TEventNames extends EventName>() {
const eventBus = useEventBus<TEventNames>();
export function useEventBusDispatcher<TEventNames extends EventName, TPayload = unknown>() {
const eventBus = useEventBus<TEventNames, TPayload>();

return useCallback((eventName: TEventNames, data?: unknown) => {
eventBus.dispatch(eventName, data);
return useCallback((eventName: TEventNames, payload?: TPayload) => {
eventBus.dispatch(eventName, payload);
}, [eventBus]);
}
4 changes: 2 additions & 2 deletions packages/core/src/messaging/useEventBusListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { AddListenerOptions, EventCallbackFunction, EventName } from "./eve
import { useEffect } from "react";
import { useEventBus } from "../runtime/useEventBus.ts";

export function useEventBusListener<TEventNames extends EventName = EventName>(eventName: TEventNames, callback: EventCallbackFunction, { once }: AddListenerOptions = {}) {
const eventBus = useEventBus<TEventNames>();
export function useEventBusListener<TEventNames extends EventName = EventName, TPayload = unknown>(eventName: TEventNames, callback: EventCallbackFunction<TPayload>, { once }: AddListenerOptions = {}) {
const eventBus = useEventBus<TEventNames, TPayload>();

return useEffect(() => {
eventBus.addListener(eventName, callback, { once });
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/runtime/useEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { EventBus, EventName } from "../messaging/eventBus.ts";

import { useRuntime } from "./RuntimeContext.ts";

export function useEventBus<TEventNames extends EventName>() {
export function useEventBus<TEventNames extends EventName, TPayload = unknown>() {
const runtime = useRuntime();

return runtime.eventBus as unknown as EventBus<TEventNames>;
return runtime.eventBus as unknown as EventBus<TEventNames, TPayload>;
}

0 comments on commit d091846

Please sign in to comment.