improve mailboxes
This commit is contained in:
+25
-6
@@ -4,8 +4,10 @@ export interface Message {
|
||||
|
||||
// mailbox
|
||||
requestingMailboxSetup?: boolean;
|
||||
assignedMailboxId?: string; // sent by server
|
||||
requestedMailbox?: string;
|
||||
deletingMailbox?: string;
|
||||
assignedMailboxId?: string; // sent by server
|
||||
connectedMailboxId?: string; // sent by server
|
||||
|
||||
// subscribing to channel
|
||||
subscribeChannel?: string;
|
||||
@@ -20,13 +22,14 @@ export interface Message {
|
||||
export default class UDNFrontend {
|
||||
ws: WebSocket | undefined;
|
||||
|
||||
// handlers
|
||||
// HANDLERS
|
||||
private connectionHandler = () => {};
|
||||
private disconnectionHandler = () => {};
|
||||
private messageHandler = (data: Message) => {};
|
||||
private mailboxHandler = (mailboxId: string) => {};
|
||||
private mailboxConnectionHandler = (mailboxId: string) => {};
|
||||
|
||||
// init
|
||||
// INIT
|
||||
set onconnect(handler: () => void) {
|
||||
this.connectionHandler = handler;
|
||||
}
|
||||
@@ -39,8 +42,11 @@ export default class UDNFrontend {
|
||||
set onmailbox(handler: (mailboxId: string) => void) {
|
||||
this.mailboxHandler = handler;
|
||||
}
|
||||
set onmailboxconnect(handler: (mailboxId: string) => void) {
|
||||
this.mailboxConnectionHandler = handler;
|
||||
}
|
||||
|
||||
// utility methods
|
||||
// UTILITY METHODS
|
||||
private send(messageObject: Message): boolean {
|
||||
if (this.ws == undefined) return false;
|
||||
|
||||
@@ -49,7 +55,8 @@ export default class UDNFrontend {
|
||||
return true;
|
||||
}
|
||||
|
||||
// public methods
|
||||
// PUBLIC METHODS
|
||||
// connection
|
||||
connect(address: string): void {
|
||||
this.disconnect();
|
||||
|
||||
@@ -62,6 +69,8 @@ export default class UDNFrontend {
|
||||
|
||||
if (data.assignedMailboxId) {
|
||||
return this.mailboxHandler(data.assignedMailboxId);
|
||||
} else if (data.connectedMailboxId) {
|
||||
return this.mailboxConnectionHandler(data.assignedMailboxId);
|
||||
} else {
|
||||
this.messageHandler(data);
|
||||
}
|
||||
@@ -72,6 +81,7 @@ export default class UDNFrontend {
|
||||
this.ws?.close();
|
||||
}
|
||||
|
||||
// message
|
||||
sendMessage(channel: string, body: string): boolean {
|
||||
const messageObject = {
|
||||
messageChannel: channel,
|
||||
@@ -80,6 +90,7 @@ export default class UDNFrontend {
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
// subscription
|
||||
subscribe(channel: string): boolean {
|
||||
const messageObject = {
|
||||
subscribeChannel: channel,
|
||||
@@ -94,6 +105,7 @@ export default class UDNFrontend {
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
// mailbox
|
||||
requestMailbox(): boolean {
|
||||
const messageObject = {
|
||||
requestingMailboxSetup: true,
|
||||
@@ -101,10 +113,17 @@ export default class UDNFrontend {
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
accessMailbox(mailboxId: string): boolean {
|
||||
connectMailbox(mailboxId: string): boolean {
|
||||
const messageObject = {
|
||||
requestedMailbox: mailboxId,
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
deleteMailbox(mailboxId: string): boolean {
|
||||
const messageObject = {
|
||||
deletingMailbox: mailboxId,
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user