mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-29 19:13:59 +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 { writable, derived, readable } from 'svelte/store';
|
||||||
|
import localforage from 'localforage';
|
||||||
import type { ExtensionsDirectory } from 'dbgate-types';
|
import type { ExtensionsDirectory } from 'dbgate-types';
|
||||||
import invalidateCommands from './commands/invalidateCommands';
|
import invalidateCommands from './commands/invalidateCommands';
|
||||||
import getElectron from './utility/getElectron';
|
import getElectron from './utility/getElectron';
|
||||||
@@ -29,6 +30,27 @@ export function writableWithStorage<T>(defaultValue: T, storageName) {
|
|||||||
return res;
|
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) {
|
export function writableSettingsValue<T>(defaultValue: T, storageName) {
|
||||||
const res = derived(useSettings(), $settings => {
|
const res = derived(useSettings(), $settings => {
|
||||||
const obj = $settings || {};
|
const obj = $settings || {};
|
||||||
@@ -63,7 +85,7 @@ export const openedConnections = writable([]);
|
|||||||
export const openedSingleDatabaseConnections = writable([]);
|
export const openedSingleDatabaseConnections = writable([]);
|
||||||
export const expandedConnections = writable([]);
|
export const expandedConnections = writable([]);
|
||||||
export const currentDatabase = writable(null);
|
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 copyRowsFormat = writableWithStorage('textWithoutHeaders', 'copyRowsFormat');
|
||||||
export const extensions = writable<ExtensionsDirectory>(null);
|
export const extensions = writable<ExtensionsDirectory>(null);
|
||||||
export const visibleCommandPalette = writable(null);
|
export const visibleCommandPalette = writable(null);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import { TabDefinition } from '../stores';
|
||||||
import getElectron from './getElectron';
|
import getElectron from './getElectron';
|
||||||
import { getLocalStorage, setLocalStorage } from './storageCache';
|
|
||||||
|
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
$: counterCopy = counter;
|
$: counterCopy = counter;
|
||||||
@@ -26,15 +26,15 @@
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
let openedTabs = getLocalStorage('openedTabs') || [];
|
let openedTabs = (await localforage.getItem<TabDefinition[]>('openedTabs')) || [];
|
||||||
if (!_.isArray(openedTabs)) openedTabs = [];
|
if (!_.isArray(openedTabs)) openedTabs = [];
|
||||||
openedTabs = openedTabs
|
openedTabs = openedTabs
|
||||||
.map(tab => (tab.closedTime ? tab : { ...tab, closedTime: new Date().getTime() }))
|
.map(tab => (tab.closedTime ? tab : { ...tab, closedTime: new Date().getTime() }))
|
||||||
.map(tab => ({ ...tab, selected: false }));
|
.map(tab => ({ ...tab, selected: false }));
|
||||||
setLocalStorage('openedTabs', openedTabs);
|
await localforage.setItem('openedTabs', openedTabs);
|
||||||
setLocalStorage('selectedWidget', 'history');
|
await localStorage.setItem('selectedWidget', 'history');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
localStorage.removeItem('openedTabs');
|
localforage.removeItem('openedTabs');
|
||||||
}
|
}
|
||||||
// try {
|
// try {
|
||||||
// await localforage.clear();
|
// await localforage.clear();
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ import moment from 'moment';
|
|||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
|
|
||||||
export default async function localStorageGarbageCollector() {
|
export default async function localStorageGarbageCollector() {
|
||||||
const openedTabsJson = localStorage.getItem('openedTabs');
|
const openedTabsJson = await localforage.getItem('openedTabs');
|
||||||
let openedTabs = openedTabsJson ? JSON.parse(openedTabsJson) : [];
|
let openedTabs = openedTabsJson ? JSON.parse(openedTabsJson) : [];
|
||||||
|
|
||||||
const closeLimit = moment().add(-7, 'day').valueOf();
|
const closeLimit = moment().add(-7, 'day').valueOf();
|
||||||
|
|
||||||
openedTabs = openedTabs.filter(x => !x.closedTime || x.closedTime > closeLimit);
|
openedTabs = openedTabs.filter(x => !x.closedTime || x.closedTime > closeLimit);
|
||||||
localStorage.setItem('openedTabs', JSON.stringify(openedTabs));
|
await localforage.setItem('openedTabs', JSON.stringify(openedTabs));
|
||||||
|
|
||||||
const toRemove = [];
|
const toRemove = [];
|
||||||
for (const key in localStorage) {
|
for (const key in localStorage) {
|
||||||
|
|||||||
Reference in New Issue
Block a user