Skip to content

Commit

Permalink
add option to specify default constant value for scale storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ERussel committed Mar 1, 2022
1 parent caf3e00 commit e561cf4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
28 changes: 24 additions & 4 deletions novawallet/Common/Operation/StorageDecodingOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,11 @@ final class StorageConstantOperation<T: Decodable>: BaseOperation<T>, ConstantDe

let path: ConstantCodingPath

init(path: ConstantCodingPath) {
let fallbackValue: T?

init(path: ConstantCodingPath, fallbackValue: T? = nil) {
self.path = path
self.fallbackValue = fallbackValue

super.init()
}
Expand All @@ -260,7 +263,14 @@ final class StorageConstantOperation<T: Decodable>: BaseOperation<T>, ConstantDe
let item: T = try decode(at: path, codingFactory: factory).map(to: T.self)
result = .success(item)
} catch {
result = .failure(error)
if
let storageError = error as? StorageDecodingOperationError,
storageError == .invalidStoragePath,
let fallbackValue = fallbackValue {
result = .success(fallbackValue)
} else {
result = .failure(error)
}
}
}
}
Expand All @@ -270,8 +280,11 @@ final class PrimitiveConstantOperation<T: LosslessStringConvertible & Equatable>

let path: ConstantCodingPath

init(path: ConstantCodingPath) {
let fallbackValue: T?

init(path: ConstantCodingPath, fallbackValue: T? = nil) {
self.path = path
self.fallbackValue = fallbackValue

super.init()
}
Expand All @@ -296,7 +309,14 @@ final class PrimitiveConstantOperation<T: LosslessStringConvertible & Equatable>
.map(to: StringScaleMapper<T>.self)
result = .success(item.value)
} catch {
result = .failure(error)
if
let storageError = error as? StorageDecodingOperationError,
storageError == .invalidStoragePath,
let fallbackValue = fallbackValue {
result = .success(fallbackValue)
} else {
result = .failure(error)
}
}
}
}
40 changes: 38 additions & 2 deletions novawallet/Common/Protocols/RuntimeConstantFetching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ protocol RuntimeConstantFetching {
for path: ConstantCodingPath,
runtimeCodingService: RuntimeCodingServiceProtocol,
operationManager: OperationManagerProtocol,
fallbackValue: T?,
closure: @escaping (Result<T, Error>) -> Void
) -> CancellableCall

func fetchCompoundConstant<T: Decodable>(
for path: ConstantCodingPath,
runtimeCodingService: RuntimeCodingServiceProtocol,
operationManager: OperationManagerProtocol,
fallbackValue: T?,
closure: @escaping (Result<T, Error>) -> Void
) -> CancellableCall
}
Expand All @@ -24,9 +26,42 @@ extension RuntimeConstantFetching {
runtimeCodingService: RuntimeCodingServiceProtocol,
operationManager: OperationManagerProtocol,
closure: @escaping (Result<T, Error>) -> Void
) -> CancellableCall {
fetchConstant(
for: path,
runtimeCodingService: runtimeCodingService,
operationManager: operationManager,
fallbackValue: nil,
closure: closure
)
}

@discardableResult
func fetchCompoundConstant<T: Decodable>(
for path: ConstantCodingPath,
runtimeCodingService: RuntimeCodingServiceProtocol,
operationManager: OperationManagerProtocol,
closure: @escaping (Result<T, Error>) -> Void
) -> CancellableCall {
fetchCompoundConstant(
for: path,
runtimeCodingService: runtimeCodingService,
operationManager: operationManager,
fallbackValue: nil,
closure: closure
)
}

@discardableResult
func fetchConstant<T: LosslessStringConvertible & Equatable>(
for path: ConstantCodingPath,
runtimeCodingService: RuntimeCodingServiceProtocol,
operationManager: OperationManagerProtocol,
fallbackValue: T?,
closure: @escaping (Result<T, Error>) -> Void
) -> CancellableCall {
let codingFactoryOperation = runtimeCodingService.fetchCoderFactoryOperation()
let constOperation = PrimitiveConstantOperation<T>(path: path)
let constOperation = PrimitiveConstantOperation<T>(path: path, fallbackValue: fallbackValue)
constOperation.configurationBlock = {
do {
constOperation.codingFactory = try codingFactoryOperation.extractNoCancellableResultData()
Expand Down Expand Up @@ -57,10 +92,11 @@ extension RuntimeConstantFetching {
for path: ConstantCodingPath,
runtimeCodingService: RuntimeCodingServiceProtocol,
operationManager: OperationManagerProtocol,
fallbackValue: T?,
closure: @escaping (Result<T, Error>) -> Void
) -> CancellableCall {
let codingFactoryOperation = runtimeCodingService.fetchCoderFactoryOperation()
let constOperation = StorageConstantOperation<T>(path: path)
let constOperation = StorageConstantOperation<T>(path: path, fallbackValue: fallbackValue)
constOperation.configurationBlock = {
do {
constOperation.codingFactory = try codingFactoryOperation.extractNoCancellableResultData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ final class SelectValidatorsStartInteractor: RuntimeConstantFetching {

operationManager.enqueue(operations: wrapper.allOperations, in: .transient)
}
}

extension SelectValidatorsStartInteractor: SelectValidatorsStartInteractorInputProtocol {
func setup() {
prepareRecommendedValidatorList()

private func provideMaxNominations() {
fetchConstant(
for: .maxNominations,
runtimeCodingService: runtimeService,
operationManager: operationManager
operationManager: operationManager,
fallbackValue: SubstrateConstants.maxNominations
) { [weak self] (result: Result<Int, Error>) in
self?.presenter.didReceiveMaxNominations(result: result)
}
}
}

extension SelectValidatorsStartInteractor: SelectValidatorsStartInteractorInputProtocol {
func setup() {
prepareRecommendedValidatorList()
provideMaxNominations()
}
}

0 comments on commit e561cf4

Please sign in to comment.