parent
84889bcbb1
commit
efa30da462
@ -0,0 +1,48 @@ |
||||
// |
||||
// Session+Publishers.swift |
||||
// AlphaWallet |
||||
// |
||||
// Created by Vladyslav Shepitko on 10.05.2022. |
||||
// |
||||
|
||||
import Foundation |
||||
import APIKit |
||||
import JSONRPCKit |
||||
import Combine |
||||
|
||||
extension Session { |
||||
|
||||
class func sendPublisher<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue? = nil) -> AnyPublisher<Request.Response, SessionTaskError> { |
||||
sendImplPublisher(request, callbackQueue: callbackQueue) |
||||
.retry(times: 2, when: { |
||||
guard case SessionTaskError.requestError(let e) = $0 else { return false } |
||||
return e is SendTransactionRetryableError |
||||
}) |
||||
.eraseToAnyPublisher() |
||||
} |
||||
|
||||
private class func sendImplPublisher<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue? = nil) -> AnyPublisher<Request.Response, SessionTaskError> { |
||||
var sessionTask: SessionTask? |
||||
let publisher = Deferred { |
||||
Future<Request.Response, SessionTaskError> { seal in |
||||
sessionTask = Session.send(request, callbackQueue: callbackQueue) { result in |
||||
switch result { |
||||
case .success(let result): |
||||
seal(.success(result)) |
||||
case .failure(let error): |
||||
if let e = convertToUserFriendlyError(error: error, baseUrl: request.baseURL) { |
||||
seal(.failure(.requestError(e))) |
||||
} else { |
||||
seal(.failure(error)) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
}.handleEvents(receiveCancel: { |
||||
sessionTask?.cancel() |
||||
}) |
||||
|
||||
return publisher |
||||
.eraseToAnyPublisher() |
||||
} |
||||
} |
Loading…
Reference in new issue