mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 11:16:01 +00:00
tabs saves to forage instead of storage
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { writable, derived, readable } from 'svelte/store';
|
||||
import localforage from 'localforage';
|
||||
import type { ExtensionsDirectory } from 'dbgate-types';
|
||||
import invalidateCommands from './commands/invalidateCommands';
|
||||
import getElectron from './utility/getElectron';
|
||||
@@ -29,6 +30,27 @@ export function writableWithStorage<T>(defaultValue: T, storageName) {
|
||||
return res;
|
||||
}
|
||||
|
||||
export function writableWithForage<T>(defaultValue: T, storageName) {
|
||||
const res = writable<T>(defaultValue);
|
||||
res.subscribe(value => {
|
||||
localforage.setItem(storageName, value);
|
||||
});
|
||||
localforage.getItem(storageName).then(value => {
|
||||
if (value == null) {
|
||||
const migrated = localStorage.getItem(storageName);
|
||||
if (migrated) {
|
||||
localStorage.removeItem(storageName);
|
||||
const parsed = safeJsonParse(migrated, defaultValue, true);
|
||||
localforage.setItem(storageName, parsed);
|
||||
res.set(parsed as T);
|
||||
}
|
||||
} else {
|
||||
res.set(value as T);
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
export function writableSettingsValue<T>(defaultValue: T, storageName) {
|
||||
const res = derived(useSettings(), $settings => {
|
||||
const obj = $settings || {};
|
||||
@@ -63,7 +85,7 @@ export const openedConnections = writable([]);
|
||||
export const openedSingleDatabaseConnections = writable([]);
|
||||
export const expandedConnections = writable([]);
|
||||
export const currentDatabase = writable(null);
|
||||
export const openedTabs = writableWithStorage<TabDefinition[]>([], 'openedTabs');
|
||||
export const openedTabs = writableWithForage<TabDefinition[]>([], 'openedTabs');
|
||||
export const copyRowsFormat = writableWithStorage('textWithoutHeaders', 'copyRowsFormat');
|
||||
export const extensions = writable<ExtensionsDirectory>(null);
|
||||
export const visibleCommandPalette = writable(null);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<script lang="ts">
|
||||
import localforage from 'localforage';
|
||||
import _ from 'lodash';
|
||||
import { TabDefinition } from '../stores';
|
||||
import getElectron from './getElectron';
|
||||
import { getLocalStorage, setLocalStorage } from './storageCache';
|
||||
|
||||
let counter = 0;
|
||||
$: counterCopy = counter;
|
||||
@@ -26,15 +26,15 @@
|
||||
)
|
||||
) {
|
||||
try {
|
||||
let openedTabs = getLocalStorage('openedTabs') || [];
|
||||
let openedTabs = (await localforage.getItem<TabDefinition[]>('openedTabs')) || [];
|
||||
if (!_.isArray(openedTabs)) openedTabs = [];
|
||||
openedTabs = openedTabs
|
||||
.map(tab => (tab.closedTime ? tab : { ...tab, closedTime: new Date().getTime() }))
|
||||
.map(tab => ({ ...tab, selected: false }));
|
||||
setLocalStorage('openedTabs', openedTabs);
|
||||
setLocalStorage('selectedWidget', 'history');
|
||||
await localforage.setItem('openedTabs', openedTabs);
|
||||
await localStorage.setItem('selectedWidget', 'history');
|
||||
} catch (err) {
|
||||
localStorage.removeItem('openedTabs');
|
||||
localforage.removeItem('openedTabs');
|
||||
}
|
||||
// try {
|
||||
// await localforage.clear();
|
||||
|
||||
@@ -2,13 +2,13 @@ import moment from 'moment';
|
||||
import localforage from 'localforage';
|
||||
|
||||
export default async function localStorageGarbageCollector() {
|
||||
const openedTabsJson = localStorage.getItem('openedTabs');
|
||||
const openedTabsJson = await localforage.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));
|
||||
await localforage.setItem('openedTabs', JSON.stringify(openedTabs));
|
||||
|
||||
const toRemove = [];
|
||||
for (const key in localStorage) {
|
||||
|
||||
Reference in New Issue
Block a user