From 40b5d32ad6f57439db94f0e58ec8c5e270f574b0 Mon Sep 17 00:00:00 2001 From: candela97 <54083835+candela97@users.noreply.github.com> Date: Sat, 16 Mar 2024 07:14:51 +0800 Subject: [PATCH] Cache appids for 24 hrs if fetch is successful --- .../Store/Wishlist/FWishlistDemoLink.js | 50 ++++++++++++++++--- src/js/Core/Storage/LocalStorage.ts | 1 + 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/js/Content/Features/Store/Wishlist/FWishlistDemoLink.js b/src/js/Content/Features/Store/Wishlist/FWishlistDemoLink.js index 0e279d952..b3fd2f37a 100644 --- a/src/js/Content/Features/Store/Wishlist/FWishlistDemoLink.js +++ b/src/js/Content/Features/Store/Wishlist/FWishlistDemoLink.js @@ -1,4 +1,4 @@ -import {Localization} from "../../../../modulesCore"; +import {Localization, LocalStorage} from "../../../../modulesCore"; import {CallbackFeature, RequestData} from "../../../modulesContent"; export default class FWishlistDemoLink extends CallbackFeature { @@ -7,20 +7,56 @@ export default class FWishlistDemoLink extends CallbackFeature { this._info = []; - const appids = this.context.wishlistData.map(({appid}) => `appids[]=${appid}`); + const now = Date.now(); + let appids = this.context.wishlistData.map(({appid}) => Number(appid)); - // https://stackoverflow.com/questions/8495687/split-array-into-chunks - const chunkSize = 400; // Split params into chunks as Steam may throw `HTTP 414 Request-URI Too Large` + let cache = LocalStorage.get("wl_demo_appids"); + if (cache.length > 0) { + appids = new Set(appids); + cache = cache.filter(({appid, cached}) => { + const _appid = Number(appid); + if (!appids.has(_appid) || (cached.timestamp < now)) { + return false; + } + if (cached.info?.demo_appid) { + this._info.push(cached.info); + } + appids.delete(_appid); + return true; + }); + appids = Array.from(appids); + } + + // Split params into chunks as Steam may throw `HTTP 414 Request-URI Too Large` + const chunkSize = 400; for (let i = 0; i < appids.length; i += chunkSize) { - const params = appids.slice(i, i + chunkSize).join("&"); + const chunk = appids.slice(i, i + chunkSize); + const params = chunk.map(appid => `appids[]=${appid}`).join("&"); const data = await RequestData.getJson(`https://store.steampowered.com/saleaction/ajaxgetdemoevents?${params}`).catch(err => console.error(err)); - if (!data || !data.success || !data.info) { continue; } + if (!data || !data.success) { continue; } + + // Cache appids for 24 hrs if fetch is successful + chunk.forEach(appid => { + + // `data.info` will be undefined if there're no demos for all given appids + const info = data.info?.find(val => Number(val.appid) === appid); + cache.push({ + appid, + cached: { + timestamp: now + 24 * 60 * 60 * 1000, + info, + } + }); - this._info = this._info.concat(data.info.filter(val => Boolean(val.demo_appid))); + if (info?.demo_appid) { + this._info.push(info); + } + }); } + LocalStorage.set("wl_demo_appids", cache); return this._info.length > 0; } diff --git a/src/js/Core/Storage/LocalStorage.ts b/src/js/Core/Storage/LocalStorage.ts index 1f120b1fe..74fe1f025 100644 --- a/src/js/Core/Storage/LocalStorage.ts +++ b/src/js/Core/Storage/LocalStorage.ts @@ -78,6 +78,7 @@ const DEFAULTS = { "hide_login_warn_store": false, "hide_login_warn_community": false, "review_filters": {}, + "wl_demo_appids": [], "local_storage_migration": {"store": false, "community": false, "extension": false}, };