Updated client to support new message format
This commit is contained in:
1
client/.gitignore
vendored
1
client/.gitignore
vendored
@@ -21,3 +21,4 @@
|
|||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
|
.vite
|
||||||
|
|||||||
@@ -1,5 +1,18 @@
|
|||||||
import { BaseKey, LiveEvent, LiveProvider } from "@refinedev/core";
|
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.
|
* Converts an API URL to a WebSocket URL.
|
||||||
* E.g. "https://example.com/api/v1/..." -> "ws://example.com/api/v1/..."
|
* 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);
|
const ws = new WebSocket(websocketURL);
|
||||||
ws.onmessage = (message) => {
|
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 = {
|
const liveEvent: LiveEvent = {
|
||||||
channel: channel,
|
channel: channel,
|
||||||
type: "updated",
|
type: type,
|
||||||
payload: {
|
payload: {
|
||||||
data: JSON.parse(message.data),
|
|
||||||
ids: [id],
|
ids: [id],
|
||||||
|
data: data.payload,
|
||||||
},
|
},
|
||||||
date: new Date(),
|
date: date,
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(liveEvent);
|
callback(liveEvent);
|
||||||
|
|||||||
Reference in New Issue
Block a user