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;
});
+15 -13
View File
@@ -31,11 +31,11 @@ export function useInstance(id: number): [InstanceState, ()=>void] {
}
updateInstance();
function updateHandler(newInstance: lib.InstanceDetails) {
if (newInstance.id !== id) {
return;
function updateHandler(newInstances: lib.InstanceDetails[]) {
const newInstance = newInstances.find(i => i.id === id);
if (newInstance) {
setInstance({ ...newInstance, present: true });
}
setInstance({ ...newInstance, present: true });
}
control.instanceUpdate.subscribe(updateHandler);
@@ -62,18 +62,20 @@ export function useInstanceList() {
useEffect(() => {
updateInstanceList();
function updateHandler(newInstance: lib.InstanceDetails) {
function updateHandler(newInstances: lib.InstanceDetails[]) {
setInstanceList(oldList => {
let newList = oldList.concat();
let index = newList.findIndex(s => s.id === newInstance.id);
if (newInstance.status !== "deleted") {
if (index !== -1) {
newList[index] = newInstance;
} else {
newList.push(newInstance);
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);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
return newList;
});
+15 -13
View File
@@ -33,11 +33,11 @@ export function useModPack(id: number) {
}
updateModPack();
function updateHandler(newModPack: lib.ModPack) {
if (newModPack.id !== id) {
return;
function updateHandler(modPacks: lib.ModPack[]) {
const newModPack = modPacks.find(m => m.id === id);
if (newModPack) {
setModPack(newModPack);
}
setModPack(newModPack);
}
control.modPackUpdate.subscribe(updateHandler);
return () => {
@@ -63,18 +63,20 @@ export function useModPackList() {
useEffect(() => {
updateModPackList();
function updateHandler(newModPack: lib.ModPack) {
function updateHandler(newModPacks: lib.ModPack[]) {
setModPackList(oldList => {
let newList = oldList.concat();
let index = newList.findIndex(u => u.id === newModPack.id);
if (!newModPack.isDeleted) {
if (index !== -1) {
newList[index] = newModPack;
} else {
newList.push(newModPack);
for (const newModPack of newModPacks) {
let index = newList.findIndex(u => u.id === newModPack.id);
if (!newModPack.isDeleted) {
if (index !== -1) {
newList[index] = newModPack;
} else {
newList.push(newModPack);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
return newList;
});
+11 -9
View File
@@ -21,18 +21,20 @@ export function useModList() {
useEffect(() => {
updateModList();
function updateHandler(newMod: lib.ModInfo) {
function updateHandler(newMods: lib.ModInfo[]) {
setModList(oldList => {
let newList = oldList.concat();
let index = newList.findIndex(mod => mod.name === newMod.name && mod.version === newMod.version);
if (!newMod.isDeleted) {
if (index !== -1) {
newList[index] = newMod;
} else {
newList.push(newMod);
for (const newMod of newMods) {
let index = newList.findIndex(mod => mod.name === newMod.name && mod.version === newMod.version);
if (!newMod.isDeleted) {
if (index !== -1) {
newList[index] = newMod;
} else {
newList.push(newMod);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
return newList;
});
+15 -13
View File
@@ -78,11 +78,11 @@ export function useUser(name: string): [RawUserState, () => void] {
}
updateUser();
function updateHandler(newUser: lib.User) {
if (newUser.name !== name) {
return;
function updateHandler(newUsers: lib.User[]) {
const newUser = newUsers.find(u => u.name === name);
if (newUser) {
setUser({ ...newUser, present: true });
}
setUser({ ...newUser, present: true });
}
control.userUpdate.subscribe(updateHandler);
@@ -109,18 +109,20 @@ export function useUserList() {
useEffect(() => {
updateUserList();
function updateHandler(newUser: lib.User) {
function updateHandler(newUsers: lib.User[]) {
setUserList(oldList => {
let newList = oldList.concat();
let index = newList.findIndex(u => u.name === newUser.name);
if (!newUser.isDeleted) {
if (index !== -1) {
newList[index] = newUser;
} else {
newList.push(newUser);
for (const newUser of newUsers) {
let index = newList.findIndex(u => u.name === newUser.name);
if (!newUser.isDeleted) {
if (index !== -1) {
newList[index] = newUser;
} else {
newList.push(newUser);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
return newList;
});