Properly update state on children refresh

This commit is contained in:
crschnick 2023-12-08 04:39:44 +00:00
parent 0a8881a9cf
commit bdcc6a1295
7 changed files with 37 additions and 10 deletions

View file

@ -10,7 +10,7 @@ import java.util.function.Supplier;
public class DataStateProviderImpl extends DataStateProvider {
@Override
public void setState(DataStore store, Object value) {
public void setState(DataStore store, DataStoreState value) {
if (DataStorage.get() == null) {
return;
}

View file

@ -5,10 +5,7 @@ import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.util.FixedHierarchyStore;
import io.xpipe.app.util.ThreadHelper;
import io.xpipe.core.store.DataStore;
import io.xpipe.core.store.DataStoreId;
import io.xpipe.core.store.FixedChildStore;
import io.xpipe.core.store.LocalStore;
import io.xpipe.core.store.*;
import io.xpipe.core.util.UuidHelper;
import javafx.util.Pair;
import lombok.Getter;
@ -255,7 +252,7 @@ public abstract class DataStorage {
nc.getStore().getFixedId() == ((FixedChildStore) entry.getStore()).getFixedId())
.findFirst()
.orElse(null);
return new Pair<>(entry, found);
return new Pair<DataStoreEntry, DataStoreEntryRef<? extends FixedChildStore>>(entry, found);
})
.filter(en -> en.getValue() != null)
.toList();
@ -268,6 +265,13 @@ public abstract class DataStorage {
addStoreEntriesIfNotPresent(toAdd.stream().map(DataStoreEntryRef::get).toArray(DataStoreEntry[]::new));
toUpdate.forEach(pair -> {
pair.getKey().setStoreInternal(pair.getValue().getStore(), false);
// Update state by merging
if (pair.getKey().getStorePersistentState() != null && pair.getValue().get().getStorePersistentState() != null) {
var mergedState = pair.getKey().getStorePersistentState().deepCopy();
mergedState.merge(pair.getValue().get().getStorePersistentState());
pair.getKey().setStorePersistentState(mergedState);
}
});
saveAsync();
return !newChildren.isEmpty();

View file

@ -59,7 +59,7 @@ public class DataStoreEntry extends StorageElement {
UUID categoryUuid;
@NonFinal
Object storePersistentState;
DataStoreState storePersistentState;
@NonFinal
JsonNode storePersistentStateNode;
@ -284,7 +284,7 @@ public class DataStoreEntry extends StorageElement {
}
}
public void setStorePersistentState(Object value) {
public void setStorePersistentState(DataStoreState value) {
var changed = !Objects.equals(storePersistentState, value);
this.storePersistentState = value;
this.storePersistentStateNode = JacksonMapper.getDefault().valueToTree(value);

View file

@ -27,4 +27,13 @@ public class ShellStoreState extends DataStoreState implements OsNameState {
public boolean isRunning() {
return running != null ? running : false;
}
@Override
public void merge(DataStoreState newer) {
var shellStoreState = (ShellStoreState) newer;
osType = useNewer(osType, shellStoreState.getOsType());
osName = useNewer(osName, shellStoreState.getOsName());
shellDialect = useNewer(shellDialect, shellStoreState.getShellDialect());
running = useNewer(running, shellStoreState.getRunning());
}
}

View file

@ -5,10 +5,16 @@ import lombok.SneakyThrows;
import lombok.experimental.SuperBuilder;
@SuperBuilder
public class DataStoreState {
public abstract class DataStoreState {
public DataStoreState() {}
protected static <T> T useNewer(T older, T newer) {
return newer != null ? newer : older;
}
public abstract void merge(DataStoreState newer);
@SneakyThrows
public String toString() {
var tree = JacksonMapper.getDefault().valueToTree(this);

View file

@ -20,7 +20,7 @@ public abstract class DataStateProvider {
return INSTANCE;
}
public abstract void setState(DataStore store, Object value);
public abstract void setState(DataStore store, DataStoreState value);
public abstract <T extends DataStoreState> T getState(DataStore store, Supplier<T> def);

View file

@ -7,6 +7,7 @@ import io.xpipe.app.storage.DataStoreEntryRef;
import io.xpipe.app.util.Validators;
import io.xpipe.core.process.ScriptSnippet;
import io.xpipe.core.process.ShellControl;
import io.xpipe.core.process.ShellStoreState;
import io.xpipe.core.process.SimpleScriptSnippet;
import io.xpipe.core.store.DataStore;
import io.xpipe.core.store.DataStoreState;
@ -163,6 +164,13 @@ public abstract class ScriptStore extends JacksonizedValue implements DataStore,
public static class State extends DataStoreState {
boolean isDefault;
boolean bringToShell;
@Override
public void merge(DataStoreState newer) {
var s = (State) newer;
isDefault = s.isDefault;
bringToShell = s.bringToShell;
}
}
@Override