@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
59 lines (56 loc) • 2.19 kB
JavaScript
import { shallowRef, ref, watch } from 'vue';
import { EVM_PLUGINS, PLUGIN_EVENTS } from '../base/plugin/IPlugin.js';
import { CONNECTED_STATUSES } from '../base/connector/connectorStatus.js';
function useWalletServicesInnerContextValue(web3AuthContext) {
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) {
const plugin = getPlugin(EVM_PLUGINS.WALLET_SERVICES);
walletServicesPlugin.value = plugin;
// When rehydrating, the connected listener may be registered after the connected event is emitted.
if (CONNECTED_STATUSES.includes(plugin === null || plugin === void 0 ? void 0 : plugin.status)) ready.value = true;
}
});
watch(walletServicesPlugin, (newWalletServicesPlugin, prevWalletServicesPlugin) => {
const connectedListener = () => {
ready.value = true;
};
const disconnectedListener = () => {
ready.value = false;
};
const connectingListener = () => {
connecting.value = true;
};
const previousPlugin = prevWalletServicesPlugin;
const currentPlugin = newWalletServicesPlugin;
if (previousPlugin && currentPlugin !== previousPlugin) {
previousPlugin.removeListener(PLUGIN_EVENTS.CONNECTED, connectedListener);
previousPlugin.removeListener(PLUGIN_EVENTS.DISCONNECTED, disconnectedListener);
previousPlugin.removeListener(PLUGIN_EVENTS.CONNECTING, connectingListener);
}
if (currentPlugin && currentPlugin !== previousPlugin) {
currentPlugin.on(PLUGIN_EVENTS.CONNECTED, connectedListener);
currentPlugin.on(PLUGIN_EVENTS.DISCONNECTED, disconnectedListener);
currentPlugin.on(PLUGIN_EVENTS.CONNECTING, connectingListener);
}
});
return {
plugin: walletServicesPlugin,
ready,
connecting
};
}
export { useWalletServicesInnerContextValue };