Updated client to support new message format

This commit is contained in:
Donkie
2023-10-15 21:04:34 +02:00
parent 5f7a063125
commit aa28bd1f19
2 changed files with 21 additions and 3 deletions

View File

@@ -1,5 +1,18 @@
import { BaseKey, LiveEvent, LiveProvider } from "@refinedev/core";
/**
* A spoolman websocket event.
*/
interface Event {
type: "updated" | "deleted" | "added";
resource: "filament" | "spool" | "vendor";
date: string;
payload: {
id: number;
[key: string]: unknown;
};
}
/**
* Converts an API URL to a WebSocket URL.
* E.g. "https://example.com/api/v1/..." -> "ws://example.com/api/v1/..."
@@ -49,14 +62,18 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, id:
const ws = new WebSocket(websocketURL);
ws.onmessage = (message) => {
const data: Event = JSON.parse(message.data);
const type = data.type === "added" ? "created" : data.type;
const date = new Date(data.date);
const liveEvent: LiveEvent = {
channel: channel,
type: "updated",
type: type,
payload: {
data: JSON.parse(message.data),
ids: [id],
data: data.payload,
},
date: new Date(),
date: date,
}
callback(liveEvent);