finish mailbox

This commit is contained in:
marlon-erler
2024-07-09 00:30:57 +02:00
parent 3b9e5038ca
commit 58475ff68b
5 changed files with 45 additions and 20 deletions
BIN
View File
Binary file not shown.
+15 -1
View File
@@ -37,12 +37,26 @@ UDN.subscribe("my-channel");
UDN.unsubscribe("my-channel"); UDN.unsubscribe("my-channel");
// mailbox // mailbox
UDN.onmailbox = (mailboxId: string) => { UDN.onmailboxcreate = (mailboxId: string) => {
UDN.connectMailbox(mailboxId); UDN.connectMailbox(mailboxId);
} }
UDN.onmailboxconnect = (mailboxId: string) => { UDN.onmailboxconnect = (mailboxId: string) => {
UDN.deleteMailbox(mailboxId); UDN.deleteMailbox(mailboxId);
} }
UDN.onmailboxdelete = (mailboxId: string) => {
console.log("mailbox deleted");
}
UDN.requestMailbox(); UDN.requestMailbox();
``` ```
# Changelog
## 1.0.5
- add mailbox support
## 1.0.6
- add error handling with `connect()`
- add `onmailboxdelete` listener
+28 -17
View File
@@ -8,11 +8,12 @@ export interface Message {
deletingMailbox?: string; deletingMailbox?: string;
assignedMailboxId?: string; // sent by server assignedMailboxId?: string; // sent by server
connectedMailboxId?: string; // sent by server connectedMailboxId?: string; // sent by server
deletedMailbox?: string; // sent by server
// subscribing to channel // subscribing to channel
subscribeChannel?: string; subscribeChannel?: string;
unsubscribeChannel?: string; unsubscribeChannel?: string;
subscribed?: boolean; // sent by server to confirm subscription subscribed?: boolean; // sent by server
//sending message //sending message
messageChannel?: string; messageChannel?: string;
@@ -28,6 +29,7 @@ export default class UDNFrontend {
private messageHandler = (data: Message) => {}; private messageHandler = (data: Message) => {};
private mailboxHandler = (mailboxId: string) => {}; private mailboxHandler = (mailboxId: string) => {};
private mailboxConnectionHandler = (mailboxId: string) => {}; private mailboxConnectionHandler = (mailboxId: string) => {};
private mailboxDeleteHandler = (mailboxId: string) => {};
// INIT // INIT
set onconnect(handler: () => void) { set onconnect(handler: () => void) {
@@ -39,12 +41,15 @@ export default class UDNFrontend {
set onmessage(handler: (data: Message) => void) { set onmessage(handler: (data: Message) => void) {
this.messageHandler = handler; this.messageHandler = handler;
} }
set onmailbox(handler: (mailboxId: string) => void) { set onmailboxcreate(handler: (mailboxId: string) => void) {
this.mailboxHandler = handler; this.mailboxHandler = handler;
} }
set onmailboxconnect(handler: (mailboxId: string) => void) { set onmailboxconnect(handler: (mailboxId: string) => void) {
this.mailboxConnectionHandler = handler; this.mailboxConnectionHandler = handler;
} }
set onmailboxdelete(handler: (mailboxId: string) => void) {
this.mailboxDeleteHandler = handler;
}
// UTILITY METHODS // UTILITY METHODS
private send(messageObject: Message): boolean { private send(messageObject: Message): boolean {
@@ -58,23 +63,29 @@ export default class UDNFrontend {
// PUBLIC METHODS // PUBLIC METHODS
// connection // connection
connect(address: string): void { connect(address: string): void {
this.disconnect(); try {
this.disconnect();
this.ws = new WebSocket(address); this.ws = new WebSocket(address);
this.ws.addEventListener("open", this.connectionHandler); this.ws.addEventListener("open", this.connectionHandler);
this.ws.addEventListener("close", this.disconnectionHandler); this.ws.addEventListener("close", this.disconnectionHandler);
this.ws.addEventListener("message", (message) => { this.ws.addEventListener("message", (message) => {
const dataString = message.data.toString(); const dataString = message.data.toString();
const data = JSON.parse(dataString); const data = JSON.parse(dataString);
if (data.assignedMailboxId) { if (data.assignedMailboxId) {
return this.mailboxHandler(data.assignedMailboxId); return this.mailboxHandler(data.assignedMailboxId);
} else if (data.connectedMailboxId) { } else if (data.connectedMailboxId) {
return this.mailboxConnectionHandler(data.assignedMailboxId); return this.mailboxConnectionHandler(data.connectedMailboxId);
} else { } else if (data.deletedMailbox) {
this.messageHandler(data); return this.mailboxDeleteHandler(data.deletedMailbox);
} } else {
}); this.messageHandler(data);
}
});
} catch (error) {
console.error(error);
}
} }
disconnect(): void { disconnect(): void {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "udn-frontend", "name": "udn-frontend",
"version": "1.0.4", "version": "1.0.6",
"description": "TypeScript library for UDN apps", "description": "TypeScript library for UDN apps",
"main": "index.ts", "main": "index.ts",
"scripts": { "scripts": {
+1 -1
View File
@@ -2,7 +2,7 @@
"dependencies": { "dependencies": {
"bloatless-react": "1.2.5-beta1", "bloatless-react": "1.2.5-beta1",
"esbuild": "latest", "esbuild": "latest",
"udn-frontend": "^1.0.4" "udn-frontend": "^1.0.6"
}, },
"scripts": { "scripts": {
"build": "esbuild src/index.tsx --bundle --outdir=dist" "build": "esbuild src/index.tsx --bundle --outdir=dist"