Skip to content

Commit 4c856a6

Browse files
committed
modify recent valve type
1 parent 38c72d1 commit 4c856a6

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

thunder-stomp/src/main/java/com/jeremy/thunder/stomp/internal/StompStateManager.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import com.jeremy.thunder.thunder_internal.event.StompSendRequest
2121
import com.jeremy.thunder.thunder_internal.event.StompSubscribeRequest
2222
import com.jeremy.thunder.thunder_internal.event.ThunderRequest
2323
import com.jeremy.thunder.thunder_internal.stateDelegate
24+
import com.jeremy.thunder.thunder_state.Active
2425
import com.jeremy.thunder.thunder_state.ConnectState
26+
import com.jeremy.thunder.thunder_state.NetworkState
2527
import com.jeremy.thunder.thunder_state.WebSocketEvent
2628
import kotlinx.coroutines.CoroutineExceptionHandler
2729
import kotlinx.coroutines.CoroutineScope
@@ -98,7 +100,7 @@ class StompStateManager private constructor(
98100
private suspend fun checkOnValidState(): Boolean = withContext(Dispatchers.Default) {
99101
val appState = connectionListener.collectAppState().firstOrNull()
100102
val networkState = networkState.networkStatus.firstOrNull()
101-
appState == com.jeremy.thunder.thunder_state.Active && networkState == com.jeremy.thunder.thunder_state.NetworkState.Available
103+
appState == Active && networkState == NetworkState.Available
102104
}
103105

104106
private suspend fun connectionRecoveryProcess(onConnect: () -> Unit) {
@@ -125,7 +127,6 @@ class StompStateManager private constructor(
125127
}
126128

127129
private fun requestExecute(message: ThunderRequest) = innerScope.launch(Dispatchers.IO) {
128-
println("execute = $message")
129130
when (message.typeOfRequest) {
130131
RequestType.STOMP_SUBSCRIBE -> {
131132
val request = message as StompSubscribeRequest
@@ -212,7 +213,6 @@ class StompStateManager private constructor(
212213
}
213214

214215
override fun send(message: ThunderRequest) {
215-
println("send = $message")
216216
val result = when (message.typeOfRequest) {
217217
RequestType.STOMP_SEND -> message as StompSendRequest
218218
RequestType.STOMP_SUBSCRIBE -> message as StompSubscribeRequest
@@ -228,6 +228,7 @@ class StompStateManager private constructor(
228228
socket?.let { websocket ->
229229
runCatching {
230230
val uuid = UUID.randomUUID().toString()
231+
headerIdStore.put(topic, uuid)
231232
websocket.send(
232233
compileMessage(
233234
thunderStompRequest {
@@ -240,11 +241,7 @@ class StompStateManager private constructor(
240241
this.payload = payload
241242
}
242243
)
243-
).apply {
244-
if (this) {
245-
headerIdStore.put(topic, uuid)
246-
}
247-
}
244+
)
248245
}
249246
}
250247
}

thunder/src/main/java/com/jeremy/thunder/cache/ValveCache.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.jeremy.thunder.cache
22

33
import com.jeremy.thunder.thunder_internal.cache.BaseValve
44
import com.jeremy.thunder.thunder_internal.event.ThunderRequest
5+
import com.jeremy.thunder.thunder_state.ConnectState
56
import kotlinx.coroutines.currentCoroutineContext
67
import kotlinx.coroutines.delay
78
import kotlinx.coroutines.flow.Flow
@@ -32,20 +33,27 @@ class ValveCache: BaseValve<ThunderRequest> {
3233
}
3334
emit(emitCacheList)
3435
}
35-
delay(500)
36+
delay(CACHE_EMIT_GAP)
3637
}
3738
}
3839

39-
override fun onUpdateValve(state: com.jeremy.thunder.thunder_state.ConnectState) {
40-
isEmissiable.update { state is com.jeremy.thunder.thunder_state.ConnectState.Establish }
40+
override fun onUpdateValve(state: ConnectState) {
41+
isEmissiable.update { state is ConnectState.Establish }
4142
}
4243

4344
override fun requestToValve(request: ThunderRequest) {
44-
innerQueue.add(request)
45+
val lastRequest = innerQueue.poll()
46+
if (lastRequest != request) {
47+
innerQueue.add(request)
48+
}
4549
}
4650

4751
override fun emissionOfValveFlow(): Flow<List<ThunderRequest>> = cacheFlow()
4852

53+
companion object {
54+
private const val CACHE_EMIT_GAP = 500L
55+
}
56+
4957
class Factory: BaseValve.BaseValveFactory<ThunderRequest> {
5058
override fun create(): ValveCache {
5159
return ValveCache()

thunder/src/main/java/com/jeremy/thunder/internal/ReceivePipeline.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class ReceivePipeline<T>(
1616
scope: CoroutineScope
1717
) {
1818
init {
19-
socketEventFlow.onEach {
20-
store(it)
21-
}.launchIn(scope)
19+
socketEventFlow.onEach(::store).launchIn(scope)
2220
}
2321

2422
private val _cache = Channel<T>(capacity = 100, onBufferOverflow = BufferOverflow.DROP_OLDEST)

thunder/src/main/java/com/jeremy/thunder/internal/ServiceExecutor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import java.lang.reflect.Type
2727
* create logic by annotation
2828
* @Receive - create pipeline flow for observer
2929
* @Send - create send data
30-
* @Subscribe - create subscribe for stomp
30+
* @StompSubscribe - create subscribe for stomp
31+
* @StompSend - create send data for stomp
3132
* */
3233

3334
class ServiceExecutor internal constructor(

thunder/src/main/java/com/jeremy/thunder/internal/ThunderProvider.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ class ThunderProvider internal constructor(
1717
) {
1818

1919
init {
20-
stateManager.collectWebSocketEvent().onEach {
21-
eventProcessor.onEventDelivery(it)
22-
}.launchIn(scope)
20+
stateManager.collectWebSocketEvent().onEach(eventProcessor::onEventDelivery).launchIn(scope)
2321
}
2422

25-
fun observeEvent(): Flow<WebSocketEvent> {
26-
return eventProcessor.collectEvent()
27-
}
23+
fun observeEvent(): Flow<WebSocketEvent> = eventProcessor.collectEvent()
2824

2925
fun send(request: ThunderRequest) {
3026
stateManager.send(request)

thunder/src/main/java/com/jeremy/thunder/internal/ThunderStateManager.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.jeremy.thunder.thunder_state.WebSocketEvent
2121
import kotlinx.coroutines.CoroutineExceptionHandler
2222
import kotlinx.coroutines.CoroutineScope
2323
import kotlinx.coroutines.Dispatchers
24-
import kotlinx.coroutines.ExperimentalCoroutinesApi
2524
import kotlinx.coroutines.flow.Flow
2625
import kotlinx.coroutines.flow.MutableSharedFlow
2726
import kotlinx.coroutines.flow.MutableStateFlow

0 commit comments

Comments
 (0)