Skip to content

Commit

Permalink
Add getEventBus func
Browse files Browse the repository at this point in the history
  • Loading branch information
mgavrila committed Dec 13, 2024
1 parent 90e0f33 commit 3e16b3b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/utils/getEventBus.ts
Original file line number Diff line number Diff line change
@@ -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<IEventBus | undefined> }
>(
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;
};

0 comments on commit 3e16b3b

Please sign in to comment.