implement fake directories for storageModel

This commit is contained in:
marlon-erler
2024-07-19 19:23:48 +02:00
parent 8a3b62152e
commit 036880f74c
3 changed files with 153 additions and 28 deletions
+71
View File
@@ -261,6 +261,9 @@
function stringify(data) { function stringify(data) {
return JSON.stringify(data); return JSON.stringify(data);
} }
function parse(string) {
return JSON.parse(string);
}
// src/Model/connectionModel.ts // src/Model/connectionModel.ts
var ConnectionModel = class { var ConnectionModel = class {
@@ -327,4 +330,72 @@
document.querySelector("main").append( document.querySelector("main").append(
/* @__PURE__ */ createElement("div", { class: "flex-column" }, /* @__PURE__ */ createElement("input", { "bind:value": addressInput }), /* @__PURE__ */ createElement("button", { "on:click": connect, "toggle:disabled": isConnected }, "Connect"), /* @__PURE__ */ createElement("button", { "on:click": disconnect }, "Disconnect"), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("span", { "subscribe:innerText": connectedAddress })) /* @__PURE__ */ createElement("div", { class: "flex-column" }, /* @__PURE__ */ createElement("input", { "bind:value": addressInput }), /* @__PURE__ */ createElement("button", { "on:click": connect, "toggle:disabled": isConnected }, "Connect"), /* @__PURE__ */ createElement("button", { "on:click": disconnect }, "Disconnect"), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("span", { "subscribe:innerText": connectedAddress }))
); );
// src/Model/storageModel.ts
var StorageModel = class {
storageEntryTree = {};
// basic
store = (directoryNames, key, value) => {
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
localStorage.setItem(unifiedPath, value);
this.updateTree(...directoryNames, key);
};
restore = (directoryNames, key) => {
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
return localStorage.getItem(unifiedPath);
};
remove = (directoryNames, key) => {
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
localStorage.removeItem(unifiedPath);
};
list = (...directoryNames) => {
let currentParent = this.storageEntryTree;
for (const directoryName of directoryNames) {
currentParent = currentParent[directoryName];
}
return [...Object.keys(currentParent)];
};
// array
storeArray = (directoryNames, key, value) => {
const valueString = stringify(value);
this.store(directoryNames, key, valueString);
};
restoreArray = (directoryNames, key) => {
const valueString = this.restore(directoryNames, key);
if (!valueString) return null;
const parsed = parse(valueString);
if (!Array.isArray(parsed)) return null;
return parsed;
};
// init
constructor() {
for (const key of Object.keys(localStorage)) {
const directoryNames = this.keyToDirectoryNames(key);
this.updateTree(...directoryNames);
}
}
// utility
updateTree = (...pathParts) => {
let currentParent = this.storageEntryTree;
for (const pathPart of pathParts) {
if (!currentParent[pathPart]) {
currentParent[pathPart] = {};
}
currentParent = currentParent[pathPart];
}
};
directoryNamesToKey = (...directoryNames) => {
return directoryNames.join("/");
};
keyToDirectoryNames = (key) => {
return key.split("/");
};
};
// src/index.tsx
var storageModel = new StorageModel();
console.log(storageModel.list("a"));
console.log(storageModel.list("b"));
console.log(storageModel.list("b", "c"));
console.log(JSON.stringify(storageModel.storageEntryTree, null, 4));
})(); })();
+76 -26
View File
@@ -2,43 +2,93 @@
import { parse, stringify } from "../utility"; import { parse, stringify } from "../utility";
// keys export default class StorageModel {
export enum storageKeys { storageEntryTree: StorageEntry = {}; // basic
// current store = (directoryNames: string[], key: string, value: string): void => {
socketAddress = "v2-socket-address", const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
localStorage.setItem(unifiedPath, value);
this.updateTree(...directoryNames, key);
};
// settings restore = (directoryNames: string[], key: string): string | null => {
userName = "v2-user-name", const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
firstDayOfWeek = "v2-first-day-of-week", return localStorage.getItem(unifiedPath);
};
// history remove = (directoryNames: string[], key: string): void => {
previousAddresses = "v2-previous-addresses", const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
previousObjectCategories = "v2-object-categories", localStorage.removeItem(unifiedPath);
previousObjectStatuses = "v2-object-statuses", };
previousObjectFilters = "v2-object-filters",
}
// basic list = (...directoryNames: string[]): string[] => {
export function store(key: string, value: string): void { let currentParent = this.storageEntryTree;
localStorage.setItem(key, value); for (const directoryName of directoryNames) {
} currentParent = currentParent[directoryName];
}
export function restore(key: string): string | null { return [...Object.keys(currentParent)];
return localStorage.getItem(key); };
}
// array // array
export function storeArray(key: string, value: any[]): void { storeArray = (directoryNames: string[], key: string, value: any[]): void => {
const valueString = stringify(value); const valueString = stringify(value);
store(key, valueString); this.store(directoryNames, key, valueString);
} };
export function restoreArray(key: string): any[] | null { restoreArray = (directoryNames: string[], key: string): any[] | null => {
const valueString = restore(key); const valueString = this.restore(directoryNames, key);
if (!valueString) return null; if (!valueString) return null;
const parsed = parse(valueString); const parsed = parse(valueString);
if (!Array.isArray(parsed)) return null; if (!Array.isArray(parsed)) return null;
return parsed; return parsed;
};
// init
constructor() {
for (const key of Object.keys(localStorage)) {
const directoryNames = this.keyToDirectoryNames(key);
this.updateTree(...directoryNames);
}
}
// utility
updateTree = (...pathParts: string[]) => {
let currentParent = this.storageEntryTree;
for (const pathPart of pathParts) {
if (!currentParent[pathPart]) {
currentParent[pathPart] = {};
}
currentParent = currentParent[pathPart];
}
};
directoryNamesToKey = (...directoryNames: string[]): string => {
return directoryNames.join("/");
};
keyToDirectoryNames = (key: string): string[] => {
return key.split("/");
};
}
// types
export type StorageEntry = { [key: string]: StorageEntry };
// keys
export enum storageKeys {
// current
socketAddress = "v2-socket-address",
// settings
userName = "v2-user-name",
firstDayOfWeek = "v2-first-day-of-week",
// history
previousAddresses = "v2-previous-addresses",
previousObjectCategories = "v2-object-categories",
previousObjectStatuses = "v2-object-statuses",
previousObjectFilters = "v2-object-filters",
} }
+4
View File
@@ -1 +1,5 @@
import "./View/maintenance"; import "./View/maintenance";
import StorageModel from "./Model/storageModel";
const storageModel = new StorageModel();