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

198 lines
5.4 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-21 23:32:20 +02:00
import {
DATA_VERSION,
ValidObject,
checkMatchesObjectStructure,
2024-07-22 02:05:27 +02:00
} from "../Utility/typeSafety";
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 = {};
// basic
2024-07-21 21:41:19 +02:00
write = (pathComponents: string[], value: string): void => {
2024-07-21 17:55:53 +02:00
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.setItem(pathString, value);
2024-07-19 19:32:41 +02:00
this.updateTree(...pathComponents);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:39:13 +02:00
2024-07-21 21:41:19 +02:00
read = (pathComponents: string[]): string | null => {
2024-07-21 17:55:53 +02:00
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
return localStorage.getItem(pathString);
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 01:38:08 +02:00
removeRecursively = (pathComponentsOfEntityToDelete: string[]): void => {
2024-07-20 04:36:15 +02:00
a: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity: string[] =
2024-07-21 17:55:53 +02:00
StorageModel.stringToPathComponents(key);
2024-07-20 04:36:15 +02:00
for (let i = 0; i < pathComponentsOfEntityToDelete.length; i++) {
if (!pathComponentsOfCurrentEntity[i]) continue a;
if (
pathComponentsOfCurrentEntity[i] != pathComponentsOfEntityToDelete[i]
)
continue a;
}
this.remove(pathComponentsOfCurrentEntity, false);
2024-07-20 04:36:15 +02:00
}
this.initializeTree();
2024-07-19 19:23:48 +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-21 17:55:53 +02:00
return [...Object.keys(currentParent).sort(localeCompare)];
2024-07-19 19:23:48 +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 => {
return pathComponents.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(
locationName: keyof typeof storageLocations,
filePath: string[]
): string[] {
return [DATA_VERSION, storageLocations[locationName], ...filePath];
}
2024-07-19 19:23:48 +02:00
}
// types
export type StorageEntry = { [key: string]: StorageEntry };
2024-07-22 03:01:04 +02:00
// locations
const storageLocations = {
connectionModel: "connection",
chat: "chat",
settingsModel: "settings",
2024-07-19 19:52:00 +02:00
};
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"],
files: (id: string) => [...filePaths.chat.chatBase(id), "files"],
lastUsedPage: (id: string) => [
...filePaths.chat.chatBase(id),
"last-used-page",
],
},
settingsModel: {
username: ["user-name"],
firstDayOfWeek: ["first-day-of-week"],
},
};