add history of previous connections, remodel storageModel to use backslash as path separator
This commit is contained in:
+18
-15
@@ -17,28 +17,28 @@ export class ChatModel {
|
||||
outbox = new Set<ChatMessage>();
|
||||
|
||||
// paths
|
||||
get infoPath(): string {
|
||||
get infoPath(): string[] {
|
||||
return storageKeys.chatInfo(this.id);
|
||||
}
|
||||
|
||||
get messageDirPath(): string {
|
||||
get messageDirPath(): string[] {
|
||||
return storageKeys.chatMessages(this.id);
|
||||
}
|
||||
|
||||
get objectDirPath(): string {
|
||||
get objectDirPath(): string[] {
|
||||
return storageKeys.chatObjects(this.id);
|
||||
}
|
||||
|
||||
get outboxDirPath(): string {
|
||||
get outboxDirPath(): string[] {
|
||||
return storageKeys.chatOutbox(this.id);
|
||||
}
|
||||
|
||||
getMessagePath(id: string): string {
|
||||
return StorageModel.join(this.messageDirPath, id);
|
||||
getMessagePath(id: string): string[] {
|
||||
return [...this.messageDirPath, id];
|
||||
}
|
||||
|
||||
getObjectPath(id: string): string {
|
||||
return StorageModel.join(this.objectDirPath, id);
|
||||
getObjectPath(id: string): string[] {
|
||||
return [...this.objectDirPath, id];
|
||||
}
|
||||
|
||||
// set
|
||||
@@ -61,13 +61,13 @@ export class ChatModel {
|
||||
if (!this.messages.has(message)) {
|
||||
this.messages.add(message);
|
||||
}
|
||||
const messagePath: string = this.getMessagePath(message.id);
|
||||
const messagePath: string[] = this.getMessagePath(message.id);
|
||||
this.storageModel.storeStringifiable(messagePath, message);
|
||||
}
|
||||
|
||||
addObject(object: ChatObject): void {
|
||||
this.objects.set(object.id, object);
|
||||
const objectPath: string = this.getObjectPath(object.id);
|
||||
const objectPath: string[] = this.getObjectPath(object.id);
|
||||
this.storageModel.storeStringifiable(objectPath, object);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ export class ChatModel {
|
||||
if (!Array.isArray(messageIds)) return;
|
||||
|
||||
for (const messageId of messageIds) {
|
||||
const messagePath: string = this.getMessagePath(messageId);
|
||||
const messagePath: string[] = this.getMessagePath(messageId);
|
||||
const message: any = this.storageModel.restoreStringifiable(messagePath);
|
||||
if (checkIsValidObject(message) == false) return;
|
||||
|
||||
@@ -95,8 +95,8 @@ export class ChatModel {
|
||||
if (!Array.isArray(objectIds)) return;
|
||||
|
||||
for (const objectId of objectIds) {
|
||||
const objectPath: string = this.getObjectPath(objectId);
|
||||
const object: any = this.storageModel.restoreStringifiable(objectPath);
|
||||
const objectPathComponents: string[] = this.getObjectPath(objectId);
|
||||
const object: any = this.storageModel.restoreStringifiable(objectPathComponents);
|
||||
if (checkIsValidObject(object) == false) return;
|
||||
if (object.id != objectId) return;
|
||||
|
||||
@@ -116,12 +116,15 @@ export class ChatModel {
|
||||
}
|
||||
|
||||
// creation methods
|
||||
export function createChat(storageModel: StorageModel, primaryChannel: string): ChatModel {
|
||||
export function createChat(
|
||||
storageModel: StorageModel,
|
||||
primaryChannel: string
|
||||
): ChatModel {
|
||||
const id: string = v4();
|
||||
|
||||
const chatModel = new ChatModel(storageModel, id);
|
||||
chatModel.setPrimaryChannel(primaryChannel);
|
||||
|
||||
|
||||
return chatModel;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
// this file is responsible for managing UDN connections.
|
||||
|
||||
import StorageModel, { storageKeys } from "./storageModel";
|
||||
import UDNFrontend, { Message } from "udn-frontend";
|
||||
|
||||
import { ChatMessage } from "./chatModel";
|
||||
import { stringify } from "./Utility/utility";
|
||||
|
||||
export default class ConnectionModel {
|
||||
// basic
|
||||
udn: UDNFrontend;
|
||||
storageModel: StorageModel;
|
||||
|
||||
// data
|
||||
get isConnected(): boolean {
|
||||
@@ -29,6 +30,11 @@ export default class ConnectionModel {
|
||||
|
||||
handleConnectionChange = (): void => {
|
||||
console.log("connection status:", this.isConnected, this.address);
|
||||
if (this.isConnected == false) return;
|
||||
|
||||
if (this.address) {
|
||||
this.storeAddress(this.address);
|
||||
}
|
||||
};
|
||||
|
||||
// messaging
|
||||
@@ -37,28 +43,52 @@ export default class ConnectionModel {
|
||||
return this.udn.sendMessage(chatMessage.channel, stringifiedBody);
|
||||
};
|
||||
|
||||
// storage
|
||||
private getAddressPath = (address: string): string[] => {
|
||||
const dirPath = storageKeys.previousAddresses;
|
||||
return [...dirPath, address];
|
||||
};
|
||||
|
||||
storeAddress = (address: string): void => {
|
||||
const addressPath = this.getAddressPath(address);
|
||||
this.storageModel.store(addressPath, "");
|
||||
};
|
||||
|
||||
removeAddress = (address: string): void => {
|
||||
const addressPath = this.getAddressPath(address);
|
||||
this.storageModel.remove(addressPath);
|
||||
};
|
||||
|
||||
getAddresses = (): string[] => {
|
||||
const dirPath = storageKeys.previousAddresses;
|
||||
return this.storageModel.list(dirPath);
|
||||
};
|
||||
|
||||
// setup
|
||||
constructor(configuration: ConnectionModelConfiguration) {
|
||||
// create frontend
|
||||
this.udn = new UDNFrontend();
|
||||
this.storageModel = configuration.storageModel;
|
||||
|
||||
// setup handlers
|
||||
this.udn.onmessage = (data: Message) => {
|
||||
configuration.messageHandler(data);
|
||||
configuration.messageHandler(data);
|
||||
};
|
||||
this.udn.onconnect = () => {
|
||||
this.handleConnectionChange();
|
||||
configuration.connectionChangeHandler();
|
||||
this.handleConnectionChange();
|
||||
configuration.connectionChangeHandler();
|
||||
};
|
||||
this.udn.ondisconnect = () => {
|
||||
this.handleConnectionChange();
|
||||
configuration.connectionChangeHandler();
|
||||
this.handleConnectionChange();
|
||||
configuration.connectionChangeHandler();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// types
|
||||
export interface ConnectionModelConfiguration {
|
||||
storageModel: StorageModel;
|
||||
|
||||
connectionChangeHandler: () => void;
|
||||
messageHandler: (data: Message) => void;
|
||||
}
|
||||
|
||||
+26
-24
@@ -4,27 +4,29 @@ import { parse, stringify } from "./Utility/utility";
|
||||
|
||||
import { DATA_VERSION } from "./Utility/typeSafety";
|
||||
|
||||
const PATH_COMPONENT_SEPARATOR = "\\";
|
||||
|
||||
export default class StorageModel {
|
||||
storageEntryTree: StorageEntry = {};
|
||||
|
||||
// basic
|
||||
store = (key: string, value: string): void => {
|
||||
store = (pathComponents: string[], value: string): void => {
|
||||
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
|
||||
localStorage.setItem(key, value);
|
||||
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
|
||||
this.updateTree(...pathComponents);
|
||||
};
|
||||
|
||||
restore = (key: string): string | null => {
|
||||
restore = (pathComponents: string[]): string | null => {
|
||||
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
|
||||
return localStorage.getItem(key);
|
||||
};
|
||||
|
||||
remove = (key: string): void => {
|
||||
remove = (pathComponents: string[]): void => {
|
||||
const key: string = StorageModel.pathComponentsToKey(...pathComponents);
|
||||
localStorage.removeItem(key);
|
||||
};
|
||||
|
||||
list = (key: string): string[] => {
|
||||
const pathComponents: string[] = StorageModel.keyToPathComponents(key);
|
||||
|
||||
list = (pathComponents: string[]): string[] => {
|
||||
let currentParent: StorageEntry = this.storageEntryTree;
|
||||
for (const component of pathComponents) {
|
||||
const nextParent: StorageEntry | undefined = currentParent[component];
|
||||
@@ -36,13 +38,13 @@ export default class StorageModel {
|
||||
};
|
||||
|
||||
// stringifiable
|
||||
storeStringifiable = (key: string, value: any): void => {
|
||||
storeStringifiable = (pathComponents: string[], value: any): void => {
|
||||
const valueString: string = stringify(value);
|
||||
this.store(key, valueString);
|
||||
this.store(pathComponents, valueString);
|
||||
};
|
||||
|
||||
restoreStringifiable = (key: string): any | null => {
|
||||
const valueString: string | null = this.restore(key);
|
||||
restoreStringifiable = (pathComponents: string[]): any | null => {
|
||||
const valueString: string | null = this.restore(pathComponents);
|
||||
if (!valueString) return null;
|
||||
|
||||
return parse(valueString);
|
||||
@@ -69,11 +71,11 @@ export default class StorageModel {
|
||||
};
|
||||
|
||||
static pathComponentsToKey = (...pathComponents: string[]): string => {
|
||||
return pathComponents.join("/");
|
||||
return pathComponents.join(PATH_COMPONENT_SEPARATOR);
|
||||
};
|
||||
|
||||
static keyToPathComponents = (key: string): string[] => {
|
||||
return key.split("/");
|
||||
return key.split(PATH_COMPONENT_SEPARATOR);
|
||||
};
|
||||
|
||||
static join = (...items: string[]): string => {
|
||||
@@ -92,21 +94,21 @@ export type StorageEntry = { [key: string]: StorageEntry };
|
||||
// keys
|
||||
export const storageKeys = {
|
||||
// connection
|
||||
socketAddress: `${DATA_VERSION}/connection/socket-address`,
|
||||
socketAddress: [DATA_VERSION, "connection", "socket-address"],
|
||||
|
||||
// settings
|
||||
username: `${DATA_VERSION}/settings/user-name`,
|
||||
firstDayOfWeek: `${DATA_VERSION}/settings/first-day-of-week`,
|
||||
username: [DATA_VERSION, "settings", "user-name"],
|
||||
firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"],
|
||||
|
||||
// history
|
||||
previousAddresses: `${DATA_VERSION}/history/previous-addresses`,
|
||||
previousObjectCategories: `${DATA_VERSION}/history/object-categories`,
|
||||
previousObjectStatuses: `${DATA_VERSION}/history/object-statuses`,
|
||||
previousObjectFilters: `${DATA_VERSION}/history/object-filters`,
|
||||
previousAddresses: [DATA_VERSION, "history", "previous-addresses"],
|
||||
previousObjectCategories: [DATA_VERSION, "history", "object-categories"],
|
||||
previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"],
|
||||
previousObjectFilters: [DATA_VERSION, "history", "object-filters"],
|
||||
|
||||
// chat etc
|
||||
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`,
|
||||
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"],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user