add mailboxes

This commit is contained in:
marlon-erler
2024-07-08 21:13:56 +02:00
parent 85164a4e0a
commit b88959e135
16 changed files with 342 additions and 79 deletions
+39 -21
View File
@@ -21,6 +21,7 @@ touch src/index.tsx dist/index.html
```
The following build script will be enough:
```JSON
{
"scripts": {
@@ -102,6 +103,7 @@ listState.remove(newItem);
## Persistence
States can persist through reloads via LocalStorage. To implement this, modify your code like this:
```diff
-const myState = new React.State("hello");
+const myState = React.restoreState("my-state", "hello");
@@ -213,44 +215,60 @@ document.body.append(
```
# Changelog
## 1.1.0
- Improve code
- Add missing `break` statements for directives
- Add `toggle` directive
- Improve documentation
- Improve code
- Add missing `break` statements for directives
- Add `toggle` directive
- Improve documentation
## 1.1.1
- Improve documentation
- Improve documentation
## 1.1.2
- Remove `console.log()` calls
- Improve documentation
- Remove `console.log()` calls
- Improve documentation
## 1.1.3
- Add `on:enter` directive
- Add `on:enter` directive
## 1.2.0
**BREAKING CHANGES**
- Replace UUID class with function;
- `new UUID()` => `UUID() returns string`
- Replace definition of `Identifiable`
- now has `id: string` instead of `uuid: UUID()`
- Replace UUID class with function;
- `new UUID()` => `UUID() returns string`
- Replace definition of `Identifiable`
- now has `id: string` instead of `uuid: UUID()`
Other changes:
- Add State persistence
- Add error description when utilizing `subscribe:children` incorrectly
- Fix bug where ListState subscriptions were not called
- Improve documentation
- Add State persistence
- Add error description when utilizing `subscribe:children` incorrectly
- Fix bug where ListState subscriptions were not called
- Improve documentation
## 1.2.1
- Add `set:<attribute>` directive
- Add `set:<attribute>` directive
## 1.2.2
- Remove list parameter from `ListItemConverter`
- Add `clear()` method to `ListState`
- Remove list parameter from `ListItemConverter`
- Add `clear()` method to `ListState`
## 1.2.3
- Allow `toggle:<attribute>` to be used without a state
- Allow `toggle:<attribute>` to be used without a state
## 1.2.4
- On `subscribe:children`, scroll new elements into view
- On `subscribe:children`, scroll new elements into view
## 1.2.5
- Instead of using `scrollIntoView()`, `subscribe:children` now scrolls to the bottom
+3 -6
View File
@@ -205,19 +205,16 @@ export function createElement(
try {
const [listState, toElement] = value as [
listState: ListState<any>,
(
item: any,
listState: ListState<any>
) => HTMLElement
ListItemConverter<any>
];
listState.handleAddition((newItem) => {
const child = toElement(newItem, listState);
const child = toElement(newItem);
listState.handleRemoval(newItem, () =>
child.remove()
);
element.append(child);
child.scrollIntoView();
element.scrollTop = element.scrollHeight;
});
} catch {
throw `error: cannot process subscribe:children directive because ListItemConverter is not defined. Usage: "subscribe:children={[list, converter]}"; you can find a more detailed example in the documentation`;
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "bloatless-react",
"version": "1.2.4",
"version": "1.2.5-beta1",
"description": "very minimal and flexible react alternative",
"repository": {
"type": "git",
+19 -2
View File
@@ -35,10 +35,18 @@ UDN.unsubscribe("my-channel");
```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 the server to confirm subscription
subscribed?: boolean; // sent by server to confirm subscription
//sending message
messageChannel?: string;
@@ -46,7 +54,7 @@ export interface Message {
}
export default class UDNFrontend {
private ws: WebSocket | undefined;
ws: WebSocket | undefined;
// handlers
private connectionHandler = () => {};
@@ -60,6 +68,7 @@ export default class UDNFrontend {
// 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
@@ -75,3 +84,11 @@ export default class UDNFrontend {
## 1.0.2
- minor fixes
## 1.0.3
- add `disconnect()` method
## 1.0.4
- make ws public
+41 -4
View File
@@ -1,8 +1,16 @@
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 the server to confirm subscription
subscribed?: boolean; // sent by server to confirm subscription
//sending message
messageChannel?: string;
@@ -10,12 +18,13 @@ export interface Message {
}
export default class UDNFrontend {
private ws: WebSocket | undefined;
ws: WebSocket | undefined;
// handlers
private connectionHandler = () => {};
private disconnectionHandler = () => {};
private messageHandler = (data: Message) => {};
private mailboxHandler = (mailboxId: string) => {};
// init
set onconnect(handler: () => void) {
@@ -27,9 +36,12 @@ export default class UDNFrontend {
set onmessage(handler: (data: Message) => void) {
this.messageHandler = handler;
}
set onmailbox(handler: (mailboxId: string) => void) {
this.mailboxHandler = handler;
}
// utility methods
private send(messageObject: Object): boolean {
private send(messageObject: Message): boolean {
if (this.ws == undefined) return false;
const messageString = JSON.stringify(messageObject);
@@ -39,16 +51,27 @@ export default class UDNFrontend {
// public methods
connect(address: string): void {
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.messageHandler(data);
if (data.assignedMailboxId) {
return this.mailboxHandler(data.assignedMailboxId);
} else {
this.messageHandler(data);
}
});
}
disconnect(): void {
this.ws?.close();
}
sendMessage(channel: string, body: string): boolean {
const messageObject = {
messageChannel: channel,
@@ -70,4 +93,18 @@ export default class UDNFrontend {
};
return this.send(messageObject);
}
requestMailbox(): boolean {
const messageObject = {
requestingMailboxSetup: true,
};
return this.send(messageObject);
}
accessMailbox(mailboxId: string): boolean {
const messageObject = {
requestedMailbox: mailboxId,
};
return this.send(messageObject);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "udn-frontend",
"version": "1.0.2",
"version": "1.0.4-beta2",
"description": "TypeScript library for UDN apps",
"main": "index.ts",
"scripts": {