implement fake directories for storageModel
This commit is contained in:
+78
-28
@@ -2,43 +2,93 @@
|
||||
|
||||
import { parse, stringify } from "../utility";
|
||||
|
||||
// keys
|
||||
export enum storageKeys {
|
||||
// current
|
||||
socketAddress = "v2-socket-address",
|
||||
export default class StorageModel {
|
||||
storageEntryTree: StorageEntry = {}; // basic
|
||||
store = (directoryNames: string[], key: string, value: string): void => {
|
||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
||||
localStorage.setItem(unifiedPath, value);
|
||||
this.updateTree(...directoryNames, key);
|
||||
};
|
||||
|
||||
// settings
|
||||
userName = "v2-user-name",
|
||||
firstDayOfWeek = "v2-first-day-of-week",
|
||||
restore = (directoryNames: string[], key: string): string | null => {
|
||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
||||
return localStorage.getItem(unifiedPath);
|
||||
};
|
||||
|
||||
// history
|
||||
previousAddresses = "v2-previous-addresses",
|
||||
previousObjectCategories = "v2-object-categories",
|
||||
previousObjectStatuses = "v2-object-statuses",
|
||||
previousObjectFilters = "v2-object-filters",
|
||||
}
|
||||
remove = (directoryNames: string[], key: string): void => {
|
||||
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
||||
localStorage.removeItem(unifiedPath);
|
||||
};
|
||||
|
||||
// basic
|
||||
export function store(key: string, value: string): void {
|
||||
localStorage.setItem(key, value);
|
||||
}
|
||||
list = (...directoryNames: string[]): string[] => {
|
||||
let currentParent = this.storageEntryTree;
|
||||
for (const directoryName of directoryNames) {
|
||||
currentParent = currentParent[directoryName];
|
||||
}
|
||||
|
||||
export function restore(key: string): string | null {
|
||||
return localStorage.getItem(key);
|
||||
}
|
||||
return [...Object.keys(currentParent)];
|
||||
};
|
||||
|
||||
// array
|
||||
export function storeArray(key: string, value: any[]): void {
|
||||
// array
|
||||
storeArray = (directoryNames: string[], key: string, value: any[]): void => {
|
||||
const valueString = stringify(value);
|
||||
store(key, valueString);
|
||||
}
|
||||
this.store(directoryNames, key, valueString);
|
||||
};
|
||||
|
||||
export function restoreArray(key: string): any[] | null {
|
||||
const valueString = restore(key);
|
||||
restoreArray = (directoryNames: string[], key: string): any[] | null => {
|
||||
const valueString = this.restore(directoryNames, key);
|
||||
if (!valueString) return null;
|
||||
|
||||
|
||||
const parsed = parse(valueString);
|
||||
if (!Array.isArray(parsed)) return null;
|
||||
|
||||
return parsed;
|
||||
}
|
||||
};
|
||||
|
||||
// init
|
||||
constructor() {
|
||||
for (const key of Object.keys(localStorage)) {
|
||||
const directoryNames = this.keyToDirectoryNames(key);
|
||||
this.updateTree(...directoryNames);
|
||||
}
|
||||
}
|
||||
|
||||
// utility
|
||||
updateTree = (...pathParts: string[]) => {
|
||||
let currentParent = this.storageEntryTree;
|
||||
for (const pathPart of pathParts) {
|
||||
if (!currentParent[pathPart]) {
|
||||
currentParent[pathPart] = {};
|
||||
}
|
||||
|
||||
currentParent = currentParent[pathPart];
|
||||
}
|
||||
};
|
||||
|
||||
directoryNamesToKey = (...directoryNames: string[]): string => {
|
||||
return directoryNames.join("/");
|
||||
};
|
||||
|
||||
keyToDirectoryNames = (key: string): string[] => {
|
||||
return key.split("/");
|
||||
};
|
||||
}
|
||||
|
||||
// types
|
||||
export type StorageEntry = { [key: string]: StorageEntry };
|
||||
|
||||
// keys
|
||||
export enum storageKeys {
|
||||
// current
|
||||
socketAddress = "v2-socket-address",
|
||||
|
||||
// settings
|
||||
userName = "v2-user-name",
|
||||
firstDayOfWeek = "v2-first-day-of-week",
|
||||
|
||||
// history
|
||||
previousAddresses = "v2-previous-addresses",
|
||||
previousObjectCategories = "v2-object-categories",
|
||||
previousObjectStatuses = "v2-object-statuses",
|
||||
previousObjectFilters = "v2-object-filters",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user