define mailbox
This commit is contained in:
@@ -1,9 +1,46 @@
|
|||||||
|
import Crypto from "crypto";
|
||||||
import { WS } from "./index";
|
import { WS } from "./index";
|
||||||
|
|
||||||
export interface Subscriber {
|
export interface Subscriber {
|
||||||
send: (message: string) => void;
|
send: (message: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class Mailbox implements Subscriber {
|
||||||
|
// basic
|
||||||
|
id = Crypto.randomUUID();
|
||||||
|
messages = new Set<string>();
|
||||||
|
|
||||||
|
// websocket
|
||||||
|
_ws: WS | undefined;
|
||||||
|
|
||||||
|
get ws(): WS | undefined {
|
||||||
|
return this._ws;
|
||||||
|
}
|
||||||
|
|
||||||
|
set ws(ws: WS) {
|
||||||
|
this._ws = ws;
|
||||||
|
this.sendUnreadMessages();
|
||||||
|
}
|
||||||
|
|
||||||
|
// sending
|
||||||
|
send(message) {
|
||||||
|
if (this._ws) {
|
||||||
|
this._ws.send(message);
|
||||||
|
} else {
|
||||||
|
this.messages.add(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendUnreadMessages() {
|
||||||
|
this.messages.forEach((message) => {
|
||||||
|
if (!this._ws) return;
|
||||||
|
|
||||||
|
this._ws.send(message);
|
||||||
|
this.messages.delete(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class SubscriptionMap {
|
export class SubscriptionMap {
|
||||||
channelsPerSubscriber = new Map<Subscriber, Set<string>>();
|
channelsPerSubscriber = new Map<Subscriber, Set<string>>();
|
||||||
subscribersPerChannel = new Map<string, Set<Subscriber>>();
|
subscribersPerChannel = new Map<string, Set<Subscriber>>();
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
import {
|
import { Subscriber, SubscriptionMap } from "./subscriptionHandler";
|
||||||
Subscriber,
|
|
||||||
SubscriptionMap,
|
|
||||||
} from "./subscriptionHandler";
|
|
||||||
import {
|
import {
|
||||||
doIfIsString,
|
doIfIsString,
|
||||||
parseMessage,
|
parseMessage,
|
||||||
@@ -23,11 +20,13 @@ export interface WebSocketMessage {
|
|||||||
|
|
||||||
// mailbox
|
// mailbox
|
||||||
requestingMailboxSetup?: boolean;
|
requestingMailboxSetup?: boolean;
|
||||||
assignedMailboxId?: string;
|
assignedMailboxId?: string; // sent by server
|
||||||
|
requestedMailbox?: string;
|
||||||
|
|
||||||
// subscribing to channel
|
// subscribing to channel
|
||||||
subscribeChannel?: string;
|
subscribeChannel?: string;
|
||||||
unsubscribeChannel?: string;
|
unsubscribeChannel?: string;
|
||||||
|
subscribed?: string; // sent by server to confirm subscription
|
||||||
|
|
||||||
//sending message
|
//sending message
|
||||||
messageChannel?: string;
|
messageChannel?: string;
|
||||||
@@ -216,7 +215,7 @@ function confirmSubscription(
|
|||||||
subscriber: Subscriber,
|
subscriber: Subscriber,
|
||||||
messageChannel: string
|
messageChannel: string
|
||||||
): void {
|
): void {
|
||||||
const messageObject = {
|
const messageObject: WebSocketMessage = {
|
||||||
messageChannel,
|
messageChannel,
|
||||||
subscribed: subscriptionMap.getChannelList(subscriber)?.has(messageChannel)
|
subscribed: subscriptionMap.getChannelList(subscriber)?.has(messageChannel)
|
||||||
? true
|
? true
|
||||||
|
|||||||
Reference in New Issue
Block a user