UNPKG

@web3auth/modal

Version:

Multi chain wallet aggregator for web3Auth

70 lines (66 loc) 3.06 kB
import { WalletServicesPluginError, EVM_PLUGINS, CONNECTOR_STATUS, PLUGIN_EVENTS } from '@web3auth/no-modal'; import { WalletServicesContextKey } from '@web3auth/no-modal/vue'; import { defineComponent, h, shallowRef, ref, watch, provide } from 'vue'; import { useWeb3AuthInner } from './composables/useWeb3AuthInner.js'; const WalletServicesInnerProvider = defineComponent({ name: "WalletServicesInnerProvider", setup() { const web3AuthContext = useWeb3AuthInner(); if (!web3AuthContext) throw WalletServicesPluginError.fromCode(1000, "`WalletServicesProvider` must be wrapped by `Web3AuthProvider`"); const { getPlugin, isInitialized, isConnected } = web3AuthContext; const walletServicesPlugin = shallowRef(null); const ready = ref(false); const connecting = ref(false); watch(isInitialized, newIsInitialized => { if (newIsInitialized) { const plugin = getPlugin(EVM_PLUGINS.WALLET_SERVICES); walletServicesPlugin.value = plugin; } }); watch(isConnected, newIsConnected => { if (newIsConnected) { var _walletServicesPlugin; const plugin = getPlugin(EVM_PLUGINS.WALLET_SERVICES); if (!walletServicesPlugin.value) walletServicesPlugin.value = plugin; // when rehydrating, the connectedListener may be registered after the connected event is emitted, we need to check the status here if (((_walletServicesPlugin = walletServicesPlugin.value) === null || _walletServicesPlugin === void 0 ? void 0 : _walletServicesPlugin.status) === CONNECTOR_STATUS.CONNECTED) ready.value = true; } }); watch(walletServicesPlugin, (newWalletServicesPlugin, prevWalletServicesPlugin) => { const connectedListener = () => { ready.value = true; }; const disconnectedListener = () => { ready.value = false; }; const connectingListener = () => { connecting.value = true; }; // unregister previous listeners if (prevWalletServicesPlugin && newWalletServicesPlugin !== prevWalletServicesPlugin) { prevWalletServicesPlugin.off(PLUGIN_EVENTS.CONNECTED, connectedListener); prevWalletServicesPlugin.off(PLUGIN_EVENTS.DISCONNECTED, disconnectedListener); prevWalletServicesPlugin.off(PLUGIN_EVENTS.CONNECTING, connectingListener); } if (newWalletServicesPlugin && newWalletServicesPlugin !== prevWalletServicesPlugin) { newWalletServicesPlugin.on(PLUGIN_EVENTS.CONNECTED, connectedListener); newWalletServicesPlugin.on(PLUGIN_EVENTS.DISCONNECTED, disconnectedListener); newWalletServicesPlugin.on(PLUGIN_EVENTS.CONNECTING, connectingListener); } }); provide(WalletServicesContextKey, { plugin: walletServicesPlugin, ready, connecting }); }, render() { var _this$$slots$default; return h((_this$$slots$default = this.$slots.default) !== null && _this$$slots$default !== void 0 ? _this$$slots$default : ""); } }); export { WalletServicesInnerProvider };