1.3.4 User-Scoped Storage Pattern: Add StorageService integration across modules for improved data management

- Introduced STORAGE_SERVICE token in the DI container for consistent service registration.
- Updated BaseService to include storageService as a dependency, ensuring proper initialization and error handling.
- Refactored ChatService to utilize storageService for managing unread messages and peers, replacing localStorage usage.
- Enhanced MarketStore to save and load orders using storageService, improving data persistence and user experience.
- Registered storageService in the base module, ensuring it is initialized and disposed of correctly.

This integration streamlines data handling across the application, promoting better maintainability and consistency.
This commit is contained in:
padreug 2025-09-06 12:08:39 +02:00
parent 3abdd2d7d9
commit 3cf10b1db4
6 changed files with 285 additions and 103 deletions

View file

@ -6,15 +6,13 @@ import type { ChatMessage, ChatPeer, UnreadMessageData, ChatConfig } from '../ty
import { getAuthToken } from '@/lib/config/lnbits'
import { config } from '@/lib/config'
const UNREAD_MESSAGES_KEY = 'nostr-chat-unread-messages'
const PEERS_KEY = 'nostr-chat-peers'
export class ChatService extends BaseService {
// Service metadata
protected readonly metadata = {
name: 'ChatService',
version: '1.0.0',
dependencies: ['RelayHub', 'AuthService', 'VisibilityService']
dependencies: ['RelayHub', 'AuthService', 'VisibilityService', 'StorageService']
}
// Service-specific state
@ -64,6 +62,9 @@ export class ChatService extends BaseService {
* Complete the initialization once all dependencies are available
*/
private async completeInitialization(): Promise<void> {
// Load peers from storage first
this.loadPeersFromStorage()
// Load peers from API
await this.loadPeersFromAPI().catch(error => {
console.warn('Failed to load peers from API:', error)
@ -290,36 +291,24 @@ export class ChatService extends BaseService {
}
private getUnreadData(peerPubkey: string): UnreadMessageData {
try {
const stored = localStorage.getItem(`${UNREAD_MESSAGES_KEY}-${peerPubkey}`)
if (stored) {
const data = JSON.parse(stored)
return {
...data,
processedMessageIds: new Set(data.processedMessageIds || [])
}
}
} catch (error) {
console.warn('Failed to load unread data for peer:', peerPubkey, error)
}
const data = this.storageService.getUserData(`chat-unread-messages-${peerPubkey}`, {
lastReadTimestamp: 0,
unreadCount: 0,
processedMessageIds: []
})
return {
lastReadTimestamp: 0,
unreadCount: 0,
processedMessageIds: new Set()
return {
...data,
processedMessageIds: new Set(data.processedMessageIds || [])
}
}
private saveUnreadData(peerPubkey: string, data: UnreadMessageData): void {
try {
const serializable = {
...data,
processedMessageIds: Array.from(data.processedMessageIds)
}
localStorage.setItem(`${UNREAD_MESSAGES_KEY}-${peerPubkey}`, JSON.stringify(serializable))
} catch (error) {
console.warn('Failed to save unread data for peer:', peerPubkey, error)
const serializable = {
...data,
processedMessageIds: Array.from(data.processedMessageIds)
}
this.storageService.setUserData(`chat-unread-messages-${peerPubkey}`, serializable)
}
// Load peers from API
@ -369,26 +358,21 @@ export class ChatService extends BaseService {
}
private loadPeersFromStorage(): void {
try {
const stored = localStorage.getItem(PEERS_KEY)
if (stored) {
const peersArray = JSON.parse(stored) as ChatPeer[]
peersArray.forEach(peer => {
this.peers.value.set(peer.pubkey, peer)
})
}
} catch (error) {
console.warn('Failed to load peers from storage:', error)
// Skip loading peers in constructor as StorageService may not be available yet
// This will be called later during initialization when dependencies are ready
if (!this.isInitialized.value) {
return
}
const peersArray = this.storageService.getUserData('chat-peers', []) as ChatPeer[]
peersArray.forEach(peer => {
this.peers.value.set(peer.pubkey, peer)
})
}
private savePeersToStorage(): void {
try {
const peersArray = Array.from(this.peers.value.values())
localStorage.setItem(PEERS_KEY, JSON.stringify(peersArray))
} catch (error) {
console.warn('Failed to save peers to storage:', error)
}
const peersArray = Array.from(this.peers.value.values())
this.storageService.setUserData('chat-peers', peersArray)
}
// Load message history for known peers