import { type Config, type GetConnectionReturnType, getConnection, type ResolvedRegister, watchConnection, } from '@wagmi/core' import type { ConfigParameter } from '@wagmi/core/internal' import { onScopeDispose, reactive, readonly, type ToRefs, toRefs } from 'vue' import { updateState } from '../utils/updateState.js' import { useConfig } from './useConfig.js' export type UseConnectionParameters = ConfigParameter export type UseConnectionReturnType = ToRefs< GetConnectionReturnType > /** https://wagmi.sh/vue/api/composables/useConnection */ export function useConnection< config extends Config = ResolvedRegister['config'], >( parameters: UseConnectionParameters = {}, ): UseConnectionReturnType { const config = useConfig(parameters) const connection = reactive(getConnection(config)) const unsubscribe = watchConnection(config, { onChange(data) { updateState(connection, data) }, }) onScopeDispose(() => unsubscribe()) return toRefs(readonly(connection)) as UseConnectionReturnType }