init to refresh messages

This commit is contained in:
padreug 2025-02-11 14:59:27 +01:00
parent 231658b980
commit c7aa88bec6

View file

@ -99,6 +99,12 @@ export const useNostrStore = defineStore('nostr', () => {
}
}, { deep: true })
// Initialize store if account exists in localStorage
if (account.value) {
console.log('Found existing account, initializing connection...')
init()
}
// Computed
const isLoggedIn = computed(() => !!account.value)
const currentMessages = computed(() =>
@ -252,7 +258,12 @@ export const useNostrStore = defineStore('nostr', () => {
since: 0 // Get all historical messages
}
relayPool.value.forEach(relay => {
const subscribeToRelay = (relay: any) => {
return new Promise((resolve) => {
let receivedCount = 0
let sentCount = 0
let eoseCount = 0
// Subscribe to received messages
const receivedSub = relay.sub([receivedFilter])
@ -263,6 +274,7 @@ export const useNostrStore = defineStore('nostr', () => {
return
}
receivedCount++
const decrypted = await window.NostrTools.nip04.decrypt(
account.value!.privkey,
event.pubkey,
@ -298,6 +310,7 @@ export const useNostrStore = defineStore('nostr', () => {
return
}
sentCount++
const decrypted = await window.NostrTools.nip04.decrypt(
account.value!.privkey,
SUPPORT_NPUB,
@ -317,7 +330,26 @@ export const useNostrStore = defineStore('nostr', () => {
console.error('Failed to decrypt sent message:', err)
}
})
// Listen for end of stored events
receivedSub.on('eose', () => {
eoseCount++
if (eoseCount >= 2) { // Both subscriptions have finished
resolve(true)
}
})
sentSub.on('eose', () => {
eoseCount++
if (eoseCount >= 2) { // Both subscriptions have finished
resolve(true)
}
})
})
}
// Wait for all relays to load their historical messages
await Promise.all(relayPool.value.map(relay => subscribeToRelay(relay)))
}
return {