improve mailboxes

This commit is contained in:
marlon-erler
2024-07-08 23:34:16 +02:00
parent 93658124cc
commit a8186e79be
13 changed files with 260 additions and 187 deletions
+23 -7
View File
@@ -14,14 +14,21 @@ export interface Subscriber {
export class Mailbox implements Subscriber {
// basic data
id = Crypto.randomUUID();
dateLastChecked = new Date();
dateLastUsed = new Date();
get expiryDate() {
const expiryDate = new Date(this.dateLastChecked);
const expiryDate = new Date(this.dateLastUsed);
expiryDate.setDate(expiryDate.getDate() + 7);
return expiryDate;
}
get isExpired() {
if (this.ws) return false;
const today = new Date();
return this.expiryDate < today;
}
// init
constructor(ws: WS) {
this.ws = ws;
@@ -39,6 +46,17 @@ export class Mailbox implements Subscriber {
);
}
confirmConnection(): void {
if (!this.ws) return;
sendWebSocketMessage(
{
connectedMailboxId: this.id,
},
this.ws
);
}
// storage
messages = new Set<string>();
@@ -51,8 +69,9 @@ export class Mailbox implements Subscriber {
set ws(ws: WS) {
this._ws = ws;
this.dateLastChecked = new Date();
this.dateLastUsed = new Date();
this.confirmConnection();
this.sendUnreadMessages();
const channels = subscriptionMap.getChannelList(ws);
@@ -66,14 +85,11 @@ export class Mailbox implements Subscriber {
subscriptionMap.set(this, channelName);
}
unsubscribe(channelName: string): void {
subscriptionMap.delete(this, channelName);
}
// sending
send(message: string): void {
if (this._ws?.readyState == 1) {
this._ws.send(message);
this.dateLastUsed = new Date();
} else {
this.messages.add(message);
}