From 79644864e8d92e0e9f47f81543db49dc632da8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phil=20H=C3=B6fer?= <phil.hoefer@suma-ev.de> Date: Tue, 25 Jul 2023 13:33:47 +0200 Subject: [PATCH] update refillTokenStash function --- background.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/background.js b/background.js index 1a32fcc..01e53fe 100644 --- a/background.js +++ b/background.js @@ -45,8 +45,13 @@ function updateStorageFromCookies() {sites.forEach((site) => { // TODO: Collect token if unused -function refillTokenStash(count) { // get count (max 10) tokens and store them in the stash - //console.log("refilling tokens"); +/** + * Get count (max 10) tokens and store them in the stash + * @param {number} count + * @returns {boolean} True if successfull, false otherwise. + */ +function refillTokenStash(count) { // TODO: handle count >10 + console.log("refilling tokens"); let tokensToBlind = []; for (let i = 0; i <= Math.min(count,9); i++) { tokensToBlind.push(uuidv4()); @@ -131,7 +136,7 @@ async function getTokensPerSearch() { * Returns the refill threshold size based on the currently saved tokensPerSearch value * @returns {number} */ -async function getRefillThresholdSize() { +function getRefillThresholdSize() { return getTokensPerSearch().then((x) => x*3); } @@ -139,7 +144,7 @@ async function getRefillThresholdSize() { * Returns the target stash size based on the currently saved tokensPerSearch value * @returns {number} */ -async function getTargetStashSize() { +function getTargetStashSize() { return getTokensPerSearch().then((x) => x*7); } @@ -162,11 +167,12 @@ async function checkTokenStash() { console.log(activeKey); let refillThresholdSize = getRefillThresholdSize(); let targetStashSize = getTargetStashSize(); - console.log(refillThresholdSize); if (currentStash > refillThresholdSize) { return true; + } else if (activeKey) { + return await refillTokenStash(targetStashSize - currentStash.length); } else { - return refillTokenStash(targetStashSize - currentStash.length); + return false; } } setTimeout(checkTokenStash,10000); -- GitLab