Skip to content

Commit

Permalink
fix response view did not refresh immediately when a request has been…
Browse files Browse the repository at this point in the history
… started
  • Loading branch information
sunny-chung committed Dec 1, 2024
1 parent 3fd1543 commit 64fcc64
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sunnychung.application.multiplatform.hellohttp.manager

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import com.jayway.jsonpath.JsonPath
import com.sunnychung.application.multiplatform.hellohttp.AppContext
Expand All @@ -25,6 +26,7 @@ import com.sunnychung.application.multiplatform.hellohttp.model.UserResponse
import com.sunnychung.application.multiplatform.hellohttp.network.CallData
import com.sunnychung.application.multiplatform.hellohttp.network.ConnectionStatus
import com.sunnychung.application.multiplatform.hellohttp.network.LiteCallData
import com.sunnychung.application.multiplatform.hellohttp.network.NetworkEvent
import com.sunnychung.application.multiplatform.hellohttp.network.hostFromUrl
import com.sunnychung.application.multiplatform.hellohttp.util.executeWithTimeout
import com.sunnychung.application.multiplatform.hellohttp.util.log
Expand Down Expand Up @@ -260,17 +262,17 @@ class NetworkClientManager : CallDataStore {
requestExampleToCallMapping[requestExampleId]
?.let { callDataMap[it] }

fun getResponseByRequestExampleId(requestExampleId: String) =
fun getResponseByRequestExampleId(requestExampleId: String): UserResponse? =
getCallDataByRequestExampleId(requestExampleId)
?.response

fun getStatusByRequestExampleId(requestExampleId: String) =
fun getStatusByRequestExampleId(requestExampleId: String): ConnectionStatus =
getCallDataByRequestExampleId(requestExampleId)
?.status
?: ConnectionStatus.DISCONNECTED

@Composable
fun subscribeToRequestExampleCall(requestExampleId: String) =
fun subscribeToRequestExampleCall(requestExampleId: String): State<NetworkEvent?>? =
getCallDataByRequestExampleId(requestExampleId)
?.eventsStateFlow
?.onEach { log.d { "callDataUpdates onEach ${it?.event}" } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ abstract class AbstractTransportClient internal constructor(callDataStore: CallD
val lastExchange = data.response.rawExchange.exchanges.lastOrNull()
lastExchange?.consumePayloadBuilder(isComplete = false)
}
data.response.rawExchange.exchanges += RawExchange.Exchange(
instant = it.instant,
direction = RawExchange.Direction.Unspecified,
detail = it.event
)
if (it.event.isNotEmpty()) { // there are some "empty" events not for display
data.response.rawExchange.exchanges += RawExchange.Exchange(
instant = it.instant,
direction = RawExchange.Direction.Unspecified,
detail = it.event
)
}
}
}
.launchIn(CoroutineScope(Dispatchers.IO))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ class SpringWebClientTransportClient(networkClientManager: NetworkClientManager)

log.i { "Request started at ${out.startAt!!.atLocalZoneOffset()}" }

emitEvent(data.id, "") // update the UI

requestBuilder
.awaitExchangeOrNull { resp ->
val response: HttpClientResponse = DefaultClientResponseFieldResponse.get(resp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,16 @@ fun AppContentView() {
}
}
second(minSize = 200.dp) {
callDataUpdates // subscribe to call updates
ResponseViewerView(
response = response?.copy() ?: UserResponse(
id = "-",
requestId = "-",
requestExampleId = "-"
),
response = selectedRequestExampleId
?.let(networkClientManager::getResponseByRequestExampleId)
?: response?.copy()
?: UserResponse(
id = "-",
requestId = "-",
requestExampleId = "-"
),
connectionStatus = selectedRequestExampleId
?.let(networkClientManager::getStatusByRequestExampleId)
?: ConnectionStatus.DISCONNECTED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ fun DurationLabel(modifier: Modifier = Modifier, response: UserResponse, updateT
val startAt = response.startAt ?: return
val timerAt = response.endAt ?: if (connectionStatus.isConnectionActive()) KInstant.now() else return
val duration = timerAt - startAt
updateTime // subscribe to timer update
log.v { "response duration update to $duration" }
val text = if (duration >= KDuration.of(10, KFixedTimeUnit.Second)) {
"${"%.1f".format(duration.toMilliseconds() / 1000.0)} s"
} else {
Expand Down

0 comments on commit 64fcc64

Please sign in to comment.