Use localStoragerather than sessionStorage for cache. Rename variable.

This commit is contained in:
Leo Vasanko 2023-11-13 13:04:39 +00:00
parent 9b58b887b4
commit dc4bb494f3
2 changed files with 11 additions and 8 deletions

View File

@ -6,24 +6,26 @@ export const uploadUrl = '/api/upload'
export const watchUrl = '/api/watch' export const watchUrl = '/api/watch'
let tree = [] as FileEntry[] let tree = [] as FileEntry[]
let reconnectDuration = 500 let reconnDelay = 500
let wsWatch = null as WebSocket | null let wsWatch = null as WebSocket | null
export const loadSession = () => { export const loadSession = () => {
const s = localStorage['cista-files']
if (!s) return false
const store = useDocumentStore() const store = useDocumentStore()
try { try {
tree = JSON.parse(sessionStorage['cista-files']) tree = JSON.parse(s)
store.updateRoot(tree) store.updateRoot(tree)
console.log(`Loaded session ${tree.length} items`) console.log(`Loaded session with ${tree.length} items cached`)
return true return true
} catch (error) { } catch (error) {
if (sessionStorage['cista-files']) console.log("Loading session failed", error) console.log("Loading session failed", error)
return false return false
} }
} }
const saveSession = () => { const saveSession = () => {
sessionStorage["cista-files"] = JSON.stringify(tree) localStorage["cista-files"] = JSON.stringify(tree)
} }
export const connect = (path: string, handlers: Partial<Record<keyof WebSocketEventMap, any>>) => { export const connect = (path: string, handlers: Partial<Record<keyof WebSocketEventMap, any>>) => {
@ -61,7 +63,7 @@ export const watchConnect = () => {
console.log('Connected to backend', msg) console.log('Connected to backend', msg)
store.server = msg.server store.server = msg.server
store.connected = true store.connected = true
reconnectDuration = 500 reconnDelay = 500
store.error = '' store.error = ''
if (msg.user) store.login(msg.user.username, msg.user.privileged) if (msg.user) store.login(msg.user.username, msg.user.privileged)
else if (store.isUserLogged) store.logout() else if (store.isUserLogged) store.logout()
@ -85,10 +87,10 @@ const watchReconnect = (event: MessageEvent) => {
store.connected = false store.connected = false
store.error = 'Reconnecting...' store.error = 'Reconnecting...'
} }
reconnectDuration = Math.min(5000, reconnectDuration + 500) reconnDelay = Math.min(5000, reconnDelay + 500)
// The server closes the websocket after errors, so we need to reopen it // The server closes the websocket after errors, so we need to reopen it
if (watchTimeout !== null) clearTimeout(watchTimeout) if (watchTimeout !== null) clearTimeout(watchTimeout)
watchTimeout = setTimeout(watchConnect, reconnectDuration) watchTimeout = setTimeout(watchConnect, reconnDelay)
} }

View File

@ -67,6 +67,7 @@ export const useDocumentStore = defineStore({
console.log("Logout") console.log("Logout")
await logoutUser() await logoutUser()
this.$reset() this.$reset()
localStorage.clear()
history.go() // Reload page history.go() // Reload page
} }
}, },