Files
CT/src/Model/storageModel.ts
T

113 lines
3.3 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 20:50:42 +02:00
import { DATA_VERSION } from "../typeSafety";
2024-07-19 19:23:48 +02:00
export default class StorageModel {
storageEntryTree: StorageEntry = {};
// basic
2024-07-19 20:07:14 +02:00
store = (key: string, value: string): void => {
2024-07-19 19:32:41 +02:00
localStorage.setItem(key, value);
2024-07-19 20:50:42 +02:00
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
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-19 20:07:14 +02:00
restore = (key: string): string | null => {
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-19 20:07:14 +02:00
remove = (key: string): void => {
2024-07-19 19:32:41 +02:00
localStorage.removeItem(key);
2024-07-19 19:23:48 +02:00
};
2024-07-19 20:07:14 +02:00
list = (key: string): string[] => {
2024-07-19 20:50:42 +02:00
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
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)];
};
// stringifiable
2024-07-19 20:50:42 +02:00
storeStringifiable = (key: string, value: any): void => {
const valueString: string = stringify(value);
2024-07-19 20:07:14 +02:00
this.store(key, valueString);
2024-07-19 19:23:48 +02:00
};
2024-07-19 18:26:04 +02:00
2024-07-19 20:07:14 +02:00
restoreStringifiable = (key: string): any | null => {
2024-07-19 20:50:42 +02:00
const valueString: string | null = this.restore(key);
2024-07-19 18:26:04 +02:00
if (!valueString) return null;
2024-07-19 19:23:48 +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-19 19:32:41 +02:00
return pathComponents.join("/");
2024-07-19 19:23:48 +02:00
};
2024-07-19 20:50:42 +02:00
static keyToPathComponents = (key: string): string[] => {
2024-07-19 19:23:48 +02:00
return key.split("/");
};
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-19 20:50:42 +02:00
socketAddress: `${DATA_VERSION}/connection/socket-address`,
2024-07-19 19:23:48 +02:00
// settings
2024-07-19 20:50:42 +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-19 20:50:42 +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-19 20:50:42 +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
};