Skip to content

Commit edd032c

Browse files
committed
feedback
1 parent cdd713a commit edd032c

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/utilities/local-storage.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { log } from "./log";
2+
13
const maxExpiryDays = 365;
24

35
const isPersistingSessionLocalStoreName = "gist.web.isPersistingSession";
@@ -52,25 +54,33 @@ function getStorage() {
5254
function checkKeyForExpiry(key) {
5355
if (!key) return null;
5456

55-
const itemStr = getStorage().getItem(key);
56-
if (!itemStr) return null;
57+
try {
58+
const itemStr = getStorage().getItem(key);
59+
if (!itemStr) return null;
5760

58-
const item = JSON.parse(itemStr);
59-
const now = new Date();
60-
const expiryTime = new Date(item.expiry);
61-
62-
// Retroactive bugfix: remove old cache entries with long expiry times
63-
const isBroadcastOrUserKey = (key.startsWith("gist.web.message.broadcasts") && !key.endsWith("shouldShow") && !key.endsWith("numberOfTimesShown")) || (key.startsWith("gist.web.message.user") && !key.endsWith("seen"));
64-
const sixtyMinutesFromNow = new Date(now.getTime() + 61 * 60 * 1000);
65-
if (isBroadcastOrUserKey && expiryTime.getTime() > sixtyMinutesFromNow.getTime()) {
66-
clearKeyFromLocalStore(key);
67-
return null;
61+
const item = JSON.parse(itemStr);
62+
if (!item.expiry) return item.value;
63+
64+
const now = new Date();
65+
const expiryTime = new Date(item.expiry);
66+
67+
// remove old cache entries with long expiry times
68+
const isBroadcastOrUserKey = (key.startsWith("gist.web.message.broadcasts") && !key.endsWith("shouldShow") && !key.endsWith("numberOfTimesShown")) || (key.startsWith("gist.web.message.user") && !key.endsWith("seen"));
69+
const sixtyMinutesFromNow = new Date(now.getTime() + 61 * 60 * 1000);
70+
if (isBroadcastOrUserKey && expiryTime.getTime() > sixtyMinutesFromNow.getTime()) {
71+
clearKeyFromLocalStore(key);
72+
return null;
73+
}
74+
75+
if (now.getTime() > expiryTime.getTime()) {
76+
clearKeyFromLocalStore(key);
77+
return null;
78+
}
79+
80+
return item.value;
81+
} catch (e) {
82+
log(`Error checking key ${key} for expiry: ${e}`);
6883
}
6984

70-
if (now.getTime() > expiryTime.getTime()) {
71-
clearKeyFromLocalStore(key);
72-
return null;
73-
}
74-
75-
return item.value;
85+
return null;
7686
}

0 commit comments

Comments
 (0)