Implemented gun.map()

This commit is contained in:
ChronosX88 2019-05-09 15:26:09 +04:00
parent 243cc61786
commit fceb33df99
4 changed files with 27 additions and 6 deletions

View File

@ -15,7 +15,8 @@ import java.util.concurrent.ConcurrentHashMap;
public class Dispatcher {
private final Map<String, BaseCompletableFuture<?>> pendingFutures = new ConcurrentHashMap<>();
private final Map<String, NodeChangeListener> changeListeners = new HashMap<>();
private final Map<String, NodeChangeListener> changeListeners = new ConcurrentHashMap<>();
private final Map<String, NodeChangeListener.ForEach> forEachListeners = new ConcurrentHashMap<>();
private final Peer peer;
private final StorageBackend graphStorage;
private final Dup dup;
@ -61,6 +62,11 @@ public class Dispatcher {
if(changeListeners.containsKey(entry.getKey())) {
changeListeners.get(entry.getKey()).onChange(entry.getValue().toUserJSONObject());
}
if(forEachListeners.containsKey(entry.getKey())) {
for(Map.Entry<String, Object> jsonEntry : entry.getValue().values.toMap().entrySet()) {
forEachListeners.get(entry.getKey()).onChange(jsonEntry.getKey(), jsonEntry.getValue());
}
}
}
}
return new JSONObject() // Acknowledgment
@ -105,4 +111,8 @@ public class Dispatcher {
public void addChangeListener(String soul, NodeChangeListener listener) {
changeListeners.put(soul, listener);
}
public void addForEachChangeListener(String soul, NodeChangeListener.ForEach listener) {
forEachListeners.put(soul, listener);
}
}

View File

@ -5,4 +5,8 @@ import org.json.JSONObject;
@FunctionalInterface
public interface NodeChangeListener {
void onChange(JSONObject node);
interface ForEach {
void onChange(String key, Object value);
}
}

View File

@ -99,4 +99,8 @@ public class PathRef {
public void on(NodeChangeListener changeListener) {
dispatcher.addChangeListener(String.join("/", path), changeListener);
}
public void map(NodeChangeListener.ForEach forEachListener) {
dispatcher.addForEachChangeListener(String.join("/", path), forEachListener);
}
}

View File

@ -34,11 +34,14 @@ public class MainClientServer {
}
});
FuturePut futurePut = gun.get("random").get("dVFtzE9CL").put(new JSONObject().put("hello", "world"));
boolean success = futurePut.await();
System.out.println("[FuturePut] Success: " + success);
FuturePut futurePut1 = gun.get("random").get("dVFtzE9CL").put(new JSONObject().put("hello", "123"));
System.out.println("[FuturePut1] Putting an item again: " + futurePut1.await());
gun.get("random").get("dVFtzE9CL").map(((key, value) -> {
System.out.println("[Map] New change in \"random/dVFtzE9CL\"! " + key + " : " + value.toString());
}));
gun.get("random").map(((key, value) -> {
System.out.println("[Map] New change in \"random\"! " + key + " : " + value);
}));
System.out.println("[FuturePut] Success: " + gun.get("random").get("dVFtzE9CL").put(new JSONObject().put("hello", "world")).await());
System.out.println("[FuturePut] Putting an item again: " + gun.get("random").get("dVFtzE9CL").put(new JSONObject().put("hello", "123")).await());
System.out.println("Deleting an item random/dVFtzE9CL");
gun.get("random").get("dVFtzE9CL").put(null).await();
gun.get("random").put(new JSONObject().put("hello", "world")).await();