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.
|
|
|
|
|
|
2024-07-19 23:45:07 +02:00
|
|
|
import { parse, stringify } from "./Utility/utility";
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-19 23:45:07 +02:00
|
|
|
import { DATA_VERSION } from "./Utility/typeSafety";
|
2024-07-19 20:50:42 +02:00
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
const PATH_COMPONENT_SEPARATOR = "\\";
|
|
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
export default class StorageModel {
|
2024-07-19 20:05:03 +02:00
|
|
|
storageEntryTree: StorageEntry = {};
|
|
|
|
|
|
|
|
|
|
// basic
|
2024-07-20 02:56:44 +02:00
|
|
|
store = (pathComponents: string[], value: string): void => {
|
|
|
|
|
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
|
2024-07-19 19:32:41 +02:00
|
|
|
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-20 02:56:44 +02:00
|
|
|
restore = (pathComponents: string[]): string | null => {
|
|
|
|
|
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
|
2024-07-19 19:32:41 +02:00
|
|
|
return localStorage.getItem(key);
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
remove = (pathComponents: string[]): void => {
|
|
|
|
|
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
|
2024-07-19 19:32:41 +02:00
|
|
|
localStorage.removeItem(key);
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
list = (pathComponents: string[]): string[] => {
|
2024-07-19 20:50:42 +02:00
|
|
|
let currentParent: StorageEntry = this.storageEntryTree;
|
2024-07-19 19:32:41 +02:00
|
|
|
for (const component of pathComponents) {
|
2024-07-19 21:10:16 +02:00
|
|
|
const nextParent: StorageEntry | undefined = currentParent[component];
|
|
|
|
|
if (nextParent == undefined) return [];
|
|
|
|
|
currentParent = nextParent;
|
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)];
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-19 20:05:03 +02:00
|
|
|
// stringifiable
|
2024-07-20 02:56:44 +02:00
|
|
|
storeStringifiable = (pathComponents: string[], value: any): void => {
|
2024-07-19 20:50:42 +02:00
|
|
|
const valueString: string = stringify(value);
|
2024-07-20 02:56:44 +02:00
|
|
|
this.store(pathComponents, valueString);
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-20 02:56:44 +02:00
|
|
|
restoreStringifiable = (pathComponents: string[]): any | null => {
|
|
|
|
|
const valueString: string | null = 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 20:05:03 +02:00
|
|
|
return parse(valueString);
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor() {
|
|
|
|
|
for (const key of Object.keys(localStorage)) {
|
2024-07-19 20:50:42 +02:00
|
|
|
const components: string[] = StorageModel.keyToPathComponents(key);
|
2024-07-19 19:32:41 +02:00
|
|
|
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 20:50:42 +02:00
|
|
|
let currentParent: StorageEntry = 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 20:50:42 +02:00
|
|
|
static pathComponentsToKey = (...pathComponents: string[]): string => {
|
2024-07-20 02:56:44 +02:00
|
|
|
return pathComponents.join(PATH_COMPONENT_SEPARATOR);
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-19 20:50:42 +02:00
|
|
|
static keyToPathComponents = (key: string): string[] => {
|
2024-07-20 02:56:44 +02:00
|
|
|
return key.split(PATH_COMPONENT_SEPARATOR);
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
2024-07-19 20:50:42 +02:00
|
|
|
|
|
|
|
|
static join = (...items: string[]): string => {
|
|
|
|
|
let allComponents: string[] = [];
|
|
|
|
|
for (const item of items) {
|
|
|
|
|
const parts = this.keyToPathComponents(item);
|
|
|
|
|
allComponents.push(...parts);
|
|
|
|
|
}
|
|
|
|
|
return StorageModel.pathComponentsToKey(...allComponents);
|
|
|
|
|
};
|
2024-07-19 19:23:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-20 02:56:44 +02:00
|
|
|
socketAddress: [DATA_VERSION, "connection", "socket-address"],
|
2024-07-19 19:23:48 +02:00
|
|
|
|
|
|
|
|
// settings
|
2024-07-20 02:56:44 +02:00
|
|
|
username: [DATA_VERSION, "settings", "user-name"],
|
|
|
|
|
firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"],
|
2024-07-19 19:23:48 +02:00
|
|
|
|
|
|
|
|
// history
|
2024-07-20 02:56:44 +02:00
|
|
|
previousAddresses: [DATA_VERSION, "history", "previous-addresses"],
|
|
|
|
|
previousObjectCategories: [DATA_VERSION, "history", "object-categories"],
|
|
|
|
|
previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"],
|
|
|
|
|
previousObjectFilters: [DATA_VERSION, "history", "object-filters"],
|
2024-07-19 19:52:00 +02:00
|
|
|
|
|
|
|
|
// chat etc
|
2024-07-20 02:56:44 +02:00
|
|
|
chatInfo: (id: string) => [DATA_VERSION, "chat", id, "info"],
|
|
|
|
|
chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"],
|
|
|
|
|
chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"],
|
|
|
|
|
chatOutbox: (id: string) => [DATA_VERSION, "chat", id, "outbox"],
|
2024-07-19 19:52:00 +02:00
|
|
|
};
|