mirror of
https://github.com/ChronosX88/JGUN.git
synced 2024-11-23 23:12:18 +00:00
Implemented gun.map()
This commit is contained in:
parent
243cc61786
commit
fceb33df99
@ -15,7 +15,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
|
|
||||||
public class Dispatcher {
|
public class Dispatcher {
|
||||||
private final Map<String, BaseCompletableFuture<?>> pendingFutures = new ConcurrentHashMap<>();
|
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 Peer peer;
|
||||||
private final StorageBackend graphStorage;
|
private final StorageBackend graphStorage;
|
||||||
private final Dup dup;
|
private final Dup dup;
|
||||||
@ -61,6 +62,11 @@ public class Dispatcher {
|
|||||||
if(changeListeners.containsKey(entry.getKey())) {
|
if(changeListeners.containsKey(entry.getKey())) {
|
||||||
changeListeners.get(entry.getKey()).onChange(entry.getValue().toUserJSONObject());
|
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
|
return new JSONObject() // Acknowledgment
|
||||||
@ -105,4 +111,8 @@ public class Dispatcher {
|
|||||||
public void addChangeListener(String soul, NodeChangeListener listener) {
|
public void addChangeListener(String soul, NodeChangeListener listener) {
|
||||||
changeListeners.put(soul, listener);
|
changeListeners.put(soul, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addForEachChangeListener(String soul, NodeChangeListener.ForEach listener) {
|
||||||
|
forEachListeners.put(soul, listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,4 +5,8 @@ import org.json.JSONObject;
|
|||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface NodeChangeListener {
|
public interface NodeChangeListener {
|
||||||
void onChange(JSONObject node);
|
void onChange(JSONObject node);
|
||||||
|
|
||||||
|
interface ForEach {
|
||||||
|
void onChange(String key, Object value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,4 +99,8 @@ public class PathRef {
|
|||||||
public void on(NodeChangeListener changeListener) {
|
public void on(NodeChangeListener changeListener) {
|
||||||
dispatcher.addChangeListener(String.join("/", path), changeListener);
|
dispatcher.addChangeListener(String.join("/", path), changeListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void map(NodeChangeListener.ForEach forEachListener) {
|
||||||
|
dispatcher.addForEachChangeListener(String.join("/", path), forEachListener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,11 +34,14 @@ public class MainClientServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
FuturePut futurePut = gun.get("random").get("dVFtzE9CL").put(new JSONObject().put("hello", "world"));
|
gun.get("random").get("dVFtzE9CL").map(((key, value) -> {
|
||||||
boolean success = futurePut.await();
|
System.out.println("[Map] New change in \"random/dVFtzE9CL\"! " + key + " : " + value.toString());
|
||||||
System.out.println("[FuturePut] Success: " + success);
|
}));
|
||||||
FuturePut futurePut1 = gun.get("random").get("dVFtzE9CL").put(new JSONObject().put("hello", "123"));
|
gun.get("random").map(((key, value) -> {
|
||||||
System.out.println("[FuturePut1] Putting an item again: " + futurePut1.await());
|
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");
|
System.out.println("Deleting an item random/dVFtzE9CL");
|
||||||
gun.get("random").get("dVFtzE9CL").put(null).await();
|
gun.get("random").get("dVFtzE9CL").put(null).await();
|
||||||
gun.get("random").put(new JSONObject().put("hello", "world")).await();
|
gun.get("random").put(new JSONObject().put("hello", "world")).await();
|
||||||
|
Loading…
Reference in New Issue
Block a user