diff --git a/src/client/scripts/esm/game/main.js b/src/client/scripts/esm/game/main.js index 2ba310ff..349f87c9 100644 --- a/src/client/scripts/esm/game/main.js +++ b/src/client/scripts/esm/game/main.js @@ -41,8 +41,6 @@ function start() { onlinegame.askServerIfWeAreInGame(); - localstorage.eraseExpiredItems(); - gameLoop(); // Update & draw the scene repeatedly } diff --git a/src/client/scripts/esm/util/localstorage.js b/src/client/scripts/esm/util/localstorage.ts similarity index 60% rename from src/client/scripts/esm/util/localstorage.js rename to src/client/scripts/esm/util/localstorage.ts index 1e90c71e..9c870fcb 100644 --- a/src/client/scripts/esm/util/localstorage.js +++ b/src/client/scripts/esm/util/localstorage.ts @@ -1,6 +1,4 @@ -'use strict'; - /** * This script handles reading, saving, and deleting expired * browser local storage data for us! @@ -8,23 +6,47 @@ * (unless the user clears their browser cache) */ + +/** An entry in local storage */ +interface Entry { + /** The actual value of the entry */ + value: any, + /** The timestamp the entry will become stale, at which point it should be deleted. */ + expires: number +} + +/** For debugging. This prints to the console all save and delete operations. */ const printSavesAndDeletes = false; const defaultExpiryTimeMillis = 1000 * 60 * 60 * 24; // 24 hours // const defaultExpiryTimeMillis = 1000 * 20; // 20 seconds -function saveItem(key, value, expiryMillis = defaultExpiryTimeMillis) { +// Do this on load every time +eraseExpiredItems(); + +/** + * Saves an item in browser local storage + * @param key - The key-name to give this entry. + * @param value - What to save + * @param [expiryMillis] How long until this entry should be auto-deleted for being stale + */ +function saveItem(key: string, value: any, expiryMillis: number = defaultExpiryTimeMillis) { if (printSavesAndDeletes) console.log(`Saving key to local storage: ${key}`); const timeExpires = Date.now() + expiryMillis; - const save = { value, expires: timeExpires }; + const save: Entry = { value, expires: timeExpires }; const stringifiedSave = JSON.stringify(save); localStorage.setItem(key, stringifiedSave); } -function loadItem(key) { - const stringifiedSave = localStorage.getItem(key); // "{ value, expiry }" +/** + * Loads an item from browser local storage + * @param key - The name/key of the item in storage + * @returns The entry + */ +function loadItem(key: string): any { + const stringifiedSave: string | null = localStorage.getItem(key); // "{ value, expiry }" if (stringifiedSave === null) return; - let save; + let save: Entry | any; try { save = JSON.parse(stringifiedSave); // { value, expires } } catch (e) { // Value wasn't in json format, just delete it. They have to be in json because we always store the 'expiry' property. @@ -39,12 +61,16 @@ function loadItem(key) { return save.value; } -function deleteItem(key) { +/** + * Deletes an item from browser local storage + * @param key The name/key of the item in storage + */ +function deleteItem(key: string) { if (printSavesAndDeletes) console.log(`Deleting local storage item with key '${key}!'`); localStorage.removeItem(key); } -function hasItemExpired(save) { +function hasItemExpired(save: Entry | any) { if (save.expires === undefined) { console.log(`Local storage item was in an old format. Deleting it! Value: ${JSON.stringify(save)}}`); return true;