-
-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage clean-up interface, prune preferences (#5070)
* Run clean-up less frequently * Common interface for purge old entries * Clean-up preferences regularly The settings console does not remove options that are the default value (or no longer exists), making the option object heavier than necessary. It is important to have it as lean as possible, as any load delays slow down the init process.
- Loading branch information
1 parent
2720754
commit ccf7437
Showing
7 changed files
with
76 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* @flow */ | ||
|
||
import { Storage } from '../environment'; | ||
import { DAY, WEEK } from './time'; | ||
|
||
export async function maybePruneOldEntries(id: string, entryStorage: *, keepTrackDays: number = 30) { | ||
if (!await shouldPrune(id)) return; | ||
|
||
const keepTrackPeriod = DAY * keepTrackDays; | ||
const now = Date.now(); | ||
for (const [id, { updateTime } = {}] of Object.entries(await entryStorage.getAll())) { | ||
if (!updateTime || (now - updateTime) > keepTrackPeriod) { | ||
entryStorage.delete(id); | ||
} | ||
} | ||
} | ||
|
||
export async function shouldPrune(id: string, interval: number = WEEK) { | ||
await new Promise(res => requestIdleCallback(res)); | ||
const lastStorage = Storage.wrap(`last_prune.${id}`, 0); | ||
const now = Date.now(); | ||
if ((now - await lastStorage.get()) < interval) return false; | ||
lastStorage.set(now); | ||
return true; | ||
} |