diff --git a/app/src/main/java/io/xpipe/app/storage/DataStateProviderImpl.java b/app/src/main/java/io/xpipe/app/storage/DataStateProviderImpl.java index 8f2a154d2..9d0759e07 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStateProviderImpl.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStateProviderImpl.java @@ -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; } diff --git a/app/src/main/java/io/xpipe/app/storage/DataStorage.java b/app/src/main/java/io/xpipe/app/storage/DataStorage.java index 6a3a9d85a..c6b01125f 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStorage.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStorage.java @@ -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>(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(); diff --git a/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java b/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java index a5ddc077b..a010d6302 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java @@ -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); diff --git a/core/src/main/java/io/xpipe/core/process/ShellStoreState.java b/core/src/main/java/io/xpipe/core/process/ShellStoreState.java index ab512f02c..3857d8054 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellStoreState.java +++ b/core/src/main/java/io/xpipe/core/process/ShellStoreState.java @@ -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()); + } } diff --git a/core/src/main/java/io/xpipe/core/store/DataStoreState.java b/core/src/main/java/io/xpipe/core/store/DataStoreState.java index 243766b44..4f2bfc1aa 100644 --- a/core/src/main/java/io/xpipe/core/store/DataStoreState.java +++ b/core/src/main/java/io/xpipe/core/store/DataStoreState.java @@ -5,10 +5,16 @@ import lombok.SneakyThrows; import lombok.experimental.SuperBuilder; @SuperBuilder -public class DataStoreState { +public abstract class DataStoreState { public DataStoreState() {} + protected static 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); diff --git a/core/src/main/java/io/xpipe/core/util/DataStateProvider.java b/core/src/main/java/io/xpipe/core/util/DataStateProvider.java index cf2680d7b..4b0599552 100644 --- a/core/src/main/java/io/xpipe/core/util/DataStateProvider.java +++ b/core/src/main/java/io/xpipe/core/util/DataStateProvider.java @@ -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 getState(DataStore store, Supplier def); diff --git a/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java b/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java index f6bf8167e..1bcbcb903 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/script/ScriptStore.java @@ -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