Push multiple updates in each update event

Refactor HostUpdatesEvent, InstanceDetailsUpdatesEvent, ModUpdatesEvent,
ModPackUpdatesEvent and UserUpdatesEvent to have multiple updates of the
entities they support in each event. This is intended to make the events
capable of syncing the entire list that they represent.
This commit is contained in:
Hornwitser committed 2023-12-08 22:01:46 +01:00
1 parent f12a9926eb
commit 20330bc2c3
18 files changed
+215 -190

No files matched your search

+12 -10
View File
@@ -35,11 +35,11 @@ export function useHost(id: number): [ HostState, () => void ] {
}
updateHost();
function updateHandler(newHost: lib.HostDetails) {
if (newHost.id !== id) {
return;
function updateHandler(newHosts: lib.HostDetails[]) {
const newHost = newHosts.find(h => h.id === id);
if (newHost) {
setHost({ ...newHost, loading: false, missing: false, present: true });
}
setHost({ ...newHost, loading: false, missing: false, present: true });
}
control.hostUpdate.subscribe(updateHandler);
@@ -66,14 +66,16 @@ export function useHostList() {
useEffect(() => {
updateHostList();
function updateHandler(newHost: lib.HostDetails) {
function updateHandler(newHosts: lib.HostDetails[]) {
setHostList(oldList => {
let newList = oldList.concat();
let index = newList.findIndex(s => s.id === newHost.id);
if (index !== -1) {
newList[index] = newHost;
} else {
newList.push(newHost);
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;
});