-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bidi] [java] Ensure the listeners returns an id (#14215)
- Loading branch information
Showing
3 changed files
with
46 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ | |
import java.io.Closeable; | ||
import java.io.StringReader; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -66,11 +65,12 @@ public class Connection implements Closeable { | |
return thread; | ||
}); | ||
private static final AtomicLong NEXT_ID = new AtomicLong(1L); | ||
private final AtomicLong EVENT_CALLBACK_ID = new AtomicLong(1); | ||
private WebSocket socket; | ||
private final Map<Long, Consumer<Either<Throwable, JsonInput>>> methodCallbacks = | ||
new ConcurrentHashMap<>(); | ||
private final ReadWriteLock callbacksLock = new ReentrantReadWriteLock(true); | ||
private final Map<Event<?>, List<Consumer<?>>> eventCallbacks = new HashMap<>(); | ||
private final Map<Event<?>, Map<Long, Consumer<?>>> eventCallbacks = new HashMap<>(); | ||
private final HttpClient client; | ||
private final AtomicBoolean underlyingSocketClosed = new AtomicBoolean(); | ||
|
||
|
@@ -180,17 +180,26 @@ public <X> X sendAndWait(Command<X> command, Duration timeout) { | |
} | ||
} | ||
|
||
public <X> void addListener(Event<X> event, Consumer<X> handler) { | ||
public <X> long addListener(Event<X> event, Consumer<X> handler) { | ||
Require.nonNull("Event to listen for", event); | ||
Require.nonNull("Handler to call", handler); | ||
|
||
long id = EVENT_CALLBACK_ID.getAndIncrement(); | ||
|
||
Lock lock = callbacksLock.writeLock(); | ||
lock.lock(); | ||
try { | ||
eventCallbacks.computeIfAbsent(event, (key) -> new ArrayList<>()).add(handler); | ||
eventCallbacks.computeIfAbsent( | ||
event, | ||
key -> { | ||
HashMap<Long, Consumer<?>> map = new HashMap<>(); | ||
map.put(id, handler); | ||
return map; | ||
}); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
pujagani
Author
Contributor
|
||
} finally { | ||
lock.unlock(); | ||
} | ||
return id; | ||
} | ||
|
||
public <X> void clearListener(Event<X> event) { | ||
|
@@ -203,6 +212,23 @@ public <X> void clearListener(Event<X> event) { | |
} | ||
} | ||
|
||
public void removeListener(long id) { | ||
Lock lock = callbacksLock.writeLock(); | ||
lock.lock(); | ||
try { | ||
eventCallbacks.forEach((k, v) -> v.remove(id)); | ||
eventCallbacks.forEach( | ||
(k, v) -> { | ||
v.remove(id); | ||
if (v.isEmpty()) { | ||
eventCallbacks.remove(k); | ||
} | ||
}); | ||
This comment has been minimized.
Sorry, something went wrong.
joerg1985
Member
|
||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public <X> boolean isEventSubscribed(Event<X> event) { | ||
Lock lock = callbacksLock.writeLock(); | ||
lock.lock(); | ||
|
@@ -354,7 +380,7 @@ private void handleEventResponse(Map<String, Object> rawDataMap) { | |
|
||
final Object finalValue = value; | ||
|
||
for (Consumer<?> action : event.getValue()) { | ||
for (Consumer<?> action : event.getValue().values()) { | ||
@SuppressWarnings("unchecked") | ||
Consumer<Object> obj = (Consumer<Object>) action; | ||
LOG.log( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@pujagani this will only add the first handler to the map.