mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-10-01 09:40:35 +13:00
Improve storage update propagation
This commit is contained in:
parent
fe7586cbec
commit
d000bc89c8
3 changed files with 41 additions and 2 deletions
|
@ -57,7 +57,7 @@ public class StoreSection implements StorageFilter.Filterable {
|
|||
storeEntrySection -> storeEntrySection.wrapper.getEntry().getName());
|
||||
|
||||
public static StoreSection createTopLevel() {
|
||||
var topLevel = BindingsHelper.mappedContentBinding(
|
||||
var topLevel = BindingsHelper.cachedMappedContentBinding(
|
||||
StoreViewState.get().getAllEntries(), storeEntryWrapper -> create(storeEntryWrapper, 1));
|
||||
var filtered = BindingsHelper.filteredContentBinding(topLevel, section -> {
|
||||
return DataStorage.get()
|
||||
|
@ -80,7 +80,7 @@ public class StoreSection implements StorageFilter.Filterable {
|
|||
.map(found -> found.equals(e.getEntry()))
|
||||
.orElse(false);
|
||||
});
|
||||
var children = BindingsHelper.mappedContentBinding(filtered, entry1 -> create(entry1, depth + 1));
|
||||
var children = BindingsHelper.cachedMappedContentBinding(filtered, entry1 -> create(entry1, depth + 1));
|
||||
var ordered = BindingsHelper.orderedContentBinding(children, COMPARATOR);
|
||||
return new StoreSection(e, ordered, depth);
|
||||
}
|
||||
|
|
|
@ -119,6 +119,29 @@ public class BindingsHelper {
|
|||
return l1;
|
||||
}
|
||||
|
||||
public static <T, V> ObservableList<T> cachedMappedContentBinding(ObservableList<V> l2, Function<V, T> map) {
|
||||
var cache = new HashMap<V, T>();
|
||||
|
||||
ObservableList<T> l1 = FXCollections.observableList(new ArrayList<>());
|
||||
Runnable runnable = () -> {
|
||||
cache.keySet().removeIf(t -> !l2.contains(t));
|
||||
setContent(l1, l2.stream()
|
||||
.map(v -> {
|
||||
if (!cache.containsKey(v)) {
|
||||
cache.put(v, map.apply(v));
|
||||
}
|
||||
|
||||
return cache.get(v);
|
||||
}).toList());
|
||||
};
|
||||
runnable.run();
|
||||
l2.addListener((ListChangeListener<? super V>) c -> {
|
||||
runnable.run();
|
||||
});
|
||||
linkPersistently(l2, l1);
|
||||
return l1;
|
||||
}
|
||||
|
||||
public static <V> ObservableList<V> orderedContentBinding(ObservableList<V> l2, Comparator<V> comp) {
|
||||
ObservableList<V> l1 = FXCollections.observableList(new ArrayList<>());
|
||||
Runnable runnable = () -> {
|
||||
|
|
|
@ -16,6 +16,7 @@ import lombok.NonNull;
|
|||
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class DataStorage {
|
||||
|
||||
|
@ -329,11 +330,26 @@ public abstract class DataStorage {
|
|||
}
|
||||
|
||||
public void updateEntry(DataStoreEntry entry, DataStoreEntry newEntry) {
|
||||
var oldParent = DataStorage.get().getParent(entry, false);
|
||||
var newParent = DataStorage.get().getParent(newEntry, false);
|
||||
|
||||
propagateUpdate(
|
||||
() -> {
|
||||
newEntry.finalizeEntry();
|
||||
|
||||
var children = getStoreChildren(entry, false, true);
|
||||
if (!Objects.equals(oldParent, newParent)) {
|
||||
var toRemove = Stream.concat(Stream.of(entry), children.stream()).toArray(DataStoreEntry[]::new);
|
||||
listeners.forEach(storageListener -> storageListener.onStoreRemove(toRemove));
|
||||
}
|
||||
|
||||
entry.applyChanges(newEntry);
|
||||
entry.initializeEntry();
|
||||
|
||||
if (!Objects.equals(oldParent, newParent)) {
|
||||
var toAdd = Stream.concat(Stream.of(entry), children.stream()).toArray(DataStoreEntry[]::new);
|
||||
listeners.forEach(storageListener -> storageListener.onStoreAdd(toAdd));
|
||||
}
|
||||
},
|
||||
entry);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue