Skip to content

Commit

Permalink
Cache appids for 24 hrs if fetch is successful
Browse files Browse the repository at this point in the history
  • Loading branch information
candela97 committed Mar 15, 2024
1 parent ea98313 commit 40b5d32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/js/Content/Features/Store/Wishlist/FWishlistDemoLink.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Localization} from "../../../../modulesCore";
import {Localization, LocalStorage} from "../../../../modulesCore";
import {CallbackFeature, RequestData} from "../../../modulesContent";

export default class FWishlistDemoLink extends CallbackFeature {
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/js/Core/Storage/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down

0 comments on commit 40b5d32

Please sign in to comment.