From 58475ff68b4bce18bb04cce30e2a6c78c6abce42 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Tue, 9 Jul 2024 00:30:57 +0200 Subject: [PATCH] finish mailbox --- static/bun.lockb | Bin 11434 -> 11434 bytes static/node_modules/udn-frontend/README.md | 16 ++++++- static/node_modules/udn-frontend/index.ts | 45 +++++++++++------- static/node_modules/udn-frontend/package.json | 2 +- static/package.json | 2 +- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/static/bun.lockb b/static/bun.lockb index 7eb7c7ee8b202b4e6ee09aa651311189b23dbc48..a31775bf065e47194cf1901b400b01ee2fa2b4b9 100755 GIT binary patch delta 144 zcmV;B0B`@QS*lr(E+BYFf*JW93+#L)!$!oBi*aFvx1ueDDC97NFT^_z@f)#DS^@zE zlTiXEv(N(l2tZpzK-_rKRDr>C4q=|GElp5}2axUqV1+#Ahx?*iR6EU^pk^ delta 144 zcmV;B0B`@QS*lr(E+C;?ZUr^HA(W*A#FUfL8VP5w4@Mu?YF-xaAM_*xFPO1TS^@zC zlTiXEv(N(l2tekTF@5ZM60pg9*`j8leLS%|bS{&CL^?875bF*wrREcZhk|Rm$a6~C y>v6`Im}jm+{4{KM+k!#iF|av*ALX+R3@8%^0ssI200029JRfBP0W`CsA_xi>oH=U% diff --git a/static/node_modules/udn-frontend/README.md b/static/node_modules/udn-frontend/README.md index 9562c99..307d9ff 100644 --- a/static/node_modules/udn-frontend/README.md +++ b/static/node_modules/udn-frontend/README.md @@ -37,12 +37,26 @@ UDN.subscribe("my-channel"); UDN.unsubscribe("my-channel"); // mailbox -UDN.onmailbox = (mailboxId: string) => { +UDN.onmailboxcreate = (mailboxId: string) => { UDN.connectMailbox(mailboxId); } UDN.onmailboxconnect = (mailboxId: string) => { UDN.deleteMailbox(mailboxId); } +UDN.onmailboxdelete = (mailboxId: string) => { + console.log("mailbox deleted"); +} UDN.requestMailbox(); ``` + +# Changelog + +## 1.0.5 + +- add mailbox support + +## 1.0.6 + +- add error handling with `connect()` +- add `onmailboxdelete` listener \ No newline at end of file diff --git a/static/node_modules/udn-frontend/index.ts b/static/node_modules/udn-frontend/index.ts index 3b729f6..8bf88cf 100644 --- a/static/node_modules/udn-frontend/index.ts +++ b/static/node_modules/udn-frontend/index.ts @@ -8,11 +8,12 @@ export interface Message { deletingMailbox?: string; assignedMailboxId?: string; // sent by server connectedMailboxId?: string; // sent by server + deletedMailbox?: string; // sent by server // subscribing to channel subscribeChannel?: string; unsubscribeChannel?: string; - subscribed?: boolean; // sent by server to confirm subscription + subscribed?: boolean; // sent by server //sending message messageChannel?: string; @@ -28,6 +29,7 @@ export default class UDNFrontend { private messageHandler = (data: Message) => {}; private mailboxHandler = (mailboxId: string) => {}; private mailboxConnectionHandler = (mailboxId: string) => {}; + private mailboxDeleteHandler = (mailboxId: string) => {}; // INIT set onconnect(handler: () => void) { @@ -39,12 +41,15 @@ export default class UDNFrontend { set onmessage(handler: (data: Message) => void) { this.messageHandler = handler; } - set onmailbox(handler: (mailboxId: string) => void) { + set onmailboxcreate(handler: (mailboxId: string) => void) { this.mailboxHandler = handler; } set onmailboxconnect(handler: (mailboxId: string) => void) { this.mailboxConnectionHandler = handler; } + set onmailboxdelete(handler: (mailboxId: string) => void) { + this.mailboxDeleteHandler = handler; + } // UTILITY METHODS private send(messageObject: Message): boolean { @@ -58,23 +63,29 @@ export default class UDNFrontend { // PUBLIC METHODS // connection connect(address: string): void { - this.disconnect(); + try { + this.disconnect(); - this.ws = new WebSocket(address); - this.ws.addEventListener("open", this.connectionHandler); - this.ws.addEventListener("close", this.disconnectionHandler); - this.ws.addEventListener("message", (message) => { - const dataString = message.data.toString(); - const data = JSON.parse(dataString); + this.ws = new WebSocket(address); + this.ws.addEventListener("open", this.connectionHandler); + this.ws.addEventListener("close", this.disconnectionHandler); + this.ws.addEventListener("message", (message) => { + const dataString = message.data.toString(); + const data = JSON.parse(dataString); - if (data.assignedMailboxId) { - return this.mailboxHandler(data.assignedMailboxId); - } else if (data.connectedMailboxId) { - return this.mailboxConnectionHandler(data.assignedMailboxId); - } else { - this.messageHandler(data); - } - }); + if (data.assignedMailboxId) { + return this.mailboxHandler(data.assignedMailboxId); + } else if (data.connectedMailboxId) { + return this.mailboxConnectionHandler(data.connectedMailboxId); + } else if (data.deletedMailbox) { + return this.mailboxDeleteHandler(data.deletedMailbox); + } else { + this.messageHandler(data); + } + }); + } catch (error) { + console.error(error); + } } disconnect(): void { diff --git a/static/node_modules/udn-frontend/package.json b/static/node_modules/udn-frontend/package.json index 91637c8..2e2d9e7 100644 --- a/static/node_modules/udn-frontend/package.json +++ b/static/node_modules/udn-frontend/package.json @@ -1,6 +1,6 @@ { "name": "udn-frontend", - "version": "1.0.4", + "version": "1.0.6", "description": "TypeScript library for UDN apps", "main": "index.ts", "scripts": { diff --git a/static/package.json b/static/package.json index 3da7530..459557d 100644 --- a/static/package.json +++ b/static/package.json @@ -2,7 +2,7 @@ "dependencies": { "bloatless-react": "1.2.5-beta1", "esbuild": "latest", - "udn-frontend": "^1.0.4" + "udn-frontend": "^1.0.6" }, "scripts": { "build": "esbuild src/index.tsx --bundle --outdir=dist"