mirror of
https://github.com/logos-messaging/OpChan.git
synced 2026-01-02 21:03:09 +00:00
chore: remove store
This commit is contained in:
parent
b4d29c21d8
commit
2b9b93c88f
@ -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]);
|
||||
|
||||
@ -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<HealthChangeCallback> = 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");
|
||||
|
||||
@ -14,7 +14,7 @@ export const refreshData = async (
|
||||
setError: (error: string | null) => void,
|
||||
): Promise<void> => {
|
||||
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);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@ -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;
|
||||
Loading…
x
Reference in New Issue
Block a user