mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 07:36:01 +00:00
local storage garbage collector
This commit is contained in:
@@ -2,6 +2,10 @@ import App from './App.svelte';
|
||||
import './utility/connectionsPinger';
|
||||
import './utility/changeCurrentDbByTab';
|
||||
import './commands/stdCommands';
|
||||
import localStorageGarbageCollector from './utility/localStorageGarbageCollector';
|
||||
|
||||
localStorageGarbageCollector();
|
||||
|
||||
|
||||
const app = new App({
|
||||
target: document.body,
|
||||
|
||||
35
packages/web/src/utility/localStorageGarbageCollector.js
Normal file
35
packages/web/src/utility/localStorageGarbageCollector.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import moment from 'moment';
|
||||
import localforage from 'localforage';
|
||||
|
||||
export default async function localStorageGarbageCollector() {
|
||||
const openedTabsJson = localStorage.getItem('openedTabs');
|
||||
let openedTabs = openedTabsJson ? JSON.parse(openedTabsJson) : [];
|
||||
|
||||
const closeLimit = moment().add(-7, 'day').valueOf();
|
||||
|
||||
openedTabs = openedTabs.filter(x => !x.closedTime || x.closedTime > closeLimit);
|
||||
localStorage.setItem('openedTabs', JSON.stringify(openedTabs));
|
||||
|
||||
const toRemove = [];
|
||||
for (const key in localStorage) {
|
||||
if (!key.startsWith('tabdata_')) continue;
|
||||
if (openedTabs.find(x => key.endsWith('_' + x.tabid))) continue;
|
||||
toRemove.push(key);
|
||||
}
|
||||
|
||||
for (const key of toRemove) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
const keysForage = await localforage.keys();
|
||||
const toRemoveForage = [];
|
||||
for (const key in keysForage) {
|
||||
if (!key.startsWith('tabdata_')) continue;
|
||||
if (openedTabs.find(x => key.endsWith('_' + x.tabid))) continue;
|
||||
toRemoveForage.push(key);
|
||||
}
|
||||
|
||||
for (const key of toRemoveForage) {
|
||||
await localforage.removeItem(key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user