From 10b0d903925c53a7ab328a2dd9eaf17bc1e1849f Mon Sep 17 00:00:00 2001 From: Gavrila Andrei Date: Tue, 10 Dec 2024 16:04:31 +0200 Subject: [PATCH] Remove getEventBus helper --- src/core/providers/helpers/getConfig.ts | 23 ++++++++++++-- src/core/providers/helpers/getEventBus.ts | 37 ----------------------- 2 files changed, 20 insertions(+), 40 deletions(-) delete mode 100644 src/core/providers/helpers/getEventBus.ts diff --git a/src/core/providers/helpers/getConfig.ts b/src/core/providers/helpers/getConfig.ts index f98243f..764941a 100644 --- a/src/core/providers/helpers/getConfig.ts +++ b/src/core/providers/helpers/getConfig.ts @@ -1,5 +1,8 @@ +import type { LedgerConnectModal } from '@multiversx/sdk-dapp-core-ui/dist/components/ledger-connect-modal'; +import type { WalletConnectModal } from '@multiversx/sdk-dapp-core-ui/dist/components/wallet-connect-modal'; + +import { defineCustomElements } from '@multiversx/sdk-dapp-core-ui/loader'; import { safeWindow } from 'constants/index'; -import { getEventBus } from './getEventBus'; import { IProviderConfig, IProviderConfigUI, @@ -29,13 +32,27 @@ export const getConfig = async (config: IProviderConfig = defaultConfig) => { const UI = { [ProviderTypeEnum.ledger]: { mount: async () => { - const eventBus = await getEventBus('ledger-connect-modal'); + defineCustomElements(safeWindow); + const ledgerModalElement = document.createElement( + 'ledger-connect-modal' + ) as LedgerConnectModal; + + document.body.appendChild(ledgerModalElement); + + const eventBus = await ledgerModalElement.getEventBus(); return eventBus; } }, [ProviderTypeEnum.walletConnect]: { mount: async () => { - const eventBus = await getEventBus('wallet-connect-modal'); + defineCustomElements(safeWindow); + const walletConnectModalElement = document.createElement( + 'wallet-connect-modal' + ) as WalletConnectModal; + + document.body.appendChild(walletConnectModalElement); + await customElements.whenDefined('wallet-connect-modal'); + const eventBus = await walletConnectModalElement.getEventBus(); return eventBus; } } diff --git a/src/core/providers/helpers/getEventBus.ts b/src/core/providers/helpers/getEventBus.ts deleted file mode 100644 index e281e00..0000000 --- a/src/core/providers/helpers/getEventBus.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { defineCustomElements } from '@multiversx/sdk-dapp-core-ui/loader'; -import { safeWindow } from 'constants/index'; -import { IEventBus } from '../types/providerFactory.types'; - -// Retrieve eventBus from webcomponent -export async function getEventBus( - tagName: string -): Promise { - const existingModal = document.querySelector(tagName); - if (existingModal) { - existingModal.remove(); - } - - defineCustomElements(safeWindow); - - const modalElement = document.createElement(tagName) as T; - - document.body.appendChild(modalElement); - console.log(`${tagName} element appended:`, { modalElement }); - - return new Promise((resolve, reject) => { - // Check every 100ms if component has been mounted - const checkInitialization = setInterval(async () => { - try { - const eventBus = await (modalElement as any).getEventBus(); - clearInterval(checkInitialization); - resolve(eventBus); - } catch (error) { - console.error('Error getting event bus:', error); - if (!document.body.contains(modalElement)) { - clearInterval(checkInitialization); - reject(new Error(`${tagName} was removed before initialization.`)); - } - } - }, 100); - }); -}