From 3e16b3baf80990617eac01573ccc796162a121a9 Mon Sep 17 00:00:00 2001 From: Gavrila Andrei Date: Fri, 13 Dec 2024 12:58:32 +0200 Subject: [PATCH] Add getEventBus func --- src/utils/getEventBus.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/utils/getEventBus.ts diff --git a/src/utils/getEventBus.ts b/src/utils/getEventBus.ts new file mode 100644 index 0000000..78a4d43 --- /dev/null +++ b/src/utils/getEventBus.ts @@ -0,0 +1,23 @@ +import { IEventBus } from '@multiversx/sdk-dapp-core-ui/loader'; +import { safeWindow } from 'constants/index'; +import { defineCustomElements } from 'lib/sdkDappCoreUi'; + +export const getEventBus = async < + T extends HTMLElement & { getEventBus: () => Promise } +>( + tagName: string +) => { + await defineCustomElements(safeWindow); + + const modalElement = document.createElement(tagName) as T; + document.body.appendChild(modalElement); + await customElements.whenDefined(tagName); + + const eventBus = await modalElement.getEventBus(); + + if (!eventBus) { + throw new Error('Event bus not provided.'); + } + + return eventBus; +};