Refactor EventSubscriber to track its resource

Process updates of the resource EventSubscriber are subscribed to and
have it keep a mapping of id to value syncronised with event provider as
long as there are subscription callbacks registered with it.  The
mapping is kept around after the last callback is unsubscribed so that
it can be efficiently resynced with the resource based on the updatedAt
timestaps of individual elements.

This allows the EventSubscriber to provide the right building blocks to
provide its values to React components via the the useSyncExternalStore
hook.  This in turn greatly simplifies the web_ui models for the
subscribed resources.
This commit is contained in:
Hornwitser committed 2023-12-12 23:40:29 +01:00
1 parent 867bb0c646
commit 8c17adf15b
27 files changed
+410 -713

No files matched your search

+8 -87
View File
@@ -1,92 +1,13 @@
import { useEffect, useContext, useState } from "react";
import { useCallback, useContext, useSyncExternalStore } from "react";
import ControlContext from "../components/ControlContext";
import * as lib from "@clusterio/lib";
export type HostState = Partial<lib.HostDetails> & {
loading?: boolean,
present?: boolean,
missing?: boolean,
export function useHost(id?: number) {
const [hosts, synced] = useHosts();
return [id !== undefined ? hosts.get(id) : undefined, synced] as const;
}
export function useHost(id: number): [ HostState, () => void ] {
let control = useContext(ControlContext);
let [host, setHost] = useState<HostState>({ loading: true });
function updateHost() {
// XXX optimize by requesting only the host in question
control.send(
new lib.HostListRequest()
).then((hosts: lib.HostDetails[]) => {
let match = hosts.find(i => i.id === id);
if (!match) {
setHost({ missing: true });
} else {
setHost({ ...match, present: true });
}
});
}
useEffect(() => {
if (!Number.isInteger(id)) {
setHost({ missing: true });
return undefined;
}
updateHost();
function updateHandler(newHosts: lib.HostDetails[]) {
const newHost = newHosts.find(h => h.id === id);
if (newHost) {
setHost({ ...newHost, loading: false, missing: false, present: true });
}
}
control.hostUpdate.subscribe(updateHandler);
return () => {
control.hostUpdate.unsubscribe(updateHandler);
};
}, [id]);
return [host, updateHost];
}
export function useHostList() {
let control = useContext(ControlContext);
let [hostList, setHostList] = useState<lib.HostDetails[]>([]);
function updateHostList() {
control.send(
new lib.HostListRequest()
).then((hosts: lib.HostDetails[]) => {
setHostList(hosts);
});
}
useEffect(() => {
updateHostList();
function updateHandler(newHosts: lib.HostDetails[]) {
setHostList(oldList => {
let newList = oldList.concat();
for (const newHost of newHosts) {
let index = newList.findIndex(s => s.id === newHost.id);
if (index !== -1) {
newList[index] = newHost;
} else {
newList.push(newHost);
}
}
return newList;
});
}
control.hostUpdate.subscribe(updateHandler);
return () => {
control.hostUpdate.unsubscribe(updateHandler);
};
}, []);
return [hostList];
export function useHosts() {
const control = useContext(ControlContext);
const subscribe = useCallback((callback: () => void) => control.hosts.subscribe(callback), [control]);
return useSyncExternalStore(subscribe, () => control.hosts.getSnapshot());
}