Skip to content

Commit

Permalink
Unsubscribe from the remote storage (#25)
Browse files Browse the repository at this point in the history
* unsubscribe from remote storage when cancelling subscription request

* bump library version

* fix request call
  • Loading branch information
ERussel authored Jun 14, 2022
1 parent 44635bb commit d4c83eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SubstrateSdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions SubstrateSdk/Classes/Network/RPCMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 35 additions & 0 deletions SubstrateSdk/Classes/Network/WebSocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool, Error>)) in
self?.provideUnsubscriptionResult(result, remoteId: remoteId)
}

updateConnectionForRequest(request)
} catch {
logger?.error("(\(chainName):\(selectedURL)) Failed to create unsubscription request: \(error)")
}
}

func provideUnsubscriptionResult(_ result: (Result<Bool, Error>), 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) {
Expand Down

0 comments on commit d4c83eb

Please sign in to comment.