improve subscribe to messages

This commit is contained in:
padreug 2025-02-12 01:42:04 +01:00
parent 32b0bf7247
commit 8b3f1aa14b
3 changed files with 45 additions and 4 deletions

View file

@ -79,7 +79,7 @@ define(['./workbox-54d0af47'], (function (workbox) { 'use strict';
*/ */
workbox.precacheAndRoute([{ workbox.precacheAndRoute([{
"url": "index.html", "url": "index.html",
"revision": "0.d4m96v8cgs" "revision": "0.i6hkd1cnpb"
}], {}); }], {});
workbox.cleanupOutdatedCaches(); workbox.cleanupOutdatedCaches();
workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), { workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, nextTick, onMounted, watch } from 'vue' import { ref, computed, nextTick, onMounted, watch, onUnmounted } from 'vue'
import { useNostrStore } from '@/stores/nostr' import { useNostrStore } from '@/stores/nostr'
import { npubToHex } from '@/lib/nostr' import { npubToHex } from '@/lib/nostr'
import type { DirectMessage } from '@/types/nostr' import type { DirectMessage } from '@/types/nostr'
@ -79,6 +79,25 @@ onMounted(async () => {
} }
}) })
// Add cleanup on unmount
onUnmounted(() => {
if (nostrStore.activeChat) {
nostrStore.unsubscribeFromMessages()
}
})
// Watch for changes in activeChat
watch(() => nostrStore.activeChat, async (newChat) => {
if (newChat) {
try {
await nostrStore.subscribeToMessages()
} catch (err) {
console.error('Failed to subscribe to messages:', err)
error.value = 'Failed to connect to chat. Please try again later.'
}
}
})
function scrollToBottom() { function scrollToBottom() {
if (messagesEndRef.value) { if (messagesEndRef.value) {
messagesEndRef.value.scrollIntoView({ behavior: 'smooth' }) messagesEndRef.value.scrollIntoView({ behavior: 'smooth' })

View file

@ -89,6 +89,7 @@ export const useNostrStore = defineStore('nostr', () => {
const activeChat = ref<string | null>(null) const activeChat = ref<string | null>(null)
const relayPool = ref<any[]>([]) const relayPool = ref<any[]>([])
const processedMessageIds = ref(new Set<string>()) const processedMessageIds = ref(new Set<string>())
const currentSubscription = ref<any | null>(null)
// Watch account changes and persist to localStorage // Watch account changes and persist to localStorage
watch(account, (newAccount) => { watch(account, (newAccount) => {
@ -245,9 +246,14 @@ export const useNostrStore = defineStore('nostr', () => {
await publishEvent(event, account.value.relays) await publishEvent(event, account.value.relays)
} }
const subscribeToMessages = async () => { async function subscribeToMessages() {
if (!account.value) return if (!account.value) return
// Cleanup existing subscription if any
if (currentSubscription.value) {
unsubscribeFromMessages()
}
// Filter for received messages with history // Filter for received messages with history
const receivedFilter = { const receivedFilter = {
kinds: [4], kinds: [4],
@ -349,6 +355,14 @@ export const useNostrStore = defineStore('nostr', () => {
resolve(true) resolve(true)
} }
}) })
// Store subscriptions for cleanup
currentSubscription.value = {
unsub: () => {
receivedSub.unsub()
sentSub.unsub()
}
}
}) })
} }
@ -356,6 +370,13 @@ export const useNostrStore = defineStore('nostr', () => {
await Promise.all(relayPool.value.map(relay => subscribeToRelay(relay))) await Promise.all(relayPool.value.map(relay => subscribeToRelay(relay)))
} }
function unsubscribeFromMessages() {
if (currentSubscription.value) {
currentSubscription.value.unsub()
currentSubscription.value = null
}
}
return { return {
account, account,
profiles, profiles,
@ -367,6 +388,7 @@ export const useNostrStore = defineStore('nostr', () => {
login, login,
logout, logout,
sendMessage, sendMessage,
subscribeToMessages subscribeToMessages,
unsubscribeFromMessages
} }
}) })