From d4c83ebb419bc7b39f5e3a351cf7e8c8ccf07f54 Mon Sep 17 00:00:00 2001 From: Ruslan Date: Tue, 14 Jun 2022 11:44:44 +0300 Subject: [PATCH] Unsubscribe from the remote storage (#25) * unsubscribe from remote storage when cancelling subscription request * bump library version * fix request call --- SubstrateSdk.podspec | 2 +- SubstrateSdk/Classes/Network/RPCMethod.swift | 1 + .../Classes/Network/WebSocketEngine.swift | 35 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/SubstrateSdk.podspec b/SubstrateSdk.podspec index a0c232d..1fb13a5 100644 --- a/SubstrateSdk.podspec +++ b/SubstrateSdk.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'SubstrateSdk' - s.version = '1.1.0' + s.version = '1.2.0' s.summary = 'Utility library that implements clients specific logic to interact with substrate based networks' s.homepage = 'https://github.com/nova-wallet/substrate-sdk-ios' diff --git a/SubstrateSdk/Classes/Network/RPCMethod.swift b/SubstrateSdk/Classes/Network/RPCMethod.swift index 4371e99..9422f83 100644 --- a/SubstrateSdk/Classes/Network/RPCMethod.swift +++ b/SubstrateSdk/Classes/Network/RPCMethod.swift @@ -2,6 +2,7 @@ import Foundation public enum RPCMethod { public static let storageSubscribe = "state_subscribeStorage" + public static let storageUnsubscribe = "state_unsubscribeStorage" public static let chain = "system_chain" public static let getStorage = "state_getStorage" public static let getStorageKeysPaged = "state_getKeysPaged" diff --git a/SubstrateSdk/Classes/Network/WebSocketEngine.swift b/SubstrateSdk/Classes/Network/WebSocketEngine.swift index 6fa8be6..da14a12 100644 --- a/SubstrateSdk/Classes/Network/WebSocketEngine.swift +++ b/SubstrateSdk/Classes/Network/WebSocketEngine.swift @@ -443,6 +443,41 @@ extension WebSocketEngine { error: JSONRPCEngineError.clientCancelled ) } + + // check whether there is subscription for this id and send unsubscribe request + + if let subscription = subscriptions[identifier], let remoteId = subscription.remoteId { + unsubscribe(for: remoteId) + } + + subscriptions[identifier] = nil + } + + func unsubscribe(for remoteId: String) { + pendingSubscriptionResponses[remoteId] = nil + + do { + let request = try prepareRequest( + method: RPCMethod.storageUnsubscribe, + params: [remoteId], + options: JSONRPCOptions() + ) { [weak self] (result: (Result)) in + self?.provideUnsubscriptionResult(result, remoteId: remoteId) + } + + updateConnectionForRequest(request) + } catch { + logger?.error("(\(chainName):\(selectedURL)) Failed to create unsubscription request: \(error)") + } + } + + func provideUnsubscriptionResult(_ result: (Result), remoteId: String) { + switch result { + case let .success(isSuccess): + logger?.debug("(\(chainName):\(selectedURL)) Unsubscription request completed \(remoteId): \(isSuccess)") + case let .failure(error): + logger?.error("(\(chainName):\(selectedURL)) Unsubscription request failed \(remoteId): \(error)") + } } func completeRequestForRemoteId(_ identifier: UInt16, data: Data) {