Auto-update for added/deleted spools in client
This commit is contained in:
@@ -47,18 +47,18 @@ function toWebsocketURL(apiUrl: string) {
|
|||||||
* @param apiUrl The API URL
|
* @param apiUrl The API URL
|
||||||
* @param channel The channel name, not really used
|
* @param channel The channel name, not really used
|
||||||
* @param resource The resource name
|
* @param resource The resource name
|
||||||
* @param id The ID of the resource
|
|
||||||
* @param callback The callback to call when the resource is updated
|
* @param callback The callback to call when the resource is updated
|
||||||
|
* @param id Specific ID to subscribe to, if any. If not specified, subscribes to all IDs.
|
||||||
* @returns A function to unsubscribe from the resource
|
* @returns A function to unsubscribe from the resource
|
||||||
*/
|
*/
|
||||||
function subscribeSingle(apiUrl: string, channel: string, resource: string, id: BaseKey, callback: (event: LiveEvent) => void) {
|
function subscribeSingle(apiUrl: string, channel: string, resource: string, callback: (event: LiveEvent) => void, id?: BaseKey) {
|
||||||
// Verify that WebSockets are supported
|
// Verify that WebSockets are supported
|
||||||
if (!('WebSocket' in window)) {
|
if (!('WebSocket' in window)) {
|
||||||
console.warn("WebSockets are not supported in this browser. Live updates will not be available.");
|
console.warn("WebSockets are not supported in this browser. Live updates will not be available.");
|
||||||
return () => { };
|
return () => { };
|
||||||
}
|
}
|
||||||
|
|
||||||
const websocketURL = toWebsocketURL(`${apiUrl}/${resource}/${id}`);
|
const websocketURL = id ? toWebsocketURL(`${apiUrl}/${resource}/${id}`) : toWebsocketURL(`${apiUrl}/${resource}`);
|
||||||
|
|
||||||
const ws = new WebSocket(websocketURL);
|
const ws = new WebSocket(websocketURL);
|
||||||
ws.onmessage = (message) => {
|
ws.onmessage = (message) => {
|
||||||
@@ -70,8 +70,8 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, id:
|
|||||||
channel: channel,
|
channel: channel,
|
||||||
type: type,
|
type: type,
|
||||||
payload: {
|
payload: {
|
||||||
ids: [id],
|
|
||||||
data: data.payload,
|
data: data.payload,
|
||||||
|
ids: [data.payload.id],
|
||||||
},
|
},
|
||||||
date: date,
|
date: date,
|
||||||
}
|
}
|
||||||
@@ -110,12 +110,13 @@ const liveProvider = (
|
|||||||
let idList: BaseKey[];
|
let idList: BaseKey[];
|
||||||
if (ids) idList = ids;
|
if (ids) idList = ids;
|
||||||
else if (id) idList = [id];
|
else if (id) idList = [id];
|
||||||
else throw new Error(
|
else {
|
||||||
"[useSubscription]: `id` or `ids` is required in `params`",
|
// No ID specified, subscribe to all IDs
|
||||||
);
|
return [subscribeSingle(apiUrl, channel, resource, callback)]
|
||||||
|
}
|
||||||
|
|
||||||
return idList.map((id) => {
|
return idList.map((id) => {
|
||||||
return subscribeSingle(apiUrl, channel, resource, id, callback);
|
return subscribeSingle(apiUrl, channel, resource, callback, id);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
unsubscribe: (closers: (() => void)[]) => {
|
unsubscribe: (closers: (() => void)[]) => {
|
||||||
|
|||||||
@@ -148,6 +148,16 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
|
|||||||
mode: "server",
|
mode: "server",
|
||||||
initial: initialState.filters,
|
initial: initialState.filters,
|
||||||
},
|
},
|
||||||
|
liveMode: "manual",
|
||||||
|
onLiveEvent(event) {
|
||||||
|
if (event.type === "created" || event.type === "deleted") {
|
||||||
|
// updated is handled by the liveify
|
||||||
|
invalidate({
|
||||||
|
resource: "spool",
|
||||||
|
invalidates: ["list"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create state for the columns to show
|
// Create state for the columns to show
|
||||||
|
|||||||
Reference in New Issue
Block a user