Skip to content

Commit a887ffe

Browse files
committed
Fix issue with app state
1 parent 0aca51d commit a887ffe

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/main/kotlin/failchat/AppStateManager.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ class AppStateManager(private val kodein: DKodein) {
8888
private var state: AppState = SETTINGS
8989

9090
fun startChat(): Unit = lock.withLock {
91-
if (state != SETTINGS) throw IllegalStateException("Expected: $SETTINGS, actual: $state")
91+
if (state != SETTINGS) {
92+
throw IllegalStateException("Expected: $SETTINGS, actual: $state")
93+
}
94+
state = CHAT
9295

9396
val viewersCountLoaders: MutableList<ViewersCountLoader> = ArrayList()
9497
val initializedChatClients: MutableMap<Origin, ChatClient> = enumMap()
@@ -206,7 +209,11 @@ class AppStateManager(private val kodein: DKodein) {
206209
}
207210

208211
fun stopChat(): Unit = lock.withLock {
209-
if (state != CHAT) throw IllegalStateException("Expected: $CHAT, actual: $state")
212+
if (state != CHAT) {
213+
throw IllegalStateException("Expected: $CHAT, actual: $state")
214+
}
215+
state = SETTINGS
216+
210217
reset()
211218

212219
// Save config

src/main/kotlin/failchat/gui/FullGuiEventHandler.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import failchat.chat.ChatMessageSender
66
import failchat.util.LateinitVal
77
import failchat.util.completedFuture
88
import failchat.util.executeWithCatch
9+
import failchat.util.logException
910
import javafx.application.Platform
1011
import org.apache.commons.configuration2.Configuration
1112
import java.util.concurrent.CompletableFuture
@@ -29,9 +30,11 @@ class FullGuiEventHandler(
2930
it.chatFrame.show()
3031
}
3132

32-
guiTransitionFuture.whenCompleteAsync(BiConsumer { _, _ ->
33-
appStateManager.startChat()
34-
}, executor)
33+
guiTransitionFuture
34+
.whenCompleteAsync(BiConsumer { _, _ ->
35+
appStateManager.startChat()
36+
}, executor)
37+
.logException()
3538
}
3639

3740
override fun handleStopChat() {
@@ -40,9 +43,11 @@ class FullGuiEventHandler(
4043
it.settingsFrame.show()
4144
}
4245

43-
guiTransitionFuture.whenCompleteAsync(BiConsumer { _, _ ->
44-
appStateManager.stopChat()
45-
}, executor)
46+
guiTransitionFuture
47+
.whenCompleteAsync(BiConsumer { _, _ ->
48+
appStateManager.stopChat()
49+
}, executor)
50+
.logException()
4651
}
4752

4853
override fun handleShutDown() {

src/main/kotlin/failchat/util/CompletableFutures.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import mu.KotlinLogging
44
import java.time.Duration
55
import java.util.concurrent.CompletableFuture
66
import java.util.concurrent.CompletionException
7+
import java.util.concurrent.CompletionStage
78
import java.util.concurrent.ExecutionException
89
import java.util.concurrent.TimeUnit
910

@@ -36,9 +37,9 @@ fun Throwable.completionCause(): Throwable {
3637

3738
private class NullCompletionCauseException(e: CompletionException) : Exception(e)
3839

39-
fun <T> CompletableFuture<T>.logException() {
40+
fun <T> CompletionStage<T>.logException() {
4041
whenComplete { _, t ->
41-
if (t !== null) logger.error("Unhandled exception from CompletableFuture", t)
42+
if (t !== null) logger.error("Unhandled exception from CompletionStage", t)
4243
}
4344
}
4445

src/main/kotlin/failchat/youtube/YoutubeChatClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class YoutubeChatClient(
6262
if (!statusChanged) {
6363
error("Chat client status: ${atomicStatus.value}")
6464
}
65+
logger.info { "Starting youtube client" }
6566

6667
val job = launch {
6768
val initialParameters = youtubeClient.getNewLiveChatSessionData(videoId)
@@ -109,6 +110,7 @@ class YoutubeChatClient(
109110
}
110111

111112
override fun stop() {
113+
logger.info { "Stopping youtube client" }
112114
cancel()
113115
atomicStatus.value = ChatClientStatus.OFFLINE
114116
}

0 commit comments

Comments
 (0)