Refactor authentication and async operation handling in useAuth composable

- Introduce useMultiAsyncOperation to manage multiple async operations in useAuth, enhancing error handling and loading state management.
- Replace manual loading and error state management with standardized async operation patterns for initialize, login, register, and logout functions.
- Update related components to utilize the new async operation structure, improving code clarity and maintainability.
- Add useAsyncOperation to other composables (useChat, useTicketPurchase, useMarket) for consistent async handling across the application.
This commit is contained in:
padreug 2025-09-05 06:08:08 +02:00
parent e0443742c5
commit 7c439361b7
5 changed files with 298 additions and 133 deletions

View file

@ -2,6 +2,7 @@ import { ref, computed } from 'vue'
import { injectService } from '@/core/di-container'
import type { ChatService } from '../services/chat-service'
import type { ChatPeer } from '../types'
import { useMultiAsyncOperation } from '@/core/composables/useAsyncOperation'
// Service token for chat service
export const CHAT_SERVICE_TOKEN = Symbol('chatService')
@ -14,8 +15,15 @@ export function useChat() {
}
const selectedPeer = ref<string | null>(null)
const isLoading = ref(false)
const error = ref<string | null>(null)
// Async operations
const asyncOps = useMultiAsyncOperation<{
sendMessage: void
refreshPeers: void
}>()
const sendMessageOp = asyncOps.createOperation('sendMessage')
const refreshPeersOp = asyncOps.createOperation('refreshPeers')
// Computed properties
const peers = computed(() => chatService.allPeers.value)
@ -41,17 +49,11 @@ export function useChat() {
return
}
isLoading.value = true
error.value = null
try {
await chatService.sendMessage(selectedPeer.value, content.trim())
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to send message'
console.error('Send message error:', err)
} finally {
isLoading.value = false
}
return await sendMessageOp.execute(async () => {
await chatService.sendMessage(selectedPeer.value!, content.trim())
}, {
errorMessage: 'Failed to send message'
})
}
const addPeer = (pubkey: string, name?: string): ChatPeer => {
@ -63,23 +65,22 @@ export function useChat() {
}
const refreshPeers = async () => {
isLoading.value = true
error.value = null
try {
return await refreshPeersOp.execute(async () => {
await chatService.refreshPeers()
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to refresh peers'
console.error('Failed to refresh peers:', err)
} finally {
isLoading.value = false
}
}, {
errorMessage: 'Failed to refresh peers'
})
}
return {
// State
selectedPeer,
isLoading,
error,
isSendingMessage: sendMessageOp.isLoading,
sendMessageError: sendMessageOp.error,
isRefreshingPeers: refreshPeersOp.isLoading,
refreshPeersError: refreshPeersOp.error,
isLoading: computed(() => asyncOps.isAnyLoading()),
error: computed(() => sendMessageOp.error.value || refreshPeersOp.error.value),
// Computed
peers,