mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 07:33:12 +09:00
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:
1 parent
867bb0c646
commit
8c17adf15b
27 files changed
+410
-713
No files matched your search
@@ -1,91 +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";
|
||||
|
||||
const { logger } = lib;
|
||||
|
||||
export type InstanceState = Partial<lib.InstanceDetails> & {
|
||||
loading?: boolean,
|
||||
present?: boolean,
|
||||
missing?: boolean,
|
||||
export function useInstance(id?: number) {
|
||||
const [instances, synced] = useInstances();
|
||||
return [id !== undefined ? instances.get(id) : undefined, synced] as const;
|
||||
}
|
||||
|
||||
export function useInstance(id: number): [InstanceState, ()=>void] {
|
||||
let control = useContext(ControlContext);
|
||||
let [instance, setInstance] = useState<InstanceState>({ loading: true });
|
||||
|
||||
function updateInstance() {
|
||||
control.send(new lib.InstanceDetailsGetRequest(id)).then(updatedInstance => {
|
||||
setInstance({ ...updatedInstance, present: true });
|
||||
}).catch((err: any) => {
|
||||
logger.error(`Failed to get instance: ${err}`);
|
||||
setInstance({ missing: true });
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!Number.isInteger(id)) {
|
||||
setInstance({ missing: true });
|
||||
return undefined;
|
||||
}
|
||||
updateInstance();
|
||||
|
||||
function updateHandler(newInstances: lib.InstanceDetails[]) {
|
||||
const newInstance = newInstances.find(i => i.id === id);
|
||||
if (newInstance) {
|
||||
setInstance({ ...newInstance, present: true });
|
||||
}
|
||||
}
|
||||
|
||||
control.instanceUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.instanceUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, [id]);
|
||||
|
||||
return [instance, updateInstance];
|
||||
}
|
||||
|
||||
export function useInstanceList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [instanceList, setInstanceList] = useState<lib.InstanceDetails[]>([]);
|
||||
|
||||
function updateInstanceList() {
|
||||
control.send(new lib.InstanceDetailsListRequest()).then(instances => {
|
||||
setInstanceList(instances);
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to list instances:\n${err}`);
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
updateInstanceList();
|
||||
|
||||
function updateHandler(newInstances: lib.InstanceDetails[]) {
|
||||
setInstanceList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
for (const newInstance of newInstances) {
|
||||
let index = newList.findIndex(s => s.id === newInstance.id);
|
||||
if (newInstance.status !== "deleted") {
|
||||
if (index !== -1) {
|
||||
newList[index] = newInstance;
|
||||
} else {
|
||||
newList.push(newInstance);
|
||||
}
|
||||
} else if (index !== -1) {
|
||||
newList.splice(index, 1);
|
||||
}
|
||||
}
|
||||
return newList;
|
||||
});
|
||||
}
|
||||
|
||||
control.instanceUpdate.subscribe(updateHandler);
|
||||
return () => {
|
||||
control.instanceUpdate.unsubscribe(updateHandler);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return [instanceList];
|
||||
export function useInstances() {
|
||||
const control = useContext(ControlContext);
|
||||
const subscribe = useCallback((callback: () => void) => control.instances.subscribe(callback), [control]);
|
||||
return useSyncExternalStore(subscribe, () => control.instances.getSnapshot());
|
||||
}
|
||||
Reference in new issue
Block a user