Files
CT/src/Model/Global/storageModel.ts
T

257 lines
7.1 KiB
TypeScript
Raw Normal View History

2024-07-22 02:23:21 +02:00
// this file is responsible for reading and writing persistent data.
2024-07-19 18:26:04 +02:00
2024-07-25 16:44:08 +02:00
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
2024-07-22 02:05:27 +02:00
import { localeCompare, parseValidObject, stringify } from "../Utility/utility";
2024-07-19 20:50:42 +02:00
2024-07-21 18:56:56 +02:00
export const PATH_COMPONENT_SEPARATOR = "\\";
2024-07-19 19:23:48 +02:00
export default class StorageModel {
storageEntryTree: StorageEntry = {};
2024-07-22 15:06:01 +02:00
// read
read = (pathComponents: string[]): string | null => {
2024-07-21 17:55:53 +02:00
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
2024-07-22 15:06:01 +02:00
return localStorage.getItem(pathString);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:39:13 +02:00
2024-07-22 15:06:01 +02:00
list = (pathComponents: string[]): string[] => {
let currentParent: StorageEntry = this.storageEntryTree;
for (const component of pathComponents) {
const nextParent: StorageEntry | undefined = currentParent[component];
if (nextParent == undefined) return [];
currentParent = nextParent;
}
return [...Object.keys(currentParent).sort(localeCompare)];
};
// write
write = (pathComponents: string[], value: string): void => {
2024-07-21 17:55:53 +02:00
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
2024-07-22 15:06:01 +02:00
localStorage.setItem(pathString, value);
this.updateTree(...pathComponents);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:26:04 +02:00
2024-07-20 04:36:15 +02:00
remove = (
pathComponents: string[],
shouldInitialize: boolean = true
): void => {
2024-07-21 17:55:53 +02:00
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.removeItem(pathString);
2024-07-20 04:36:15 +02:00
if (shouldInitialize == true) {
2024-07-20 04:36:15 +02:00
this.initializeTree();
}
};
2024-07-22 15:06:01 +02:00
removeRecursively = (pathComponents: string[]): void => {
loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity
2024-07-20 04:36:15 +02:00
const pathComponentsOfCurrentEntity: string[] =
2024-07-21 17:55:53 +02:00
StorageModel.stringToPathComponents(key);
2024-07-20 04:36:15 +02:00
2024-07-22 15:06:01 +02:00
// exit if entity does not match
loop_over_path_components: for (
let i = 0;
i < pathComponents.length;
i++
) {
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files;
2024-07-20 04:36:15 +02:00
}
2024-07-22 15:06:01 +02:00
// remove
this.remove(pathComponentsOfCurrentEntity, false);
2024-07-20 04:36:15 +02:00
}
this.initializeTree();
2024-07-19 19:23:48 +02:00
};
2024-07-22 15:06:01 +02:00
rename = (
sourcePathComponents: string[],
destinationPathComponents: string[],
shouldInitialize: boolean = true
): boolean => {
const content: string | null = this.read(sourcePathComponents);
2024-07-22 12:56:16 +02:00
if (content == null) return false;
this.write(destinationPathComponents, content);
this.remove(sourcePathComponents);
2024-07-22 15:06:01 +02:00
if (shouldInitialize == true) {
this.initializeTree();
}
2024-07-22 12:56:16 +02:00
return true;
2024-07-22 15:06:01 +02:00
};
renameRecursively = (
sourcePathComponents: string[],
destinationPathComponents: string[]
): void => {
loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity
const originalPathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key);
// exit if entity does not match
loop_over_path_components: for (
let i = 0;
i < sourcePathComponents.length;
i++
) {
if (!originalPathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (originalPathComponentsOfCurrentEntity[i] != sourcePathComponents[i])
continue loop_over_files;
}
// assemble destinationPath
const relativePathOfCurrentEntity =
originalPathComponentsOfCurrentEntity.slice(
sourcePathComponents.length
);
const destinationPathComponentsOfCurrentEntity = [
...destinationPathComponents,
...relativePathOfCurrentEntity,
];
// rename
this.rename(
originalPathComponentsOfCurrentEntity,
destinationPathComponentsOfCurrentEntity,
false
);
}
this.initializeTree();
};
2024-07-22 12:56:16 +02:00
// stringifiable
2024-07-21 23:32:20 +02:00
writeStringifiable = (pathComponents: string[], value: ValidObject): void => {
2024-07-19 20:50:42 +02:00
const valueString: string = stringify(value);
2024-07-21 21:41:19 +02:00
this.write(pathComponents, valueString);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:26:04 +02:00
readStringifiable = <T extends ValidObject>(
pathComponents: string[],
reference: T
): T | null => {
2024-07-21 21:41:19 +02:00
const valueString: string | null = this.read(pathComponents);
2024-07-19 18:26:04 +02:00
if (!valueString) return null;
2024-07-19 19:23:48 +02:00
const object: any | null = parseValidObject(valueString, reference);
2024-07-22 01:38:08 +02:00
if (object == null) return null;
2024-07-21 23:32:20 +02:00
return object;
2024-07-19 19:23:48 +02:00
};
2024-07-22 03:01:04 +02:00
// tree
2024-07-20 03:34:54 +02:00
initializeTree = (): void => {
2024-07-20 05:10:50 +02:00
console.log("initializing tree");
2024-07-20 03:34:54 +02:00
this.storageEntryTree = {};
2024-07-19 19:23:48 +02:00
for (const key of Object.keys(localStorage)) {
2024-07-21 17:55:53 +02:00
const components: string[] = StorageModel.stringToPathComponents(key);
2024-07-19 19:32:41 +02:00
this.updateTree(...components);
2024-07-19 19:23:48 +02:00
}
2024-07-20 03:34:54 +02:00
};
2024-07-19 19:23:48 +02:00
2024-07-20 03:34:54 +02:00
updateTree = (...pathComponents: string[]): void => {
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-21 17:55:53 +02:00
printTree = (): string => {
return stringify(this.storageEntryTree);
2024-07-20 05:10:50 +02:00
};
2024-07-22 03:01:04 +02:00
// init
constructor() {
this.initializeTree();
}
// utility
2024-07-21 18:56:56 +02:00
static getFileName = (pathComponents: string[]): string => {
2024-07-21 20:54:18 +02:00
return pathComponents[pathComponents.length - 1] || "\\";
};
2024-07-21 18:56:56 +02:00
static getFileNameFromString = (pathString: string): string => {
const pathComponents: string[] = this.stringToPathComponents(pathString);
2024-07-21 20:54:18 +02:00
return pathComponents[pathComponents.length - 1] || "\\";
};
2024-07-21 18:56:56 +02:00
2024-07-21 17:55:53 +02:00
static pathComponentsToString = (...pathComponents: string[]): string => {
2024-07-22 15:06:01 +02:00
return pathComponents.filter((x) => x != "").join(PATH_COMPONENT_SEPARATOR);
2024-07-19 19:23:48 +02:00
};
2024-07-21 17:55:53 +02:00
static stringToPathComponents = (string: string): string[] => {
return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != "");
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) {
2024-07-21 17:55:53 +02:00
const parts = this.stringToPathComponents(item);
2024-07-19 20:50:42 +02:00
allComponents.push(...parts);
}
2024-07-21 17:55:53 +02:00
return StorageModel.pathComponentsToString(...allComponents);
2024-07-19 20:50:42 +02:00
};
2024-07-22 03:01:04 +02:00
static getPath(
2024-07-25 16:44:08 +02:00
locationName: StorageModelSubPath,
2024-07-22 03:01:04 +02:00
filePath: string[]
): string[] {
2024-07-25 16:44:08 +02:00
return [DATA_VERSION, locationName, ...filePath];
2024-07-22 03:01:04 +02:00
}
2024-07-19 19:23:48 +02:00
}
// types
export type StorageEntry = { [key: string]: StorageEntry };
2024-07-22 03:01:04 +02:00
// locations
2024-07-25 16:44:08 +02:00
export enum StorageModelSubPath {
Chat = "chat",
ConnectionModel = "connection",
SettingsModel = "settings",
}
2024-07-22 03:01:04 +02:00
export const filePaths = {
connectionModel: {
socketAddress: ["socket-address"],
reconnectAddress: ["reconnect-address"],
outbox: ["outbox"],
mailboxes: ["mailboxes"],
previousAddresses: ["previous-addresses"],
},
chat: {
base: [],
chatBase: (id: string) => [id],
info: (id: string) => [...filePaths.chat.chatBase(id), "info"],
color: (id: string) => [...filePaths.chat.chatBase(id), "color"],
messages: (id: string) => [...filePaths.chat.chatBase(id), "messages"],
lastUsedPage: (id: string) => [
...filePaths.chat.chatBase(id),
"last-used-page",
],
2024-07-22 12:39:26 +02:00
files: (id: string) => [...filePaths.chat.chatBase(id), "files"],
2024-07-22 03:01:04 +02:00
},
settingsModel: {
username: ["user-name"],
firstDayOfWeek: ["first-day-of-week"],
},
2024-07-22 12:39:26 +02:00
};