This commit is contained in:
padreug 2025-02-15 02:34:19 +01:00
parent 8df44506c0
commit 2bbb9ae938
10 changed files with 47 additions and 9 deletions

View file

@ -1,3 +1,5 @@
import type { DirectMessage } from '@/types/nostr'
export class MessageManager {
private messages = new Map<string, DirectMessage[]>()
private processedIds = new Set<string>()

View file

@ -1,3 +1,5 @@
import type { DirectMessage } from '@/types/nostr'
export class MessageStorage {
static saveMessages(pubkey: string, messages: DirectMessage[]) {
try {

View file

@ -1,3 +1,5 @@
import type { NostrEvent } from '@/types/nostr'
export class SubscriptionManager {
private currentSubs: any[] = []
private isActive = false

View file

@ -4,3 +4,12 @@ import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export async function withTimeout<T>(promise: Promise<T>, timeoutMs: number = 10000): Promise<T> {
return Promise.race([
promise,
new Promise<T>((_, reject) =>
setTimeout(() => reject(new Error('Operation timed out')), timeoutMs)
)
])
}

View file

@ -1,3 +1,6 @@
import { withTimeout } from '@/lib/utils'
import type { NostrEvent } from '@/types/nostr'
// Create a new WebSocket manager class
export class NostrWebSocketManager {
private relayPool: any[] = []
@ -41,4 +44,19 @@ export class NostrWebSocketManager {
get isConnected() {
return this.relayPool.length > 0
}
private async publishToRelay(event: NostrEvent, url: string) {
const relay = window.NostrTools.relayInit(url)
try {
await relay.connect()
return new Promise((resolve, reject) => {
const pub = relay.publish(event)
pub.on('ok', () => resolve(true))
pub.on('failed', reject)
})
} catch (err) {
console.error(`Failed to publish to ${url}:`, err)
return false
}
}
}