Skip to content

Commit

Permalink
Support clean localStorage when got excess error (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzxyz authored May 28, 2024
1 parent e8cc36a commit 63f207f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/network-support/src/utils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,23 @@ export class LocalStorageCache implements IStore {
}

async set<T>(key: string, value: T): Promise<void> {
const data = {
value,
expiry: this.ttl ? Date.now() + this.ttl : undefined,
};
localStorage.setItem(key, JSON.stringify(data));
const expiry = this.ttl ? Date.now() + this.ttl : undefined;
const data = { value, expiry };

try {
localStorage.setItem(key, JSON.stringify(data));
} catch {
await this.cleanExpiredLocalStorage();
}
}

async remove(key: string): Promise<void> {
localStorage.removeItem(key);
}

async cleanExpiredLocalStorage(): Promise<void> {
await Promise.all(Object.entries(localStorage).map(async ([key]) => this.get(key)));
}
}

export class LRUCache implements IStore {
Expand Down

0 comments on commit 63f207f

Please sign in to comment.