use key instead of pathComponents
This commit is contained in:
@@ -1,7 +1,22 @@
|
||||
// this file is responsible for managing chats and chat messages. objects are delegated to objectModel.ts.
|
||||
|
||||
import StorageModel, { storageKeys } from "./storageModel";
|
||||
|
||||
import { createTimestamp } from "../utility";
|
||||
|
||||
export class ChatModel {
|
||||
info: ChatInfo;
|
||||
|
||||
messages: ChatMessage[];
|
||||
objects: ChatObject[];
|
||||
outbox: ChatMessage[];
|
||||
|
||||
constructor(storageModel: StorageModel, chatId: string) {
|
||||
const infoPath = storageKeys.chatInfo(chatId)
|
||||
const info = storageModel.restoreStringifiable(infoPath);
|
||||
}
|
||||
}
|
||||
|
||||
// creation methods
|
||||
export function createChatMessage(channel: string, sender: string, body: string): ChatMessage {
|
||||
return {
|
||||
@@ -15,6 +30,14 @@ export function createChatMessage(channel: string, sender: string, body: string)
|
||||
}
|
||||
|
||||
// types
|
||||
export interface ChatInfo {
|
||||
primaryChannel: string;
|
||||
secondaryChannels: string[];
|
||||
encryptionKey: string;
|
||||
|
||||
hasUnreadMessages: boolean;
|
||||
}
|
||||
|
||||
export interface ChatMessage {
|
||||
dataVersion: "v2";
|
||||
|
||||
|
||||
+11
-11
@@ -6,23 +6,23 @@ export default class StorageModel {
|
||||
storageEntryTree: StorageEntry = {};
|
||||
|
||||
// basic
|
||||
store = (pathComponents: string[], value: string): void => {
|
||||
const key = this.pathComponentsToKey(...pathComponents);
|
||||
store = (key: string, value: string): void => {
|
||||
localStorage.setItem(key, value);
|
||||
const pathComponents = this.keyToPathComponents(key);
|
||||
this.updateTree(...pathComponents);
|
||||
};
|
||||
|
||||
restore = (pathComponents: string[]): string | null => {
|
||||
const key = this.pathComponentsToKey(...pathComponents);
|
||||
restore = (key: string): string | null => {
|
||||
return localStorage.getItem(key);
|
||||
};
|
||||
|
||||
remove = (pathComponents: string[]): void => {
|
||||
const key = this.pathComponentsToKey(...pathComponents);
|
||||
remove = (key: string): void => {
|
||||
localStorage.removeItem(key);
|
||||
};
|
||||
|
||||
list = (...pathComponents: string[]): string[] => {
|
||||
list = (key: string): string[] => {
|
||||
const pathComponents = this.keyToPathComponents(key);
|
||||
|
||||
let currentParent = this.storageEntryTree;
|
||||
for (const component of pathComponents) {
|
||||
currentParent = currentParent[component];
|
||||
@@ -32,13 +32,13 @@ export default class StorageModel {
|
||||
};
|
||||
|
||||
// stringifiable
|
||||
storeStringifiable = (pathComponents: string[], value: any[]): void => {
|
||||
storeStringifiable = (key: string, value: any[]): void => {
|
||||
const valueString = stringify(value);
|
||||
this.store(pathComponents, valueString);
|
||||
this.store(key, valueString);
|
||||
};
|
||||
|
||||
restoreStringifiable = (pathComponents: string[]): any | null => {
|
||||
const valueString = this.restore(pathComponents);
|
||||
restoreStringifiable = (key: string): any | null => {
|
||||
const valueString = this.restore(key);
|
||||
if (!valueString) return null;
|
||||
|
||||
return parse(valueString);
|
||||
|
||||
Reference in New Issue
Block a user