Skip to content

Commit

Permalink
refactor: deleting expired records
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Dec 11, 2024
1 parent 52a4fe4 commit 2ab6d50
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/uploadx/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ export class Store<T = string> {
get(key: string): T | null {
const item = localStorage.getItem(this.prefix + key);
if (item) {
const [value, expires] = JSON.parse(item);
return value && expires ? value : null;
try {
const [value, expires] = JSON.parse(item) as [T, number];
if (Date.now() < expires) {
return value;
}
} catch {}
this.delete(key);
}
return null;
}
Expand All @@ -28,11 +33,13 @@ export class Store<T = string> {
this.keys().forEach(key => {
const item = localStorage.getItem(key);
if (item && maxAgeHours) {
const [, expires] = JSON.parse(item);
now > Number(expires) && localStorage.removeItem(key);
} else {
localStorage.removeItem(key);
try {
const [, expires] = JSON.parse(item) as [T, number];
now > expires && localStorage.removeItem(key);
return;
} catch {}
}
localStorage.removeItem(key);
});
}

Expand Down

0 comments on commit 2ab6d50

Please sign in to comment.