From fd52b7af0315be08caa92c0da92eedb2ac81e369 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Sun, 21 Jul 2024 15:15:24 +0200 Subject: [PATCH] add mailbox --- bun.lockb | Bin 11806 -> 11788 bytes dist/index.js | 34 ++++++++++++++++++++++++++++-- package.json | 4 ++-- src/Model/connectionModel.ts | 39 ++++++++++++++++++++++++++++++++++- src/Model/storageModel.ts | 1 + 5 files changed, 73 insertions(+), 5 deletions(-) diff --git a/bun.lockb b/bun.lockb index 9f4cce5b645e94a248d30d619182af8ef3726ad8..983033214a7095abe7993ed6d660d973c265fadf 100755 GIT binary patch delta 1194 zcmYLJYe-XJ82-MqX`3xfQ*+MR#d#^+n3qg(sV%dtXla=kW@Hypl#DE+Rf^HNSfYL8 z4{hZ{Dm7G?7fdoOyp@VYD0I9nP ztzgL+qh`D~#%I9rbeV30O>ro(rqndknG<9Dm)&=^Qopg-2O%UvsN~3rtdlu;hfy1a zkUOl+fGYs619|`&n0Pj`P6qUXcr2jZ7sBY?j7~B745srZ0at>r573_13=psg6fD70UXtktcJ;vdrfr@iG48eE^g2(X_kb_hp zsig$Cg{+(|`j=sFxJiTavw6g{e3*RHKGJS_-Fjg&TzQjwRHHX3Hs1)ZyxA zgxJ1a?zdr6(UPB?plH-#;gG|2c7mWl@98-o>B!NjqsV)Oudfvf+PiIP>anRQ>`^oy zGg)W>k#d>0gl(^s*zsWBRc`kY(U#be{%yIMGoDxe^3bBNp?=ZJ>dp|r($>Q{E za!?`024Vmg`*j$aFOigIP#*zetQrZv5q>lfj2abuGzOIzL)5$7U3T~5oVn-RJ3G7gZe`ZRtQuOI zW&UYN?A=tR?mW6Sx#!C|n@rd7;8}6uXoYh5UWIO3b#3aB!yyPE8A9dx#n#e-!aUSX zA>jz}=oPdy*T;2kN z*`N-P*|>sR7XdQHY(Qp%BtQjV6sHGrx;MA}?a0Kb7{`+)Dn6_TPPGiopt&zE4z2km zIg!9!Pyfm3>cm1)Lruf?$Q)6;=PP(nGx_Ef>mh<%q8Balv;s?VvunW2ZswYZMSCEW z;*K2w#%A;YV|_ADJ~s^*>$6S7hJmeg=SGWp^$0Mo3NUtGIXpO9FAW$QI}ePlHw=u; zpe67s-SzeXV|`9wtWPH8V`;$PkfK;Q5dH7 z;iJwBv){OJ%b<6cJuW@teab2MJX3ZCxv4mSjU-YTgl{=kE8C=p;VZG%BmiId#B-g` z&GoYH)fCo{D38gBF(qD)^74c)!aZ1lOa&;xVTkT)jPnrUjj6D@JQ5i@4`T@?&b zWDDn1>g3>~= zwJJ3?O68ZD4sCTwX7>qtKdIHK@Ca!J7e9#M{$VD3@_nP=3LtM_e|~VS%5kZ0P{5d= z^P~OgOvz|t{8*QutRi}yO3nWYjD82x-*#H8g1(>R=v26h8~_*pBk36{4=TI7mIy8f uxel&w@Fb8vNfIn diff --git a/dist/index.js b/dist/index.js index 8e2434b2..36572a90 100644 --- a/dist/index.js +++ b/dist/index.js @@ -393,6 +393,7 @@ socketAddress: [DATA_VERSION, "connection", "socket-address"], reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"], outbox: [DATA_VERSION, "connection", "outbox"], + mailboxes: [DATA_VERSION, "connection", "mailboxes"], // settings username: [DATA_VERSION, "settings", "user-name"], firstDayOfWeek: [DATA_VERSION, "settings", "first-day-of-week"], @@ -1657,7 +1658,6 @@ connect = (address) => { this.udn.connect(address); }; - a; disconnect = () => { this.udn.disconnect(); const reconnectAddressPath = storageKeys.reconnectAddress; @@ -1671,10 +1671,29 @@ this.connectionChangeHandler(); if (this.isConnected == false) return; if (!this.address) return; + this.connectMailbox(); this.storeAddress(this.address); this.sendSubscriptionRequest(); this.sendMessagesInOutbox(); }; + // mailbox + requestNewMailbox = () => { + console.trace("requesting mailbox"); + this.udn.requestMailbox(); + }; + connectMailbox = () => { + if (this.address == void 0) return; + const path = [...storageKeys.mailboxes, this.address]; + const mailboxId = this.storageModel.restore(path); + console.log("connecting mailbox", mailboxId); + if (mailboxId == null) return this.requestNewMailbox(); + this.udn.connectMailbox(mailboxId); + }; + storeMailbox = (mailboxId) => { + if (this.address == void 0) return; + const path = [...storageKeys.mailboxes, this.address]; + this.storageModel.store(path, mailboxId); + }; // subscription addChannel = (channel) => { this.channelsToSubscribe.add(channel); @@ -1685,6 +1704,7 @@ for (const channel of this.channelsToSubscribe) { this.udn.subscribe(channel); } + this.connectMailbox(); }; // outbox getOutboxMessags = () => { @@ -1743,7 +1763,6 @@ this.storageModel.store(addressPath, ""); const reconnectAddressPath = storageKeys.reconnectAddress; this.storageModel.store(reconnectAddressPath, address); - this.storageModel.print(); }; removeAddress = (address) => { const addressPath = this.getAddressPath(address); @@ -1776,6 +1795,17 @@ this.udn.ondisconnect = () => { this.handleConnectionChange(); }; + this.udn.onmailboxcreate = (mailboxId) => { + console.log("Created mailbox", mailboxId); + this.storeMailbox(mailboxId); + this.connectMailbox(); + }; + this.udn.onmailboxdelete = () => { + this.requestNewMailbox(); + }; + this.udn.onmailboxconnect = (mailboxId) => { + console.log(`Using mailbox ${mailboxId}`); + }; const reconnectAddressPath = storageKeys.reconnectAddress; const reconnectAddress = storageModel2.restore(reconnectAddressPath); if (reconnectAddress != null) { diff --git a/package.json b/package.json index 97dfebb5..912faca4 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "dependencies": { "@types/uuid": "^10.0.0", - "bloatless-react": "^1.3.8", + "bloatless-react": "^1.3.9", "esbuild": "^0.23.0", - "udn-frontend": "^1.0.7-beta1", + "udn-frontend": "^1.0.8", "uuid": "^10.0.0" }, "scripts": { diff --git a/src/Model/connectionModel.ts b/src/Model/connectionModel.ts index 4ad77e16..90c79d9c 100644 --- a/src/Model/connectionModel.ts +++ b/src/Model/connectionModel.ts @@ -50,11 +50,34 @@ export default class ConnectionModel { if (this.isConnected == false) return; if (!this.address) return; + this.connectMailbox(); this.storeAddress(this.address); this.sendSubscriptionRequest(); this.sendMessagesInOutbox(); }; + // mailbox + requestNewMailbox = (): void => { + console.trace("requesting mailbox"); + this.udn.requestMailbox(); + }; + + connectMailbox = (): void => { + if (this.address == undefined) return; + const path = [...storageKeys.mailboxes, this.address]; + const mailboxId = this.storageModel.restore(path); + console.log("connecting mailbox", mailboxId) + if (mailboxId == null) return this.requestNewMailbox(); + + this.udn.connectMailbox(mailboxId); + }; + + storeMailbox = (mailboxId: string): void => { + if (this.address == undefined) return; + const path = [...storageKeys.mailboxes, this.address]; + this.storageModel.store(path, mailboxId); + }; + // subscription addChannel = (channel: string): void => { this.channelsToSubscribe.add(channel); @@ -67,6 +90,8 @@ export default class ConnectionModel { for (const channel of this.channelsToSubscribe) { this.udn.subscribe(channel); } + // update mailbox + this.connectMailbox(); }; // outbox @@ -142,7 +167,6 @@ export default class ConnectionModel { // reconnect const reconnectAddressPath: string[] = storageKeys.reconnectAddress; this.storageModel.store(reconnectAddressPath, address); - this.storageModel.print(); }; removeAddress = (address: string): void => { @@ -187,6 +211,19 @@ export default class ConnectionModel { this.handleConnectionChange(); }; + this.udn.onmailboxcreate = (mailboxId: string) => { + console.log("created mailbox", mailboxId); + this.storeMailbox(mailboxId); + this.connectMailbox(); + }; + this.udn.onmailboxdelete = (mailboxId: string) => { + console.log(`mailbox ${mailboxId} deleted`); + this.requestNewMailbox(); + }; + this.udn.onmailboxconnect = (mailboxId: string) => { + console.log(`using mailbox ${mailboxId}`); + }; + // reconnect const reconnectAddressPath: string[] = storageKeys.reconnectAddress; const reconnectAddress: string | null = diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index 368b3a73..2b802bc8 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -133,6 +133,7 @@ export const storageKeys = { socketAddress: [DATA_VERSION, "connection", "socket-address"], reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"], outbox: [DATA_VERSION, "connection", "outbox"], + mailboxes: [DATA_VERSION, "connection", "mailboxes"], // settings username: [DATA_VERSION, "settings", "user-name"],