Skip to content

Commit

Permalink
refactor(SSE handling): improve SSE parsing logic #97
Browse files Browse the repository at this point in the history
Refactored the Server Sent Event (SSE) parsing logic to handle edge cases better and make code cleaner. The new approach fixes #16 which involved adding checks on whether incoming events were `"ping":` or just starting with `":"` followed by "ping". It also added functionality to clear chat window before showing new messages while creating a new conversation using NewChatAction component. Made this change because it enhances user experience by providing smoother interaction between server push notifications and client display of those notifications. Modified these files accordingly as per suggestions from SonarLint analysis.
  • Loading branch information
phodal committed Mar 5, 2024
1 parent c839c89 commit 3188b9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/gui/toolbar/NewChatAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ class NewChatAction : DumbAwareAction(), CustomComponentAction {
AutoDevToolWindowFactory.Util.id
)

AutoDevToolWindowFactory().createToolWindowContent(project, toolWindowManager!!)
val contentManager = toolWindowManager?.contentManager
val codingPanel =
contentManager?.component?.components?.filterIsInstance<ChatCodingPanel>()?.firstOrNull()

if (codingPanel == null) {
AutoDevToolWindowFactory().createToolWindowContent(project, toolWindowManager!!)
return@addActionListener
}

codingPanel.clearChat()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
sse = when {
line!!.startsWith("data:") -> {
val data = line!!.substring(5).trim { it <= ' ' }
// data: ping - 2024-03-05 02:07:20.310586
if (data.startsWith("ping") || data.startsWith(" ping")) {
// https://github.com/sysid/sse-starlette/issues/16
// ```
// event: ping
// data: 2020-12-22 15:33:27.463789
// ```
if (data.startsWith(":ping") || data.startsWith(": ping")) {
null
} else {
SSE(data)
Expand All @@ -84,7 +88,14 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
}
// starts with event:
line!!.startsWith("event:") -> {
// do nothing
// https://github.com/sysid/sse-starlette/issues/16
val eventName = line!!.substring(6).trim { it <= ' ' }
if (eventName == "ping") {
// skip ping event and data
emitter.onNext(sse)
emitter.onNext(sse)
}

null
}

Expand Down

0 comments on commit 3188b9e

Please sign in to comment.