From c4cd44070780c2849b92291d0865b04e11cc9b7c Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:46:40 +0300 Subject: [PATCH] refactor: Combine event-bus and typed-event-bus --- .../src/components/N8nFormBox/FormBox.vue | 4 +- packages/design-system/src/utils/event-bus.ts | 48 ++++++++++++++++--- .../design-system/src/utils/form-event-bus.ts | 6 +-- packages/design-system/src/utils/index.ts | 1 - .../src/utils/typed-event-bus.ts | 36 -------------- packages/editor-ui/src/components/Modal.vue | 7 ++- packages/editor-ui/src/event-bus/mfa.ts | 9 ++-- 7 files changed, 53 insertions(+), 58 deletions(-) delete mode 100644 packages/design-system/src/utils/typed-event-bus.ts diff --git a/packages/design-system/src/components/N8nFormBox/FormBox.vue b/packages/design-system/src/components/N8nFormBox/FormBox.vue index ed01219eb914e..7881c4b5c9ba4 100644 --- a/packages/design-system/src/components/N8nFormBox/FormBox.vue +++ b/packages/design-system/src/components/N8nFormBox/FormBox.vue @@ -44,7 +44,7 @@ import N8nHeading from '../N8nHeading'; import N8nLink from '../N8nLink'; import N8nButton from '../N8nButton'; import type { IFormInput } from 'n8n-design-system/types'; -import { createEventBus } from '../../utils'; +import { createFormEventBus } from '../../utils'; interface FormBoxProps { title?: string; @@ -67,7 +67,7 @@ withDefaults(defineProps(), { redirectLink: '', }); -const formBus = createEventBus(); +const formBus = createFormEventBus(); const emit = defineEmits<{ submit: [value: { [key: string]: Value }]; update: [value: { name: string; value: Value }]; diff --git a/packages/design-system/src/utils/event-bus.ts b/packages/design-system/src/utils/event-bus.ts index 23f5e5f02acf8..1b7cb10182a48 100644 --- a/packages/design-system/src/utils/event-bus.ts +++ b/packages/design-system/src/utils/event-bus.ts @@ -2,17 +2,51 @@ export type CallbackFn = Function; export type UnregisterFn = () => void; -export interface EventBus { - on: (eventName: string, fn: CallbackFn) => UnregisterFn; - once: (eventName: string, fn: CallbackFn) => UnregisterFn; - off: (eventName: string, fn: CallbackFn) => void; - emit: (eventName: string, event?: T) => void; +export type Listener = (payload: Payload) => void; + +export type Payloads = { + [E in keyof ListenerMap]: unknown; +}; + +// TODO: Fix all usages of `createEventBus` and convert `any` to `unknown` +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export interface EventBus = Record> { + on: ( + eventName: EventName, + fn: Listener, + ) => UnregisterFn; + + once: ( + eventName: EventName, + fn: Listener, + ) => UnregisterFn; + + off: ( + eventName: EventName, + fn: Listener, + ) => void; + + emit: ( + eventName: EventName, + event?: ListenerMap[EventName], + ) => void; } /** - * @deprecated Use the typed version instead from `typed-event-bus.ts` + * Creates an event bus with the given listener map. + * + * @example + * ```ts + * const eventBus = createEventBus<{ + * 'user-logged-in': { username: string }; + * 'user-logged-out': never; + * }>(); */ -export function createEventBus(): EventBus { +export function createEventBus< + // TODO: Fix all usages of `createEventBus` and convert `any` to `unknown` + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ListenerMap extends Payloads = Record, +>(): EventBus { const handlers = new Map(); function off(eventName: string, fn: CallbackFn) { diff --git a/packages/design-system/src/utils/form-event-bus.ts b/packages/design-system/src/utils/form-event-bus.ts index 65ecf9dbfee3f..5b518c8a4233d 100644 --- a/packages/design-system/src/utils/form-event-bus.ts +++ b/packages/design-system/src/utils/form-event-bus.ts @@ -1,4 +1,4 @@ -import { createTypedEventBus } from './typed-event-bus'; +import { createEventBus } from './event-bus'; export interface FormEventBusEvents { submit: never; @@ -9,6 +9,4 @@ export type FormEventBus = ReturnType; /** * Creates a new event bus to be used with the `FormInputs` component. */ -export function createFormEventBus() { - return createTypedEventBus(); -} +export const createFormEventBus = createEventBus; diff --git a/packages/design-system/src/utils/index.ts b/packages/design-system/src/utils/index.ts index e461ee8d632d1..3f4ed339f0f73 100644 --- a/packages/design-system/src/utils/index.ts +++ b/packages/design-system/src/utils/index.ts @@ -1,5 +1,4 @@ export * from './event-bus'; -export * from './typed-event-bus'; export * from './form-event-bus'; export * from './markdown'; export * from './typeguards'; diff --git a/packages/design-system/src/utils/typed-event-bus.ts b/packages/design-system/src/utils/typed-event-bus.ts deleted file mode 100644 index 8290b68fb2a8a..0000000000000 --- a/packages/design-system/src/utils/typed-event-bus.ts +++ /dev/null @@ -1,36 +0,0 @@ -import type { UnregisterFn } from './event-bus'; -import { createEventBus as createUntypedEventBus } from './event-bus'; - -export type Listener = (payload: Payload) => void; - -export type Payloads = { - [E in keyof ListenerMap]: unknown; -}; - -export interface TypedEventBus> { - on: ( - eventName: EventName, - fn: Listener, - ) => UnregisterFn; - - once: ( - eventName: EventName, - fn: Listener, - ) => UnregisterFn; - - off: ( - eventName: EventName, - fn: Listener, - ) => void; - - emit: ( - eventName: EventName, - event?: ListenerMap[EventName], - ) => void; -} - -export function createTypedEventBus< - ListenerMap extends Payloads, ->(): TypedEventBus { - return createUntypedEventBus() as TypedEventBus; -} diff --git a/packages/editor-ui/src/components/Modal.vue b/packages/editor-ui/src/components/Modal.vue index 7f4a8334af6fa..141e93a521472 100644 --- a/packages/editor-ui/src/components/Modal.vue +++ b/packages/editor-ui/src/components/Modal.vue @@ -1,7 +1,7 @@