Rework states

This commit is contained in:
crschnick 2024-05-29 05:41:29 +00:00
parent 6f6b4a76d4
commit e14b38b31f
10 changed files with 75 additions and 86 deletions

View file

@ -24,6 +24,10 @@ public class BrowserEntry {
}
private static BrowserIconFileType fileType(FileSystem.FileEntry rawFileEntry) {
if (rawFileEntry == null) {
return null;
}
if (rawFileEntry.getKind() == FileKind.DIRECTORY) {
return null;
}
@ -38,6 +42,10 @@ public class BrowserEntry {
}
private static BrowserIconDirectoryType directoryType(FileSystem.FileEntry rawFileEntry) {
if (rawFileEntry == null) {
return null;
}
if (rawFileEntry.getKind() != FileKind.DIRECTORY) {
return null;
}

View file

@ -448,8 +448,8 @@ public abstract class DataStorage {
pair.getKey().setStoreInternal(merged, false);
}
var mergedState = pair.getKey().getStorePersistentState().copy();
mergedState.merge(pair.getValue().get().getStorePersistentState());
var s = pair.getKey().getStorePersistentState();
var mergedState = s.mergeCopy(pair.getValue().get().getStorePersistentState());
pair.getKey().setStorePersistentState(mergedState);
}
}

View file

@ -58,12 +58,13 @@ public interface ShellControl extends ProcessControl {
default <T extends ShellStoreState> ShellControl withShellStateInit(StatefulDataStore<T> store) {
return onInit(shellControl -> {
var s = store.getState();
s.setOsType(shellControl.getOsType());
s.setShellDialect(shellControl.getOriginalShellDialect());
s.setRunning(true);
s.setOsName(shellControl.getOsName());
store.setState(s);
var s = store.getState().toBuilder()
.osType(shellControl.getOsType())
.shellDialect(shellControl.getOriginalShellDialect())
.running(true)
.osName(shellControl.getOsName())
.build();
store.setState(s.asNeeded());
});
}
@ -74,9 +75,8 @@ public interface ShellControl extends ProcessControl {
return;
}
var s = store.getState();
s.setRunning(false);
store.setState(s);
var s = store.getState().toBuilder().running(false).build();
store.setState(s.asNeeded());
});
}

View file

@ -0,0 +1,24 @@
package io.xpipe.core.process;
import io.xpipe.core.store.DataStoreState;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
@Value
@EqualsAndHashCode(callSuper=true)
@SuperBuilder(toBuilder = true)
@Jacksonized
public class ShellNameStoreState extends ShellStoreState {
String shellName;
@Override
public DataStoreState mergeCopy(DataStoreState newer) {
var n = (ShellNameStoreState) newer;
var b = toBuilder();
mergeBuilder(n,b);
return b.shellName(useNewer(shellName, n.shellName)).build();
}
}

View file

@ -1,19 +1,18 @@
package io.xpipe.core.process;
import io.xpipe.core.store.DataStoreState;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
@FieldDefaults(level = AccessLevel.PRIVATE)
@Setter
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@Getter
@EqualsAndHashCode(callSuper=true)
@SuperBuilder(toBuilder = true)
@Jacksonized
@SuperBuilder
public class ShellStoreState extends DataStoreState implements OsNameState {
OsType.Any osType;
@ -26,11 +25,17 @@ public class ShellStoreState extends DataStoreState implements OsNameState {
}
@Override
public void merge(DataStoreState newer) {
public DataStoreState mergeCopy(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());
var b = toBuilder();
mergeBuilder(shellStoreState, b);
return b.build();
}
protected void mergeBuilder(ShellStoreState shellStoreState, ShellStoreStateBuilder<?,?> b) {
b.osType(useNewer(osType, shellStoreState.getOsType()))
.osName(useNewer(osName, shellStoreState.getOsName()))
.shellDialect(useNewer(shellDialect, shellStoreState.getShellDialect()))
.running(useNewer(running, shellStoreState.getRunning()));
}
}

View file

@ -1,49 +1,22 @@
package io.xpipe.core.store;
import io.xpipe.core.util.JacksonMapper;
import lombok.SneakyThrows;
import lombok.experimental.SuperBuilder;
@SuperBuilder
@SuperBuilder(toBuilder = true)
public abstract class DataStoreState {
public DataStoreState() {}
@SuppressWarnings("unchecked")
public <DS extends DataStoreState> DS asNeeded() {
return (DS) this;
}
protected static <T> T useNewer(T older, T newer) {
return newer != null ? newer : older;
}
public abstract void merge(DataStoreState newer);
@SneakyThrows
public DataStoreState deepCopy() {
return JacksonMapper.getDefault().treeToValue(JacksonMapper.getDefault().valueToTree(this), getClass());
}
@Override
public final int hashCode() {
var tree = JacksonMapper.getDefault().valueToTree(this);
return tree.hashCode();
}
@Override
public final boolean equals(Object o) {
if (this == o) {
return true;
}
if (o != null && getClass() != o.getClass()) {
return false;
}
var tree = JacksonMapper.getDefault().valueToTree(this);
var otherTree = JacksonMapper.getDefault().valueToTree(o);
return tree.equals(otherTree);
}
@SneakyThrows
public String toString() {
var tree = JacksonMapper.getDefault().valueToTree(this);
return tree.toPrettyString();
public DataStoreState mergeCopy(DataStoreState newer) {
return this;
}
}

View file

@ -1,11 +1,9 @@
package io.xpipe.core.store;
import io.xpipe.core.util.DataStateProvider;
import lombok.SneakyThrows;
import java.util.Arrays;
import java.util.function.Supplier;
public interface StatefulDataStore<T extends DataStoreState> extends DataStore {
@ -19,20 +17,14 @@ public interface StatefulDataStore<T extends DataStoreState> extends DataStore {
return getStateClass().cast(m.invoke(b));
}
@SuppressWarnings("unchecked")
default T getState() {
return (T)
DataStateProvider.get().getState(this, this::createDefaultState).deepCopy();
return DataStateProvider.get().getState(this, this::createDefaultState);
}
default void setState(T val) {
DataStateProvider.get().setState(this, val);
}
default T getState(Supplier<T> def) {
return DataStateProvider.get().getState(this, def);
}
@SneakyThrows
@SuppressWarnings("unchecked")
default Class<T> getStateClass() {

View file

@ -31,15 +31,13 @@ public class ScriptGroupStoreProvider implements DataStoreProvider {
var def = StoreToggleComp.<ScriptGroupStore>simpleToggle(
"base.isDefaultGroup", sec, s -> s.getState().isDefault(), (s, aBoolean) -> {
var state = s.getState();
state.setDefault(aBoolean);
var state = s.getState().toBuilder().isDefault(aBoolean).build();
s.setState(state);
});
var bring = StoreToggleComp.<ScriptGroupStore>simpleToggle(
"base.bringToShells", sec, s -> s.getState().isBringToShell(), (s, aBoolean) -> {
var state = s.getState();
state.setBringToShell(aBoolean);
var state = s.getState().toBuilder().bringToShell(aBoolean).build();
s.setState(state);
});

View file

@ -6,15 +6,14 @@ import io.xpipe.app.storage.DataStoreEntry;
import io.xpipe.app.storage.DataStoreEntryRef;
import io.xpipe.app.util.ShellTemp;
import io.xpipe.app.util.Validators;
import io.xpipe.core.process.ShellInitCommand;
import io.xpipe.core.process.ShellControl;
import io.xpipe.core.process.ShellInitCommand;
import io.xpipe.core.store.DataStore;
import io.xpipe.core.store.DataStoreState;
import io.xpipe.core.store.FileNames;
import io.xpipe.core.store.StatefulDataStore;
import io.xpipe.core.util.JacksonizedValue;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
@ -222,20 +221,12 @@ public abstract class ScriptStore extends JacksonizedValue implements DataStore,
public abstract List<DataStoreEntryRef<ScriptStore>> getEffectiveScripts();
@FieldDefaults(level = AccessLevel.PRIVATE)
@Setter
@Getter
@SuperBuilder
@Value
@EqualsAndHashCode(callSuper=true)
@SuperBuilder(toBuilder = true)
@Jacksonized
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;
}
}
}

View file

@ -56,15 +56,13 @@ public class SimpleScriptStoreProvider implements DataStoreProvider {
var def = StoreToggleComp.<SimpleScriptStore>simpleToggle(
"base.isDefaultGroup", sec, s -> s.getState().isDefault(), (s, aBoolean) -> {
var state = s.getState();
state.setDefault(aBoolean);
var state = s.getState().toBuilder().isDefault(aBoolean).build();
s.setState(state);
});
var bring = StoreToggleComp.<SimpleScriptStore>simpleToggle(
"base.bringToShells", sec, s -> s.getState().isBringToShell(), (s, aBoolean) -> {
var state = s.getState();
state.setBringToShell(aBoolean);
var state = s.getState().toBuilder().bringToShell(aBoolean).build();
s.setState(state);
});