From 2b9b93c88f4f66e1514cdbc76eafb731e19adf04 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Fri, 29 Aug 2025 16:03:06 +0530 Subject: [PATCH] chore: remove store --- src/contexts/ForumContext.tsx | 11 ++++-- src/lib/waku/index.ts | 15 -------- src/lib/waku/network.ts | 17 +-------- src/lib/waku/store.ts | 69 ----------------------------------- 4 files changed, 9 insertions(+), 103 deletions(-) delete mode 100644 src/lib/waku/store.ts diff --git a/src/contexts/ForumContext.tsx b/src/contexts/ForumContext.tsx index 305fff9..02d9525 100644 --- a/src/contexts/ForumContext.tsx +++ b/src/contexts/ForumContext.tsx @@ -195,14 +195,17 @@ export function ForumProvider({ children }: { children: React.ReactNode }) { const handleRefreshData = async () => { setIsRefreshing(true); try { - // Manually query the network for updates - await messageManager.queryStore(); + // SDS handles message syncing automatically, just update UI updateStateFromCache(); + toast({ + title: "Data Refreshed", + description: "Your view has been updated.", + }); } catch (error) { console.error("Error refreshing data:", error); toast({ title: "Refresh Failed", - description: "Could not fetch the latest data. Please try again.", + description: "Could not update the view. Please try again.", variant: "destructive", }); } finally { @@ -226,7 +229,7 @@ export function ForumProvider({ children }: { children: React.ReactNode }) { loadData(); // Set up periodic queries - const { cleanup } = setupPeriodicQueries(isNetworkConnected, updateStateFromCache); + const { cleanup } = setupPeriodicQueries(updateStateFromCache); return cleanup; }, [isNetworkConnected, toast, updateStateFromCache]); diff --git a/src/lib/waku/index.ts b/src/lib/waku/index.ts index ec9318d..f75f536 100644 --- a/src/lib/waku/index.ts +++ b/src/lib/waku/index.ts @@ -2,7 +2,6 @@ // with a `isPublished` flag to indicate if the message has been sent to the network import { createLightNode, LightNode, WakuEvent, HealthStatus } from "@waku/sdk"; -import StoreManager from "./store"; import { CommentCache, MessageType, VoteCache, ModerateMessage } from "./types"; import { PostCache } from "./types"; import { CellCache } from "./types"; @@ -15,7 +14,6 @@ export type HealthChangeCallback = (isReady: boolean, health: HealthStatus) => v class MessageManager { private node: LightNode; private reliableMessageManager: ReliableMessageManager | null = null; - private storeManager: StoreManager; private _isReady: boolean = false; private _currentHealth: HealthStatus = HealthStatus.Unhealthy; private healthListeners: Set = new Set(); @@ -58,8 +56,6 @@ class MessageManager { private constructor(node: LightNode) { this.node = node; - this.storeManager = new StoreManager(node); - this.setupHealthMonitoring(); } @@ -185,17 +181,6 @@ class MessageManager { }); } - public async queryStore() { - const messages = await this.storeManager.queryStore(); - - for (const message of messages) { - console.log("message", message); - this.updateCache(message); - } - - return messages; - } - public async sendMessage(message: OpchanMessage) { if (!this.reliableMessageManager) { throw new Error("Reliable message manager not initialized"); diff --git a/src/lib/waku/network.ts b/src/lib/waku/network.ts index a974af8..c223b1b 100644 --- a/src/lib/waku/network.ts +++ b/src/lib/waku/network.ts @@ -14,7 +14,7 @@ export const refreshData = async ( setError: (error: string | null) => void, ): Promise => { try { - toast({ title: 'Refreshing data', description: 'Fetching latest messages from the network...' }); + toast({ title: 'Refreshing data', description: 'SDS handles message syncing automatically...' }); if (!isNetworkConnected) { try { await messageManager.waitForRemotePeer(10000); @@ -22,12 +22,11 @@ export const refreshData = async ( console.warn('Could not connect to peer during refresh:', err); } } - await messageManager.queryStore(); updateStateFromCache(); toast({ title: 'Data refreshed', description: 'Your view has been updated with the latest messages.' }); } catch (err) { console.error('Error refreshing data:', err); - toast({ title: 'Refresh failed', description: 'Could not fetch the latest messages. Please try again.', variant: 'destructive' }); + toast({ title: 'Refresh failed', description: 'Could not sync with network. Please try again.', variant: 'destructive' }); setError('Failed to refresh data. Please try again later.'); } }; @@ -45,7 +44,6 @@ export const initializeNetwork = async ( toast({ title: 'Connection timeout', description: 'Could not connect to any peers. Some features may be unavailable.', variant: 'destructive' }); console.warn('Timeout connecting to peer:', err); } - // await messageManager.queryStore(); updateStateFromCache(); } catch (err) { console.error('Error loading forum data:', err); @@ -55,23 +53,12 @@ export const initializeNetwork = async ( }; export const setupPeriodicQueries = ( - isNetworkConnected: boolean, updateStateFromCache: () => void, ): { cleanup: () => void } => { const uiRefreshInterval = setInterval(updateStateFromCache, 5000); - const networkQueryInterval = setInterval(async () => { - if (isNetworkConnected) { - try { - await messageManager.queryStore(); - } catch (err) { - console.warn('Error during scheduled network query:', err); - } - } - }, 3000); return { cleanup: () => { clearInterval(uiRefreshInterval); - clearInterval(networkQueryInterval); }, }; }; diff --git a/src/lib/waku/store.ts b/src/lib/waku/store.ts deleted file mode 100644 index 1f16961..0000000 --- a/src/lib/waku/store.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { IDecodedMessage, LightNode } from "@waku/sdk"; -import { decodeMessage, decoders} from "./codec"; -import { OpchanMessage } from "@/types/forum"; - -class StoreManager { - private node: LightNode; - - constructor(node: LightNode) { - this.node = node; - } - - public async queryStore() { - const result: OpchanMessage[] = []; - - try { - // Add query options to prevent database overload - const queryOptions = { - paginationLimit: 50, // Correct parameter name for page size - timeStart: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // Last 7 days - timeEnd: new Date(), // Current time - paginationForward: false, // false = newest first - includeData: true // Include full message data - }; - - // Try with query options first, fallback to no options if it fails - try { - await this.node.store.queryWithOrderedCallback( - Object.values(decoders), - (message: IDecodedMessage) => { - const { payload } = message; - const decodedMessage = decodeMessage(payload); - result.push(decodedMessage); - }, - queryOptions - ); - } catch (queryError) { - console.warn("Query with options failed, trying without options:", queryError); - // Fallback: query without options but add manual limit - let messageCount = 0; - const MAX_MESSAGES = 100; - - await this.node.store.queryWithOrderedCallback( - Object.values(decoders), - (message: IDecodedMessage) => { - if (messageCount >= MAX_MESSAGES) { - return; - } - - const { payload } = message; - const decodedMessage = decodeMessage(payload); - result.push(decodedMessage); - messageCount++; - } - ); - } - - if (result.length > 0) { - console.log(`Store query completed. Found ${result.length} messages`); - } - } catch (error) { - console.error("Store query failed:", error); - throw error; - } - - return result; - } -} - -export default StoreManager; \ No newline at end of file