From 5839aae2d5ccfcf7494ee72dd31be899d126ea7b Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 10 Oct 2024 20:50:52 +0200 Subject: [PATCH] add indicator list to watchlist --- src/lib/components/WatchListCard.svelte | 395 +----------------- src/routes/watchlist/stocks/+page.svelte | 313 +++++--------- .../stocks/workers/downloadWorker.ts | 57 +++ 3 files changed, 169 insertions(+), 596 deletions(-) create mode 100644 src/routes/watchlist/stocks/workers/downloadWorker.ts diff --git a/src/lib/components/WatchListCard.svelte b/src/lib/components/WatchListCard.svelte index 5d65e74e..9a32e298 100644 --- a/src/lib/components/WatchListCard.svelte +++ b/src/lib/components/WatchListCard.svelte @@ -1,105 +1,12 @@ @@ -320,10 +277,10 @@ function handleWatchlistModal() { -
+
- + + + + + + {#each (searchQuery?.length !== 0 ? testList : indicatorList) as item} + +
event.preventDefault()}> + +
+
+ {/each} +
+
+
+
@@ -386,6 +381,7 @@ function handleWatchlistModal() { @@ -464,112 +460,9 @@ function handleWatchlistModal() { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/routes/watchlist/stocks/workers/downloadWorker.ts b/src/routes/watchlist/stocks/workers/downloadWorker.ts new file mode 100644 index 00000000..c4885396 --- /dev/null +++ b/src/routes/watchlist/stocks/workers/downloadWorker.ts @@ -0,0 +1,57 @@ +// Cache to store previous requests +let cache = new Map(); + +const getStockScreenerData = async (rules) => { + console.log("Checking cache and fetching new data if needed"); + + // Extract the rule names + let getRuleOfList = rules?.map((rule) => rule.name) || []; + + // Convert the rule set into a string key for the cache + const ruleKey = JSON.stringify(getRuleOfList); + + // Check if data for this rule set is already in the cache + if (cache.has(ruleKey)) { + console.log("Returning cached data"); + return cache.get(ruleKey); + } + + // Fetch new data if it's not in the cache + const postData = { ruleOfList: getRuleOfList }; + const response = await fetch("/api/stock-screener-data", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), + }); + + const output = await response.json(); + + // Store the new data in the cache + cache.set(ruleKey, output); + + return output; +}; + +onmessage = async (event) => { + const { ruleOfList } = event.data || {}; + + const output = await getStockScreenerData(ruleOfList); + + const stockScreenerData = output?.filter((item) => + Object?.values(item)?.every( + (value) => + value !== null && + value !== undefined && + (typeof value !== "object" || + Object.values(value)?.every( + (subValue) => subValue !== null && subValue !== undefined + )) + ) + ); + + postMessage({ message: "success", stockScreenerData }); +}; + +export {};