Skip to content

Commit

Permalink
feat(backend): Send an close stream event when then sync process is d…
Browse files Browse the repository at this point in the history
…one to prevent the frontend reconnect.
  • Loading branch information
zhe-zhao committed Apr 23, 2021
1 parent 42aacde commit 1d03f29
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import java.util.concurrent.Executors

data class SynchronizationRecordResponse(val synchronizationTimestamp: Long?)

private const val PROGRESS_UPDATE_EVENT = "PROGRESS_UPDATE_EVENT"
private const val COMPLETE_STREAM_EVENT = "COMPLETE_STREAM_EVENT"

@RestController
class SynchronizationController {
@Autowired
Expand All @@ -34,12 +37,14 @@ class SynchronizationController {
@GetMapping("/api/project/{projectId}/sse-sync")
fun sseSynchronization(@PathVariable projectId: String): SseEmitter {
val emitter = SseEmitter(Companion.SSE_CONNECTION_TIMEOUT)
val emitCb: (SyncProgress) -> Unit = { progress -> emitter.send(SseEmitter.event().data(progress)) }
val emitCb: (SyncProgress) -> Unit =
{ progress -> emitter.send(SseEmitter.event().name(PROGRESS_UPDATE_EVENT).data(progress)) }
val sseMvcExecutor = Executors.newSingleThreadExecutor()

sseMvcExecutor.execute {
try {
synchronizationApplicationService.synchronize(projectId, emitCb)
emitter.send(SseEmitter.event().name(COMPLETE_STREAM_EVENT).data(COMPLETE_STREAM_EVENT))
emitter.complete()
} catch (ex: ApplicationException) {
emitter.completeWithError(ex)
Expand Down

0 comments on commit 1d03f29

Please sign in to comment.