remove websocket to table component

This commit is contained in:
MuslemRahimi 2025-01-07 18:53:58 +01:00
parent 9268cfdcb3
commit c6b93a1d73

View File

@ -16,7 +16,6 @@
export let data;
export let rawData;
export let excludedRules = new Set([
"volume",
"price",
@ -357,16 +356,6 @@
}
}
// Save scroll position before data changes
function saveScrollPosition() {
scrollPosition = window.scrollY;
}
// Restore scroll position after data changes
function restoreScrollPosition() {
window.scrollTo(0, scrollPosition);
}
function sendMessage(message) {
if (socket && socket.readyState === WebSocket.OPEN) {
socket.send(JSON?.stringify(message));
@ -382,9 +371,7 @@
socket.addEventListener("open", () => {
console.log("WebSocket connection opened");
// Send only current watchlist symbols
//Only update 200 tickers because of a bug. The previous price is getting changed for tickers. To-do list.
const tickerList =
rawData?.slice(0, 200)?.map((item) => item?.symbol) || [];
const tickerList = rawData?.map((item) => item?.symbol) || [];
sendMessage(tickerList);
});
@ -393,18 +380,15 @@
try {
const newList = JSON?.parse(data);
if (newList?.length > 0) {
if (originalData.some((item) => "changesPercentage" in item)) {
originalData = calculateChange(originalData, newList);
stockList = updateStockList(stockList, originalData);
setTimeout(() => {
stockList = stockList?.map((item) => ({
...item,
previous: null,
}));
}, 800);
saveScrollPosition();
sortMode = true;
}
stockList = calculateChange(stockList, newList);
//stockList = updateStockList(stockList, originalData);
setTimeout(() => {
stockList = stockList?.map((item) => ({
...item,
previous: null,
}));
}, 800);
sortMode = true;
}
} catch (e) {
console.error("Error parsing WebSocket message:", e);
@ -418,14 +402,47 @@
console.error("WebSocket connection error:", error);
}
}
$: stockList = [...stockList];
/*
$: if ($isOpen) {
websocketRealtimeData();
console.log("WebSocket restarted");
}
afterUpdate(async () => {
// Compare only the symbols to detect changes
const currentSymbols = rawData?.map((item) => item?.symbol).sort();
const previousSymbols = previousList?.map((item) => item?.symbol).sort();
// Check if symbols have changed
if (
JSON.stringify(currentSymbols) !== JSON.stringify(previousSymbols) &&
typeof socket !== "undefined"
) {
// Update previous list
previousList = rawData;
try {
// Close existing socket if open
if (socket && socket.readyState !== WebSocket.CLOSED) {
socket?.close();
}
// Wait for socket to close
await new Promise((resolve) => {
socket?.addEventListener("close", resolve, { once: true });
});
// Reconnect with new symbols
if ($isOpen) {
await websocketRealtimeData();
console.log("WebSocket restarted due to watchlist changes");
}
} catch (error) {
console.error("Error restarting WebSocket:", error);
}
}
});
*/
onMount(async () => {
try {
const savedRules = localStorage?.getItem(pagePathName);
@ -481,41 +498,6 @@
let previousList = [];
let reconnectionTimeout;
afterUpdate(async () => {
// Compare only the symbols to detect changes
const currentSymbols = rawData?.map((item) => item?.symbol).sort();
const previousSymbols = previousList?.map((item) => item?.symbol).sort();
// Check if symbols have changed
if (
JSON.stringify(currentSymbols) !== JSON.stringify(previousSymbols) &&
typeof socket !== "undefined"
) {
// Update previous list
previousList = rawData;
try {
// Close existing socket if open
if (socket && socket.readyState !== WebSocket.CLOSED) {
socket?.close();
}
// Wait for socket to close
await new Promise((resolve) => {
socket?.addEventListener("close", resolve, { once: true });
});
// Reconnect with new symbols
if ($isOpen) {
await websocketRealtimeData();
console.log("WebSocket restarted due to watchlist changes");
}
} catch (error) {
console.error("Error restarting WebSocket:", error);
}
}
});
onDestroy(() => {
try {
// Clear any pending reconnection timeout
@ -613,6 +595,11 @@
}
}
stockList = stockList?.map((item) => ({
...item,
previous: null,
}));
// If input is false, cycle through 'none', 'asc', 'desc' for the clicked key
if (!input) {
const orderCycle = ["none", "asc", "desc"];