improve mailboxes
This commit is contained in:
+17
-63
@@ -13,82 +13,36 @@ import UDNFrontend from "udn-frontend";
|
||||
|
||||
// setup
|
||||
const UDN = new UDNFrontend();
|
||||
|
||||
// connect
|
||||
UDN.onconnect = () => {
|
||||
console.log("connected!");
|
||||
};
|
||||
UDN.ondisconnect = () => {
|
||||
console.log("disconnected!");
|
||||
};
|
||||
|
||||
UDN.connect("ws://192.168.0.69:1234");
|
||||
UDN.disconnect();
|
||||
|
||||
// messages
|
||||
UDN.onmessage = (data: Message) => {
|
||||
console.log(data);
|
||||
};
|
||||
|
||||
// do stuff
|
||||
UDN.connect("ws://192.168.0.69:1234");
|
||||
|
||||
UDN.subscribe("my-channel");
|
||||
UDN.sendMessage("my-channel", "Hello, world!");
|
||||
|
||||
// subscribe
|
||||
UDN.subscribe("my-channel");
|
||||
UDN.unsubscribe("my-channel");
|
||||
```
|
||||
|
||||
# Type Reference
|
||||
|
||||
```TypeScript
|
||||
export interface Message {
|
||||
// connecting to server
|
||||
requestingServerConnection?: boolean;
|
||||
|
||||
// mailbox
|
||||
requestingMailboxSetup?: boolean;
|
||||
assignedMailboxId?: string; // sent by server
|
||||
requestedMailbox?: string;
|
||||
|
||||
// subscribing to channel
|
||||
subscribeChannel?: string;
|
||||
unsubscribeChannel?: string;
|
||||
subscribed?: boolean; // sent by server to confirm subscription
|
||||
|
||||
//sending message
|
||||
messageChannel?: string;
|
||||
messageBody?: string;
|
||||
// mailbox
|
||||
UDN.onmailbox = (mailboxId: string) => {
|
||||
UDN.connectMailbox(mailboxId);
|
||||
}
|
||||
UDN.onmailboxconnect = (mailboxId: string) => {
|
||||
UDN.deleteMailbox(mailboxId);
|
||||
}
|
||||
|
||||
export default class UDNFrontend {
|
||||
ws: WebSocket | undefined;
|
||||
|
||||
// handlers
|
||||
private connectionHandler = () => {};
|
||||
private disconnectionHandler = () => {};
|
||||
private messageHandler = (data: Message) => {};
|
||||
|
||||
// init
|
||||
set onconnect(handler: () => void);
|
||||
set ondisconnect(handler: () => void);
|
||||
set onmessage(handler: (data: Message) => void);
|
||||
|
||||
// methods
|
||||
connect(address: string): void;
|
||||
disconnect(address: string): void;
|
||||
sendMessage(channel: string, body: string): boolean; // true if message is sent
|
||||
subscribe(channel: string): boolean; // true if request is sent, NOT inherently when subscribed
|
||||
unsubscribe(channel: string): boolean; // true if request is sent, NOT inherently when unsubscribed
|
||||
}
|
||||
UDN.requestMailbox();
|
||||
```
|
||||
|
||||
# Changelog
|
||||
|
||||
## 1.0.1
|
||||
|
||||
- minor fixes
|
||||
|
||||
## 1.0.2
|
||||
|
||||
- minor fixes
|
||||
|
||||
## 1.0.3
|
||||
|
||||
- add `disconnect()` method
|
||||
|
||||
## 1.0.4
|
||||
|
||||
- make ws public
|
||||
+25
-6
@@ -4,8 +4,10 @@ export interface Message {
|
||||
|
||||
// mailbox
|
||||
requestingMailboxSetup?: boolean;
|
||||
assignedMailboxId?: string; // sent by server
|
||||
requestedMailbox?: string;
|
||||
deletingMailbox?: string;
|
||||
assignedMailboxId?: string; // sent by server
|
||||
connectedMailboxId?: string; // sent by server
|
||||
|
||||
// subscribing to channel
|
||||
subscribeChannel?: string;
|
||||
@@ -20,13 +22,14 @@ export interface Message {
|
||||
export default class UDNFrontend {
|
||||
ws: WebSocket | undefined;
|
||||
|
||||
// handlers
|
||||
// HANDLERS
|
||||
private connectionHandler = () => {};
|
||||
private disconnectionHandler = () => {};
|
||||
private messageHandler = (data: Message) => {};
|
||||
private mailboxHandler = (mailboxId: string) => {};
|
||||
private mailboxConnectionHandler = (mailboxId: string) => {};
|
||||
|
||||
// init
|
||||
// INIT
|
||||
set onconnect(handler: () => void) {
|
||||
this.connectionHandler = handler;
|
||||
}
|
||||
@@ -39,8 +42,11 @@ export default class UDNFrontend {
|
||||
set onmailbox(handler: (mailboxId: string) => void) {
|
||||
this.mailboxHandler = handler;
|
||||
}
|
||||
set onmailboxconnect(handler: (mailboxId: string) => void) {
|
||||
this.mailboxConnectionHandler = handler;
|
||||
}
|
||||
|
||||
// utility methods
|
||||
// UTILITY METHODS
|
||||
private send(messageObject: Message): boolean {
|
||||
if (this.ws == undefined) return false;
|
||||
|
||||
@@ -49,7 +55,8 @@ export default class UDNFrontend {
|
||||
return true;
|
||||
}
|
||||
|
||||
// public methods
|
||||
// PUBLIC METHODS
|
||||
// connection
|
||||
connect(address: string): void {
|
||||
this.disconnect();
|
||||
|
||||
@@ -62,6 +69,8 @@ export default class UDNFrontend {
|
||||
|
||||
if (data.assignedMailboxId) {
|
||||
return this.mailboxHandler(data.assignedMailboxId);
|
||||
} else if (data.connectedMailboxId) {
|
||||
return this.mailboxConnectionHandler(data.assignedMailboxId);
|
||||
} else {
|
||||
this.messageHandler(data);
|
||||
}
|
||||
@@ -72,6 +81,7 @@ export default class UDNFrontend {
|
||||
this.ws?.close();
|
||||
}
|
||||
|
||||
// message
|
||||
sendMessage(channel: string, body: string): boolean {
|
||||
const messageObject = {
|
||||
messageChannel: channel,
|
||||
@@ -80,6 +90,7 @@ export default class UDNFrontend {
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
// subscription
|
||||
subscribe(channel: string): boolean {
|
||||
const messageObject = {
|
||||
subscribeChannel: channel,
|
||||
@@ -94,6 +105,7 @@ export default class UDNFrontend {
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
// mailbox
|
||||
requestMailbox(): boolean {
|
||||
const messageObject = {
|
||||
requestingMailboxSetup: true,
|
||||
@@ -101,10 +113,17 @@ export default class UDNFrontend {
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
accessMailbox(mailboxId: string): boolean {
|
||||
connectMailbox(mailboxId: string): boolean {
|
||||
const messageObject = {
|
||||
requestedMailbox: mailboxId,
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
deleteMailbox(mailboxId: string): boolean {
|
||||
const messageObject = {
|
||||
deletingMailbox: mailboxId,
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "udn-frontend",
|
||||
"version": "1.0.4-beta2",
|
||||
"version": "1.0.4-beta4",
|
||||
"description": "TypeScript library for UDN apps",
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user