Commit 2834c18e authored by Benjamin Beeker's avatar Benjamin Beeker

multiple chats per api

parent dc245990
Pipeline #10869141 passed with stages
in 1 minute and 48 seconds
......@@ -18,6 +18,9 @@ import okhttp3.Request;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Maps.newHashMap;
/**
* Main entry-point to use the Twitch-Api
......@@ -25,21 +28,19 @@ import java.util.List;
public class TwitchApi {
private final ClientInformation clientInformation;
private final String oauthToken;
private final String chatChannel;
private ChatProvider chatProvider;
private OkHttpClient client = new OkHttpClient.Builder().build();
private boolean closed = false;
private final Map<String, ChatProvider> chatProviders = newHashMap();
/**
* Creates a new instance for already connected user
*/
public TwitchApi(ClientInformation clientInformation, String oauthToken, String chatChannel) {
public TwitchApi(ClientInformation clientInformation, String oauthToken) {
this.clientInformation = Preconditions.checkNotNull(clientInformation);
this.oauthToken = Preconditions.checkNotNull(oauthToken);
this.chatChannel = Preconditions.checkNotNull(chatChannel);
}
/**
......@@ -76,7 +77,7 @@ public class TwitchApi {
public void close() {
closed = true;
chatProvider.close();
chatProviders.values().forEach(ChatProvider::close);
}
private void checkClosed() {
......@@ -88,12 +89,15 @@ public class TwitchApi {
/**
* Provides the {@link ChatProvider} where you can send messages or register for incoming messages and events
*/
public Observable<ChatProvider> getChatProvider() {
public Observable<ChatProvider> getChatProvider(String channel) {
checkClosed();
ChatProvider chatProvider = chatProviders.get(channel);
if (chatProvider == null) {
return getOwnInfo().map(ownInfo -> {
chatProvider = new ChatProvider(oauthToken, ownInfo.getName(), chatChannel);
return chatProvider;
synchronized (chatProviders) {
return chatProviders.computeIfAbsent(channel, ignored -> new ChatProvider(oauthToken, ownInfo.getName(), channel));
}
});
}
return Observable.just(chatProvider);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment