chore: remove store

This commit is contained in:
Danish Arora 2025-08-29 16:03:06 +05:30
parent b4d29c21d8
commit 2b9b93c88f
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
4 changed files with 9 additions and 103 deletions

View File

@ -195,14 +195,17 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
const handleRefreshData = async () => { const handleRefreshData = async () => {
setIsRefreshing(true); setIsRefreshing(true);
try { try {
// Manually query the network for updates // SDS handles message syncing automatically, just update UI
await messageManager.queryStore();
updateStateFromCache(); updateStateFromCache();
toast({
title: "Data Refreshed",
description: "Your view has been updated.",
});
} catch (error) { } catch (error) {
console.error("Error refreshing data:", error); console.error("Error refreshing data:", error);
toast({ toast({
title: "Refresh Failed", title: "Refresh Failed",
description: "Could not fetch the latest data. Please try again.", description: "Could not update the view. Please try again.",
variant: "destructive", variant: "destructive",
}); });
} finally { } finally {
@ -226,7 +229,7 @@ export function ForumProvider({ children }: { children: React.ReactNode }) {
loadData(); loadData();
// Set up periodic queries // Set up periodic queries
const { cleanup } = setupPeriodicQueries(isNetworkConnected, updateStateFromCache); const { cleanup } = setupPeriodicQueries(updateStateFromCache);
return cleanup; return cleanup;
}, [isNetworkConnected, toast, updateStateFromCache]); }, [isNetworkConnected, toast, updateStateFromCache]);

View File

@ -2,7 +2,6 @@
// with a `isPublished` flag to indicate if the message has been sent to the network // with a `isPublished` flag to indicate if the message has been sent to the network
import { createLightNode, LightNode, WakuEvent, HealthStatus } from "@waku/sdk"; import { createLightNode, LightNode, WakuEvent, HealthStatus } from "@waku/sdk";
import StoreManager from "./store";
import { CommentCache, MessageType, VoteCache, ModerateMessage } from "./types"; import { CommentCache, MessageType, VoteCache, ModerateMessage } from "./types";
import { PostCache } from "./types"; import { PostCache } from "./types";
import { CellCache } from "./types"; import { CellCache } from "./types";
@ -15,7 +14,6 @@ export type HealthChangeCallback = (isReady: boolean, health: HealthStatus) => v
class MessageManager { class MessageManager {
private node: LightNode; private node: LightNode;
private reliableMessageManager: ReliableMessageManager | null = null; private reliableMessageManager: ReliableMessageManager | null = null;
private storeManager: StoreManager;
private _isReady: boolean = false; private _isReady: boolean = false;
private _currentHealth: HealthStatus = HealthStatus.Unhealthy; private _currentHealth: HealthStatus = HealthStatus.Unhealthy;
private healthListeners: Set<HealthChangeCallback> = new Set(); private healthListeners: Set<HealthChangeCallback> = new Set();
@ -58,8 +56,6 @@ class MessageManager {
private constructor(node: LightNode) { private constructor(node: LightNode) {
this.node = node; this.node = node;
this.storeManager = new StoreManager(node);
this.setupHealthMonitoring(); 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) { public async sendMessage(message: OpchanMessage) {
if (!this.reliableMessageManager) { if (!this.reliableMessageManager) {
throw new Error("Reliable message manager not initialized"); throw new Error("Reliable message manager not initialized");

View File

@ -14,7 +14,7 @@ export const refreshData = async (
setError: (error: string | null) => void, setError: (error: string | null) => void,
): Promise<void> => { ): Promise<void> => {
try { try {
toast({ title: 'Refreshing data', description: 'Fetching latest messages from the network...' }); toast({ title: 'Refreshing data', description: 'SDS handles message syncing automatically...' });
if (!isNetworkConnected) { if (!isNetworkConnected) {
try { try {
await messageManager.waitForRemotePeer(10000); await messageManager.waitForRemotePeer(10000);
@ -22,12 +22,11 @@ export const refreshData = async (
console.warn('Could not connect to peer during refresh:', err); console.warn('Could not connect to peer during refresh:', err);
} }
} }
await messageManager.queryStore();
updateStateFromCache(); updateStateFromCache();
toast({ title: 'Data refreshed', description: 'Your view has been updated with the latest messages.' }); toast({ title: 'Data refreshed', description: 'Your view has been updated with the latest messages.' });
} catch (err) { } catch (err) {
console.error('Error refreshing data:', 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.'); 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' }); 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); console.warn('Timeout connecting to peer:', err);
} }
// await messageManager.queryStore();
updateStateFromCache(); updateStateFromCache();
} catch (err) { } catch (err) {
console.error('Error loading forum data:', err); console.error('Error loading forum data:', err);
@ -55,23 +53,12 @@ export const initializeNetwork = async (
}; };
export const setupPeriodicQueries = ( export const setupPeriodicQueries = (
isNetworkConnected: boolean,
updateStateFromCache: () => void, updateStateFromCache: () => void,
): { cleanup: () => void } => { ): { cleanup: () => void } => {
const uiRefreshInterval = setInterval(updateStateFromCache, 5000); 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 { return {
cleanup: () => { cleanup: () => {
clearInterval(uiRefreshInterval); clearInterval(uiRefreshInterval);
clearInterval(networkQueryInterval);
}, },
}; };
}; };

View File

@ -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;