diff --git a/dist/index.js b/dist/index.js index 2138ddcd..f38f6229 100644 --- a/dist/index.js +++ b/dist/index.js @@ -261,6 +261,9 @@ function stringify(data) { return JSON.stringify(data); } + function parse(string) { + return JSON.parse(string); + } // src/Model/connectionModel.ts var ConnectionModel = class { @@ -327,4 +330,72 @@ 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 })) ); + + // 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)); })(); diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index dadae642..35775ae8 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -2,43 +2,93 @@ import { parse, stringify } from "../utility"; -// keys -export enum storageKeys { - // current - socketAddress = "v2-socket-address", +export default class StorageModel { + storageEntryTree: StorageEntry = {}; // basic + store = (directoryNames: string[], key: string, value: string): void => { + const unifiedPath = this.directoryNamesToKey(...directoryNames, key); + localStorage.setItem(unifiedPath, value); + this.updateTree(...directoryNames, key); + }; - // settings - userName = "v2-user-name", - firstDayOfWeek = "v2-first-day-of-week", + restore = (directoryNames: string[], key: string): string | null => { + const unifiedPath = this.directoryNamesToKey(...directoryNames, key); + return localStorage.getItem(unifiedPath); + }; - // history - previousAddresses = "v2-previous-addresses", - previousObjectCategories = "v2-object-categories", - previousObjectStatuses = "v2-object-statuses", - previousObjectFilters = "v2-object-filters", -} + remove = (directoryNames: string[], key: string): void => { + const unifiedPath = this.directoryNamesToKey(...directoryNames, key); + localStorage.removeItem(unifiedPath); + }; -// basic -export function store(key: string, value: string): void { - localStorage.setItem(key, value); -} + list = (...directoryNames: string[]): string[] => { + let currentParent = this.storageEntryTree; + for (const directoryName of directoryNames) { + currentParent = currentParent[directoryName]; + } -export function restore(key: string): string | null { - return localStorage.getItem(key); -} + return [...Object.keys(currentParent)]; + }; -// array -export function storeArray(key: string, value: any[]): void { + // array + storeArray = (directoryNames: string[], key: string, value: any[]): void => { const valueString = stringify(value); - store(key, valueString); -} + this.store(directoryNames, key, valueString); + }; -export function restoreArray(key: string): any[] | null { - const valueString = restore(key); + restoreArray = (directoryNames: string[], key: string): any[] | null => { + const valueString = this.restore(directoryNames, key); if (!valueString) return null; - + const parsed = parse(valueString); if (!Array.isArray(parsed)) return null; return parsed; -} \ No newline at end of file + }; + + // 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", +} diff --git a/src/index.tsx b/src/index.tsx index a38c48d0..eecb96e6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1 +1,5 @@ import "./View/maintenance"; + +import StorageModel from "./Model/storageModel"; + +const storageModel = new StorageModel(); \ No newline at end of file