forked from ob-f/OpenBot
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathControllerToBotEventBus.java
More file actions
42 lines (34 loc) · 1.25 KB
/
ControllerToBotEventBus.java
File metadata and controls
42 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.openbot.env;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.subjects.PublishSubject;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
public final class ControllerToBotEventBus {
private static final Map<String, Disposable> subscribers = new HashMap<>();
private ControllerToBotEventBus() {}
private static final PublishSubject<JSONObject> subject = PublishSubject.create();
public static void emitEvent(JSONObject event) {
subject.onNext(event);
}
public static void subscribe(
String subscriberName,
@NonNull Consumer<? super JSONObject> onNext,
@NonNull Consumer<? super Throwable> onError) {
if (subscribers.containsKey(subscriberName)) {
// This name already subscribed, cannot subscribe multiple times;
return;
}
@NonNull Disposable subscriber = subject.subscribe(onNext, onError);
subscribers.put(subscriberName, subscriber);
}
public static void unsubscribe(String name) {
Disposable subscriber = subscribers.get(name);
if (subscriber != null) {
subscriber.dispose();
subscribers.remove(name);
}
}
}