diff --git a/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java b/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java index d2dc6d2..dafaa7b 100644 --- a/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java +++ b/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java @@ -15,7 +15,8 @@ import java.util.concurrent.ConcurrentHashMap; public class Dispatcher { private final Map> pendingFutures = new ConcurrentHashMap<>(); - private final Map changeListeners = new HashMap<>(); + private final Map changeListeners = new ConcurrentHashMap<>(); + private final Map 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 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); + } } diff --git a/src/main/java/io/github/chronosx88/JGUN/NodeChangeListener.java b/src/main/java/io/github/chronosx88/JGUN/NodeChangeListener.java index dba0523..3c2a8c5 100644 --- a/src/main/java/io/github/chronosx88/JGUN/NodeChangeListener.java +++ b/src/main/java/io/github/chronosx88/JGUN/NodeChangeListener.java @@ -5,4 +5,8 @@ import org.json.JSONObject; @FunctionalInterface public interface NodeChangeListener { void onChange(JSONObject node); + + interface ForEach { + void onChange(String key, Object value); + } } diff --git a/src/main/java/io/github/chronosx88/JGUN/PathRef.java b/src/main/java/io/github/chronosx88/JGUN/PathRef.java index 9e5981e..da64844 100644 --- a/src/main/java/io/github/chronosx88/JGUN/PathRef.java +++ b/src/main/java/io/github/chronosx88/JGUN/PathRef.java @@ -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); + } } diff --git a/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java b/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java index 903ea15..b397080 100644 --- a/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java +++ b/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java @@ -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();