Skip to content

Commit

Permalink
remove messages duplicates after app relaunch
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Jul 14, 2022
1 parent d38c8ba commit 4d8e35d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
16 changes: 14 additions & 2 deletions Sources/Chat/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ protocol NetworkInteracting {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> {get}
var responsePublisher: AnyPublisher<ChatResponse, Never> {get}
func respondSuccess(payload: RequestSubscriptionPayload) async throws
func subscribe(topic: String) async throws
func request(_ request: JSONRPCRequest<ChatRequestParams>, topic: String, envelopeType: Envelope.EnvelopeType) async throws
func respond(topic: String, response: JsonRpcResult) async throws
Expand Down Expand Up @@ -59,7 +60,13 @@ class NetworkingInteractor: NetworkInteracting {
try await relayClient.publish(topic: topic, payload: message, tag: .chat)
}

func respondSuccess(payload: RequestSubscriptionPayload) async throws {
let response = JSONRPCResponse<AnyCodable>(id: payload.request.id, result: AnyCodable(true))
try await respond(topic: payload.topic, response: JsonRpcResult.response(response))
}

func respond(topic: String, response: JsonRpcResult) async throws {
_ = try jsonRpcHistory.resolve(response: response)
let message = try serializer.serialize(topic: topic, encodable: response.value)
try await relayClient.publish(topic: topic, payload: message, tag: .chat, prompt: false)
}
Expand All @@ -81,8 +88,13 @@ class NetworkingInteractor: NetworkInteracting {
}

private func handleChatRequest(topic: String, request: JSONRPCRequest<ChatRequestParams>) {
let payload = RequestSubscriptionPayload(topic: topic, request: request)
requestPublisherSubject.send(payload)
do {
try jsonRpcHistory.set(topic: topic, request: request)
let payload = RequestSubscriptionPayload(topic: topic, request: request)
requestPublisherSubject.send(payload)
} catch {
logger.debug(error)
}
}

private func handleJsonRpcResponse(response: JSONRPCResponse<AnyCodable>) {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Chat/ProtocolServices/Common/MessagingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ class MessagingService {
switch subscriptionPayload.request.params {
case .message(var message):
message.topic = subscriptionPayload.topic
handleMessage(message)
handleMessage(message, subscriptionPayload)
default:
return
}
}.store(in: &publishers)
}

private func handleMessage(_ message: Message) {
private func handleMessage(_ message: Message, _ payload: RequestSubscriptionPayload) {
Task(priority: .background) {
try await networkingInteractor.respondSuccess(payload: payload)
await messagesStore.add(message)
logger.debug("Received message")
onMessage?(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class NetworkInteractor: NetworkInteracting {
logger.info("Info: \(error.localizedDescription)")
}
}

private func handleJsonRpcErrorResponse(response: JSONRPCErrorResponse) {
do {
let record = try jsonRpcHistory.resolve(response: JsonRpcResult.error(response))
Expand Down
5 changes: 5 additions & 0 deletions Tests/ChatTests/Mocks/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import WalletConnectUtils
import WalletConnectRelay

class NetworkingInteractorMock: NetworkInteracting {

var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> {
socketConnectionStatusPublisherSubject.eraseToAnyPublisher()
}
Expand Down Expand Up @@ -34,6 +35,10 @@ class NetworkingInteractorMock: NetworkInteracting {

}

func respondSuccess(payload: RequestSubscriptionPayload) async throws {

}

private(set) var subscriptions: [String] = []

func subscribe(topic: String) async throws {
Expand Down

0 comments on commit 4d8e35d

Please sign in to comment.