Skip to content

Commit

Permalink
Add permission to fix rating store error (#32)
Browse files Browse the repository at this point in the history
- Add unlimitedStorage permission now that the rating map goes over
  Chrome's 5MB storage limit.
- Clear the local storage cache when users update from v0.6.2.
  • Loading branch information
meooow25 authored Dec 30, 2020
1 parent 57835dd commit a6c28a4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion carrot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"128": "icons/icon.svg"
},
"permissions": [
"storage"
"storage",
"unlimitedStorage"
],
"background": {
"page": "src/background/background.html",
Expand Down
8 changes: 8 additions & 0 deletions carrot/src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,11 @@ async function maybeUpdateRatings() {
}

// Cache related code ends.

browser.runtime.onInstalled.addListener((details) => {
if (details.previousVersion == '0.6.2') {
// Clear cache to remove stale timestamp
// https://github.com/meooow25/carrot/issues/31
browser.storage.local.clear();
}
});
7 changes: 3 additions & 4 deletions carrot/src/background/cache/ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const RATINGS = 'cache.ratings';
* Browser storage cache of user ratings. Ratings of all users are cached because user.info API
* endpoint does not work with a large number of handles which is required for prediction. Browser
* storage is used so that the data is retained if the user restarts the browser. The amount of
* data fetched is around 14MB so we would rather not refetch it. We keep only handles and ratings
* in storage, which uses around 4MB.
* data fetched is around 18MB so we would rather not refetch it. We keep only handles and ratings
* in storage, which uses around 5MB.
*/
export default class Ratings {
constructor(api, storage) {
Expand Down Expand Up @@ -50,9 +50,8 @@ export default class Ratings {

async cacheRatings() {
const users = await this.api.user.ratedList(false);
// Save as object, saving as list of pairs goes over Chrome's 5MB local limit.
const ratings = Object.fromEntries(users.map((u) => [u.handle, u.rating]));
await this.storage.set(RATINGS_TIMESTAMP, Date.now());
await this.storage.set(RATINGS, ratings);
await this.storage.set(RATINGS_TIMESTAMP, Date.now());
}
}
2 changes: 1 addition & 1 deletion carrot/src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
TL;DR: Disable this if you are on a data capped network
</summary>
If you are on Codeforces and a contest starts in less than an hour, having this
option enabled will prefetch user ratings (around 14MB of data) which is required for
option enabled will prefetch user ratings (around 18MB of data) which is required for
delta prediction. This is a one-time fetch for the contest. Disabling this will fetch
the ratings when you open the ranklist for the first time.
</details>
Expand Down

0 comments on commit a6c28a4

Please sign in to comment.