Skip to content

Commit

Permalink
rework caching with CachedHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Dec 19, 2024
1 parent 6923b43 commit 0d4620b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/lib/alarms/trade_offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 15 additions & 9 deletions src/lib/bridge/handlers/fetch_steam_trades.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -14,14 +16,18 @@ export interface FetchSteamTradesResponse {
steam_id?: string | null;
}

export const FetchSteamTrades = new SimpleHandler<FetchSteamTradesRequest, FetchSteamTradesResponse>(
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<FetchSteamTradesRequest, FetchSteamTradesResponse>(
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
);
43 changes: 16 additions & 27 deletions src/lib/page_scripts/trade_offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

/**
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/userinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ export function getUserSteamID() {
*/
export function convertToSteamID64(steamID32: number) {
return (BigInt('76561197960265728') + BigInt(steamID32)).toString();
}
}

0 comments on commit 0d4620b

Please sign in to comment.