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.
|
// 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";
|
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
|
// creation methods
|
||||||
export function createChatMessage(channel: string, sender: string, body: string): ChatMessage {
|
export function createChatMessage(channel: string, sender: string, body: string): ChatMessage {
|
||||||
return {
|
return {
|
||||||
@@ -15,6 +30,14 @@ export function createChatMessage(channel: string, sender: string, body: string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// types
|
// types
|
||||||
|
export interface ChatInfo {
|
||||||
|
primaryChannel: string;
|
||||||
|
secondaryChannels: string[];
|
||||||
|
encryptionKey: string;
|
||||||
|
|
||||||
|
hasUnreadMessages: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ChatMessage {
|
export interface ChatMessage {
|
||||||
dataVersion: "v2";
|
dataVersion: "v2";
|
||||||
|
|
||||||
|
|||||||
+11
-11
@@ -6,23 +6,23 @@ export default class StorageModel {
|
|||||||
storageEntryTree: StorageEntry = {};
|
storageEntryTree: StorageEntry = {};
|
||||||
|
|
||||||
// basic
|
// basic
|
||||||
store = (pathComponents: string[], value: string): void => {
|
store = (key: string, value: string): void => {
|
||||||
const key = this.pathComponentsToKey(...pathComponents);
|
|
||||||
localStorage.setItem(key, value);
|
localStorage.setItem(key, value);
|
||||||
|
const pathComponents = this.keyToPathComponents(key);
|
||||||
this.updateTree(...pathComponents);
|
this.updateTree(...pathComponents);
|
||||||
};
|
};
|
||||||
|
|
||||||
restore = (pathComponents: string[]): string | null => {
|
restore = (key: string): string | null => {
|
||||||
const key = this.pathComponentsToKey(...pathComponents);
|
|
||||||
return localStorage.getItem(key);
|
return localStorage.getItem(key);
|
||||||
};
|
};
|
||||||
|
|
||||||
remove = (pathComponents: string[]): void => {
|
remove = (key: string): void => {
|
||||||
const key = this.pathComponentsToKey(...pathComponents);
|
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
};
|
};
|
||||||
|
|
||||||
list = (...pathComponents: string[]): string[] => {
|
list = (key: string): string[] => {
|
||||||
|
const pathComponents = this.keyToPathComponents(key);
|
||||||
|
|
||||||
let currentParent = this.storageEntryTree;
|
let currentParent = this.storageEntryTree;
|
||||||
for (const component of pathComponents) {
|
for (const component of pathComponents) {
|
||||||
currentParent = currentParent[component];
|
currentParent = currentParent[component];
|
||||||
@@ -32,13 +32,13 @@ export default class StorageModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// stringifiable
|
// stringifiable
|
||||||
storeStringifiable = (pathComponents: string[], value: any[]): void => {
|
storeStringifiable = (key: string, value: any[]): void => {
|
||||||
const valueString = stringify(value);
|
const valueString = stringify(value);
|
||||||
this.store(pathComponents, valueString);
|
this.store(key, valueString);
|
||||||
};
|
};
|
||||||
|
|
||||||
restoreStringifiable = (pathComponents: string[]): any | null => {
|
restoreStringifiable = (key: string): any | null => {
|
||||||
const valueString = this.restore(pathComponents);
|
const valueString = this.restore(key);
|
||||||
if (!valueString) return null;
|
if (!valueString) return null;
|
||||||
|
|
||||||
return parse(valueString);
|
return parse(valueString);
|
||||||
|
|||||||
Reference in New Issue
Block a user