Cache save details on the controller

Keep a copy of the instance saves details on the controller so that
saves can be queried even when the host is disconnected and the
controller can properly track and provide updates on the saves to
its clients.
This commit is contained in:
Hornwitser committed 2023-12-11 20:21:43 +01:00
1 parent d186f85773
commit a2914bda33
13 files changed
+219 -66

No files matched your search

+22 -7
View File
@@ -10,8 +10,8 @@ export function useSaves(instanceId?: number): lib.SaveDetails[] {
let [saves, setSaves] = useState<lib.SaveDetails[]>([]);
function updateSaves() {
control.sendTo({ instanceId: instanceId! }, new lib.InstanceListSavesRequest()).then(updatedSaves => {
setSaves(updatedSaves);
control.sendTo("controller", new lib.InstanceSaveDetailsListRequest()).then(updatedSaves => {
setSaves(updatedSaves.filter(save => save.instanceId === instanceId));
}).catch(err => {
logger.error(`Failed to list instance saves: ${err}`);
setSaves([]);
@@ -25,11 +25,26 @@ export function useSaves(instanceId?: number): lib.SaveDetails[] {
}
updateSaves();
function updateHandler(data: lib.InstanceSaveListUpdateEvent) {
if (data.instanceId !== instanceId) {
return;
}
setSaves(data.saves);
function updateHandler(updates: lib.SaveDetails[]) {
setSaves(oldList => {
let newList = oldList.concat();
for (const newSave of updates) {
if (newSave.instanceId !== instanceId) {
continue;
}
let index = newList.findIndex(s => s.id === newSave.id);
if (!newSave.isDeleted) {
if (index !== -1) {
newList[index] = newSave;
} else {
newList.push(newSave);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
}
return newList;
});
}
control.saveListUpdate.subscribe(updateHandler);