Skip to content

Commit

Permalink
Merge pull request #56 from nova-wallet/develop
Browse files Browse the repository at this point in the history
v1.8.0
  • Loading branch information
valentunn authored Mar 31, 2023
2 parents 0de27ac + cb054bc commit ba00d6e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
// App version
versionName = '1.7.3'
versionName = '1.8.0'
versionCode = 1

// SDK and tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.google.gson.Gson
import com.neovisionaries.ws.client.WebSocketFactory
import com.neovisionaries.ws.client.WebSocketState
import jp.co.soramitsu.fearless_utils.wsrpc.exception.ConnectionClosedException
import jp.co.soramitsu.fearless_utils.wsrpc.interceptor.WebSocketResponseInterceptor
import jp.co.soramitsu.fearless_utils.wsrpc.interceptor.shouldDeliver
import jp.co.soramitsu.fearless_utils.wsrpc.logging.Logger
import jp.co.soramitsu.fearless_utils.wsrpc.mappers.nonNull
import jp.co.soramitsu.fearless_utils.wsrpc.mappers.stringIdMapper
Expand Down Expand Up @@ -31,13 +33,15 @@ class SocketService(
private val logger: Logger,
private val webSocketFactory: WebSocketFactory,
private val reconnector: Reconnector,
private val requestExecutor: RequestExecutor
private val requestExecutor: RequestExecutor,
) : RpcSocketListener {

private var socket: RpcSocket? = null

private val stateContainer = ObservableState(initialState = State.Disconnected)

private var responseInterceptor: WebSocketResponseInterceptor? = null

fun started() = stateContainer.getState() !is State.Disconnected

/**
Expand Down Expand Up @@ -81,6 +85,11 @@ class SocketService(
updateState(Event.Pause)
}

@Synchronized
fun setInterceptor(interceptor: WebSocketResponseInterceptor) {
this.responseInterceptor = interceptor
}

/**
* Resumes connection from [SocketStateMachine.State.Paused] state,
* recovering all subscriptions and [DeliveryType.AT_LEAST_ONCE] requests
Expand Down Expand Up @@ -176,15 +185,33 @@ class SocketService(

@Synchronized
override fun onSingleResponse(rpcResponse: RpcResponse) {
updateState(Event.SendableResponse(rpcResponse))
val interceptor = responseInterceptor

if (
interceptor != null &&
interceptor.onRpcResponseReceived(rpcResponse).shouldDeliver()
) {
updateState(Event.SendableResponse(rpcResponse))
}
}

override fun onSubscriptionResponse(subscriptionChange: SubscriptionChange) {
updateState(Event.SubscriptionResponse(subscriptionChange))
}

@Synchronized
override fun onBatchResponse(batchResponse: List<RpcResponse>) {
updateState(Event.SendableBatchResponse(batchResponse))
val interceptor = responseInterceptor

val toDeliver = if (interceptor != null) {
batchResponse.filter { interceptor.onRpcResponseReceived(it).shouldDeliver() }
} else {
batchResponse
}

if (toDeliver.isNotEmpty()) {
updateState(Event.SendableBatchResponse(batchResponse))
}
}

@Synchronized
Expand Down Expand Up @@ -289,7 +316,8 @@ class SocketService(
private fun unsubscribe(subscription: SocketStateMachine.Subscription) {
require(subscription is RespondableSubscription)

val unsubscribeRequest = RuntimeRequest(subscription.unsubscribeMethod, listOf(subscription.id))
val unsubscribeRequest =
RuntimeRequest(subscription.unsubscribeMethod, listOf(subscription.id))

executeRequest(
unsubscribeRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jp.co.soramitsu.fearless_utils.wsrpc.interceptor

import jp.co.soramitsu.fearless_utils.wsrpc.response.RpcResponse

interface WebSocketResponseInterceptor {

enum class ResponseDelivery {
DROP, DELIVER_TO_SENDER
}

fun onRpcResponseReceived(rpcResponse: RpcResponse): ResponseDelivery
}

fun WebSocketResponseInterceptor.ResponseDelivery.shouldDeliver(): Boolean {
return this == WebSocketResponseInterceptor.ResponseDelivery.DELIVER_TO_SENDER
}

0 comments on commit ba00d6e

Please sign in to comment.