Files
CT/src/Model/storageModel.ts
T

100 lines
2.8 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
2024-07-19 19:32:41 +02:00
store = (pathComponents: string[], value: string): void => {
const key = this.pathComponentsToKey(...pathComponents);
localStorage.setItem(key, value);
this.updateTree(...pathComponents);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:39:13 +02:00
2024-07-19 19:32:41 +02:00
restore = (pathComponents: string[]): string | null => {
const key = this.pathComponentsToKey(...pathComponents);
return localStorage.getItem(key);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:26:04 +02:00
2024-07-19 19:32:41 +02:00
remove = (pathComponents: string[]): void => {
const key = this.pathComponentsToKey(...pathComponents);
localStorage.removeItem(key);
2024-07-19 19:23:48 +02:00
};
2024-07-19 19:32:41 +02:00
list = (...pathComponents: string[]): string[] => {
2024-07-19 19:23:48 +02:00
let currentParent = this.storageEntryTree;
2024-07-19 19:32:41 +02:00
for (const component of pathComponents) {
currentParent = currentParent[component];
2024-07-19 19:23:48 +02:00
}
2024-07-19 18:26:04 +02:00
2024-07-19 19:23:48 +02:00
return [...Object.keys(currentParent)];
};
// array
2024-07-19 19:32:41 +02:00
storeArray = (pathComponents: string[], value: any[]): void => {
2024-07-19 18:26:04 +02:00
const valueString = stringify(value);
2024-07-19 19:32:41 +02:00
this.store(pathComponents, valueString);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:26:04 +02:00
2024-07-19 19:32:41 +02:00
restoreArray = (pathComponents: string[]): any[] | null => {
const valueString = this.restore(pathComponents);
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)) {
2024-07-19 19:32:41 +02:00
const components = this.keyToPathComponents(key);
this.updateTree(...components);
2024-07-19 19:23:48 +02:00
}
}
// utility
2024-07-19 19:32:41 +02:00
updateTree = (...pathComponents: string[]) => {
2024-07-19 19:23:48 +02:00
let currentParent = this.storageEntryTree;
2024-07-19 19:32:41 +02:00
for (const pathPart of pathComponents) {
2024-07-19 19:23:48 +02:00
if (!currentParent[pathPart]) {
currentParent[pathPart] = {};
}
currentParent = currentParent[pathPart];
}
};
2024-07-19 19:32:41 +02:00
pathComponentsToKey = (...pathComponents: string[]): string => {
return pathComponents.join("/");
2024-07-19 19:23:48 +02:00
};
2024-07-19 19:32:41 +02:00
keyToPathComponents = (key: string): string[] => {
2024-07-19 19:23:48 +02:00
return key.split("/");
};
}
// types
export type StorageEntry = { [key: string]: StorageEntry };
// keys
2024-07-19 19:52:00 +02:00
export const storageKeys = {
2024-07-19 19:29:50 +02:00
// connection
2024-07-19 19:52:00 +02:00
socketAddress: "v2/connection/socket-address",
2024-07-19 19:23:48 +02:00
// settings
2024-07-19 19:52:00 +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:52:00 +02:00
previousAddresses: "v2/history/previous-addresses",
previousObjectCategories: "v2/history/object-categories",
previousObjectStatuses: "v2/history/object-statuses",
previousObjectFilters: "v2/history/object-filters",
// chat etc
chatInfo: (id: string) => `v2/chat/${id}/info`,
chatMessages: (id: string) => `v2/chat/${id}/messages`,
chatObjects: (id: string) => `v2/chat/${id}/objects`,
};