From 0d4620b56d0266ce2a4eae250d2c71749e0c0e94 Mon Sep 17 00:00:00 2001 From: GODrums Date: Thu, 19 Dec 2024 04:22:01 +0100 Subject: [PATCH] rework caching with CachedHandler --- src/lib/alarms/trade_offer.ts | 2 +- src/lib/bridge/handlers/fetch_steam_trades.ts | 24 +++++++---- src/lib/page_scripts/trade_offers.ts | 43 +++++++------------ src/lib/utils/userinfo.ts | 2 +- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/lib/alarms/trade_offer.ts b/src/lib/alarms/trade_offer.ts index 023cc6c..dcb72f6 100644 --- a/src/lib/alarms/trade_offer.ts +++ b/src/lib/alarms/trade_offer.ts @@ -8,7 +8,7 @@ import {CancelTradeOffer} from '../bridge/handlers/cancel_trade_offer'; import {FetchSteamUser} from '../bridge/handlers/fetch_steam_user'; import {rgDescription} from '../types/steam'; import {HasPermissions} from '../bridge/handlers/has_permissions'; -import { convertToSteamID64 } from '../utils/userinfo'; +import {convertToSteamID64} from '../utils/userinfo'; export async function pingSentTradeOffers(pendingTrades: Trade[]) { const {offers, type} = await getSentTradeOffers(); diff --git a/src/lib/bridge/handlers/fetch_steam_trades.ts b/src/lib/bridge/handlers/fetch_steam_trades.ts index 13694ab..7af84ab 100644 --- a/src/lib/bridge/handlers/fetch_steam_trades.ts +++ b/src/lib/bridge/handlers/fetch_steam_trades.ts @@ -1,10 +1,12 @@ import {getTradeOffersWithDescriptionFromAPI, TradeOffersAPIOffer} from '../../alarms/trade_offer'; import {rgDescription} from '../../types/steam'; +import {CachedHandler} from '../wrappers/cached'; import {SimpleHandler} from './main'; import {RequestType} from './types'; interface FetchSteamTradesRequest { steam_id?: string; + trade_offer_id?: number; } export interface FetchSteamTradesResponse { @@ -14,14 +16,18 @@ export interface FetchSteamTradesResponse { steam_id?: string | null; } -export const FetchSteamTrades = new SimpleHandler( - RequestType.FETCH_STEAM_TRADES, - async (req) => { - const resp = await getTradeOffersWithDescriptionFromAPI(req.steam_id); - if (!resp) { - throw new Error('Error fetching Steam trade offers from API'); - } +export const FetchSteamTrades = new CachedHandler( + new SimpleHandler( + RequestType.FETCH_STEAM_TRADES, + async (req) => { + const resp = await getTradeOffersWithDescriptionFromAPI(req.steam_id); + if (!resp) { + throw new Error('Error fetching Steam trade offers from API'); + } - return resp; - } + return resp; + } + ), + 1, + 10 * 60 * 1000 ); diff --git a/src/lib/page_scripts/trade_offers.ts b/src/lib/page_scripts/trade_offers.ts index 93c4d2b..cde71fc 100644 --- a/src/lib/page_scripts/trade_offers.ts +++ b/src/lib/page_scripts/trade_offers.ts @@ -15,27 +15,15 @@ function main() {} /** * Gets the trade offers from the local storage or fetches them from the API. * Local storage serves as a cache here. - * @param isSentPage if the current page is the sent trade offers page + * @param steam_id the steam id of logged in user * @returns the trade offers */ -async function fetchTradeOffers(steam_id: string, isSentPage: boolean) { - const g_steamTrades = JSON.parse(localStorage.getItem('g_steamTrades') || '{}') as FetchSteamTradesResponse; - let refetchRequired = true; - if (g_steamTrades.sent || g_steamTrades.received) { - const latestTradeOfferID = Number.parseInt(g_steamTrades[isSentPage ? 'sent' : 'received']?.[0].tradeofferid); - const latestTradeOfferIDFromPage = Number.parseInt(document.querySelector('.tradeoffer')?.id.split('_')[1] ?? '0'); - - refetchRequired = Number.isNaN(latestTradeOfferID) || latestTradeOfferID !== latestTradeOfferIDFromPage; - } - - if (!refetchRequired) { - return g_steamTrades; - } - - const steamTrades = await ClientSend(FetchSteamTrades, {steam_id}); +async function fetchTradeOffers(steam_id: string) { + const latestTradeIDFromPage = document.querySelector('.tradeoffer')?.id.split('_')[1]; + const trade_offer_id = latestTradeIDFromPage ? Number.parseInt(latestTradeIDFromPage) : undefined; - localStorage.setItem('g_steamTrades', JSON.stringify(steamTrades)); - return steamTrades; + console.log('Fetching trade offers', steam_id, trade_offer_id); + return await ClientSend(FetchSteamTrades, {steam_id, trade_offer_id}); } /** @@ -49,19 +37,16 @@ async function annotateTradeOfferItemElements() { return; } - const isSentPage = location.pathname.includes('sent'); - - const steamTrades = await fetchTradeOffers(steam_id, isSentPage); + const steamTrades = await fetchTradeOffers(steam_id); const tradeOffers = document.querySelectorAll('.tradeoffer'); for (const tradeOffer of tradeOffers) { const tradeOfferID = tradeOffer.id.split('_')[1]; const tradeItemElements = tradeOffer.querySelectorAll('.trade_item'); - - const tradeOfferAPI = isSentPage - ? steamTrades.sent.find((t) => t.tradeofferid === tradeOfferID) - : steamTrades.received.find((t) => t.tradeofferid === tradeOfferID); + const tradeOfferAPI = + steamTrades.sent.find((t) => t.tradeofferid === tradeOfferID) ?? + steamTrades.received.find((t) => t.tradeofferid === tradeOfferID); if (!tradeOfferAPI) { continue; } @@ -85,10 +70,14 @@ async function annotateTradeOfferItemElements() { } let isOwnItem = true; - let apiItem = tradeOfferAPI?.items_to_give?.find((a) => a.classid === classId && a.instanceid === instanceId); + let apiItem = tradeOfferAPI?.items_to_give?.find( + (a) => a.classid === classId && a.instanceid === instanceId + ); if (!apiItem) { isOwnItem = false; - apiItem = tradeOfferAPI?.items_to_receive?.find((a) => a.classid === classId && a.instanceid === instanceId); + apiItem = tradeOfferAPI?.items_to_receive?.find( + (a) => a.classid === classId && a.instanceid === instanceId + ); } const ownerId = isOwnItem ? steam_id : convertToSteamID64(tradeOfferAPI.accountid_other); diff --git a/src/lib/utils/userinfo.ts b/src/lib/utils/userinfo.ts index addb6aa..6b0085f 100644 --- a/src/lib/utils/userinfo.ts +++ b/src/lib/utils/userinfo.ts @@ -32,4 +32,4 @@ export function getUserSteamID() { */ export function convertToSteamID64(steamID32: number) { return (BigInt('76561197960265728') + BigInt(steamID32)).toString(); -} \ No newline at end of file +}