Files
CT/src/Model/storageModel.ts
T

95 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-07-19 18:26:04 +02:00
// this file is responsible for reading and writing persistent data; all storage shall be handled by this file.
import { parse, stringify } from "../utility";
2024-07-19 19:23:48 +02:00
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);
};
2024-07-19 18:39:13 +02:00
2024-07-19 19:23:48 +02:00
restore = (directoryNames: string[], key: string): string | null => {
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
return localStorage.getItem(unifiedPath);
};
2024-07-19 18:26:04 +02:00
2024-07-19 19:23:48 +02:00
remove = (directoryNames: string[], key: string): void => {
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
localStorage.removeItem(unifiedPath);
};
list = (...directoryNames: string[]): string[] => {
let currentParent = this.storageEntryTree;
for (const directoryName of directoryNames) {
currentParent = currentParent[directoryName];
}
2024-07-19 18:26:04 +02:00
2024-07-19 19:23:48 +02:00
return [...Object.keys(currentParent)];
};
// array
storeArray = (directoryNames: string[], key: string, value: any[]): void => {
2024-07-19 18:26:04 +02:00
const valueString = stringify(value);
2024-07-19 19:23:48 +02:00
this.store(directoryNames, key, valueString);
};
2024-07-19 18:26:04 +02:00
2024-07-19 19:23:48 +02:00
restoreArray = (directoryNames: string[], key: string): any[] | null => {
const valueString = this.restore(directoryNames, key);
2024-07-19 18:26:04 +02:00
if (!valueString) return null;
2024-07-19 19:23:48 +02:00
2024-07-19 18:26:04 +02:00
const parsed = parse(valueString);
if (!Array.isArray(parsed)) return null;
return parsed;
2024-07-19 19:23:48 +02:00
};
// 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 {
2024-07-19 19:29:50 +02:00
// connection
socketAddress = "v2/connection/socket-address",
2024-07-19 19:23:48 +02:00
// settings
2024-07-19 19:29:50 +02:00
userName = "v2/settings/user-name",
firstDayOfWeek = "v2/settings/first-day-of-week",
2024-07-19 19:23:48 +02:00
// history
2024-07-19 19:29:50 +02:00
previousAddresses = "v2/history/previous-addresses",
previousObjectCategories = "v2/history/object-categories",
previousObjectStatuses = "v2/history/object-statuses",
previousObjectFilters = "v2/history/object-filters",
2024-07-19 19:23:48 +02:00
}