frontend: refactor storage.js

This commit is contained in:
hippoz 2022-05-05 16:31:10 +03:00
parent b084de7ffc
commit b93b610eef
No known key found for this signature in database
GPG key ID: 7C52899193467641
5 changed files with 37 additions and 45 deletions

View file

@ -1,5 +1,5 @@
import gateway, { GatewayEventType } from "./gateway"; import gateway, { GatewayEventType } from "./gateway";
import { setAuthToken } from "./storage"; import { removeItem, setItem } from "./storage";
import { overlayStore } from "./stores"; import { overlayStore } from "./stores";
function useAuthHandlers() { function useAuthHandlers() {
@ -15,12 +15,12 @@ function useAuthHandlers() {
export function authWithToken(token, shouldUpdate=false) { export function authWithToken(token, shouldUpdate=false) {
if (shouldUpdate) if (shouldUpdate)
setAuthToken(token); setItem("token", token);
gateway.init(token); gateway.init(token);
} }
export function logOut() { export function logOut() {
setAuthToken(null); removeItem("token");
gateway.close(); gateway.close();
gateway.dispatch(GatewayEventType.BadAuth, -1); gateway.dispatch(GatewayEventType.BadAuth, -1);
} }

View file

@ -1,5 +1,5 @@
import logging from "./logging"; import logging from "./logging";
import { getAuthToken, getItem } from "./storage"; import { getItem } from "./storage";
export const GatewayErrors = { export const GatewayErrors = {
BAD_PAYLOAD: 4001, BAD_PAYLOAD: 4001,

View file

@ -1,6 +1,6 @@
import Main from './components/Main.svelte'; import Main from './components/Main.svelte';
import gateway from './gateway'; import gateway from './gateway';
import { getAuthToken, initStorageDefaults } from './storage'; import { getItem, init } from './storage';
import logging from "./logging"; import logging from "./logging";
import { authWithToken } from './auth'; import { authWithToken } from './auth';
import { initResponsiveHandlers } from './responsive'; import { initResponsiveHandlers } from './responsive';
@ -10,9 +10,9 @@ window.__waffle = {
gateway gateway
}; };
initStorageDefaults(); init();
initResponsiveHandlers(); initResponsiveHandlers();
authWithToken(getAuthToken()); authWithToken(getItem("token"));
// Remove loading screen // Remove loading screen
const loadingElement = document.getElementById("pre--loading-screen"); const loadingElement = document.getElementById("pre--loading-screen");

View file

@ -1,4 +1,4 @@
import { getAuthToken } from "./storage"; import { getItem } from "./storage";
export default async function(method, endpoint, auth=true, body=null) { export default async function(method, endpoint, auth=true, body=null) {
const options = { const options = {
@ -14,7 +14,7 @@ export default async function(method, endpoint, auth=true, body=null) {
} }
if (auth) { if (auth) {
const token = getAuthToken(); const token = getItem("token");
if (token) { if (token) {
options.headers = { options.headers = {
...options.headers || {}, ...options.headers || {},

View file

@ -1,54 +1,46 @@
const defaults = { const defaults = {
apiBase: `${window.location.origin}/api/v1`, apiBase: `${window.location.origin}/api/v1`,
gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`, gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`,
doAnimations: true doAnimations: true,
token: ""
}; };
const store = new Map(Object.entries(defaults));
const persistentProvider = localStorage;
const dummyProvider = { export function setItem(key, value) {
_store: {}, store.set(key, value);
getItem(key) { if (persistentProvider) {
return this._store[key]; persistentProvider.setItem(key, typeof value === "string" ? value : JSON.stringify(value));
},
setItem(key, value) {
this._store[key] = value;
} }
};
function getProvider() {
return window.localStorage || dummyProvider;
} }
export function getItem(key) { export function getItem(key) {
return getProvider().getItem(key); return store.get(key);
} }
export function setItem(key, value) { export function removeItem(key) {
return getProvider().setItem(key, value); store.delete(key);
if (persistentProvider) {
persistentProvider.removeItem(key);
}
} }
export function getAuthToken() { export function init() {
return getItem("token"); if (!persistentProvider)
} return;
export function setAuthToken(value) { store.forEach((defaultValue, key) => {
return setItem("token", value); const override = persistentProvider.getItem(key);
if (override !== null) {
try {
store.set(key, typeof defaultValue === "string" ? override : JSON.parse(override));
} catch (o_O) {
console.warn("[Storage]", `init(): An exception was thrown while parsing the value of key "${key}" from _persistentProvider`, o_O);
}
}
});
} }
export function apiRoute(fragment) { export function apiRoute(fragment) {
return `${getItem("apiBase")}/${fragment}`; return `${getItem("apiBase")}/${fragment}`
}
export function setItemIfNull(key, value) {
const provider = getProvider();
if (provider.getItem(key) === undefined) {
provider.setItem(key, value);
return true;
}
return false;
}
export function initStorageDefaults() {
for (const [k, v] of Object.entries(defaults)) {
setItemIfNull(k, v);
}
} }