From 2ff42423d796441a4411ad7143ce63eabbc8a065 Mon Sep 17 00:00:00 2001 From: Joan Martin Date: Mon, 2 Jul 2018 15:26:25 +0200 Subject: [PATCH] Updating podspec. Testing source code. --- Example/MJSwiftCore/AppDelegate.swift | 4 +- .../MJSwiftCore/Core/DI/StorageAssembly.swift | 6 +-- MJSwiftCore.podspec | 2 +- .../Repository/DataSourceRepository.swift | 43 +++---------------- .../Common/Repository/RetryRepository.swift | 12 +++--- .../Classes/Future/Future+Functional.swift | 9 +++- .../Future/Observable+Functional.swift | 9 +++- 7 files changed, 31 insertions(+), 54 deletions(-) diff --git a/Example/MJSwiftCore/AppDelegate.swift b/Example/MJSwiftCore/AppDelegate.swift index 08a1a9ff..f36d3d1f 100644 --- a/Example/MJSwiftCore/AppDelegate.swift +++ b/Example/MJSwiftCore/AppDelegate.swift @@ -11,7 +11,7 @@ import MJSwiftCore import MJCocoaCore /// Creates and returns a new future, which is resolved 2 seconds after -func fut() -> Future { +func future() -> Future { return Future() { resolver in DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: { resolver.set(2) @@ -20,7 +20,7 @@ func fut() -> Future { } /// Creates and returns a new observable, which is triggered 2 seconds after -func obs() -> Observable { +func observable() -> Observable { return Observable() { resolver in DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: { resolver.set(1) diff --git a/Example/MJSwiftCore/Core/DI/StorageAssembly.swift b/Example/MJSwiftCore/Core/DI/StorageAssembly.swift index c5319fc2..38866063 100644 --- a/Example/MJSwiftCore/Core/DI/StorageAssembly.swift +++ b/Example/MJSwiftCore/Core/DI/StorageAssembly.swift @@ -27,7 +27,7 @@ class StorageAssembly: Assembly { func assemble(container: Container) { -// Realm + // Realm container.register(RealmFactory.self) { _ in let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let configuration = Realm.Configuration.init(fileURL: URL(string:"\(documentsPath)/SwiftCore.realm"), @@ -61,12 +61,12 @@ class StorageAssembly: Assembly { }) -// In-Memory key value storage + // In-Memory key value storage // container.register(AnyDataSource.self, name: Names.storageRepository, factory: { r in // return InMemoryDataSource().asAnyDataSource() // }) -// User defaults key value storge + // User defaults key value storge // container.register(AnyDataSource.self, name: Names.storageRepository, factory: { r in // let userDefaultsDataSource = UserDefaultsDataSource(UserDefaults.standard, prefix: "ItemEntity") // let dataSource = DataSourceMapper(dataSource: userDefaultsDataSource, diff --git a/MJSwiftCore.podspec b/MJSwiftCore.podspec index 2fbb094d..599d3489 100644 --- a/MJSwiftCore.podspec +++ b/MJSwiftCore.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'MJSwiftCore' - s.version = '0.6.0' + s.version = '0.7.0' s.summary = 'Mobile Jazz Swift toolkit utilities' # This description is used to generate tags and improve search results. diff --git a/MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift b/MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift index ac0cd2b3..d8205972 100644 --- a/MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift +++ b/MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift @@ -130,80 +130,47 @@ extension DeleteDataSource { /// All repository methods are directly forwarded to a single data source. /// Operation parameter is not used in any case. /// -public class DataSourceRepository : Repository where Get.T == T, Put.T == T, Delete.T == T { +public class DataSourceRepository : Repository where D.T == T { - private let getDataSource : Get? - private let putDataSource : Put? - private let deleteDataSource : Delete? + private let dataSource : D /// Main initializer /// /// - Parameters: - /// - getDataSource: The get data source - /// - putDataSource: The put data source - /// - deleteDataSource: The delete data source - public init(get getDataSource: Get? = nil, put putDataSource: Put? = nil, delete deleteDataSource: Delete? = nil) { - self.getDataSource = getDataSource - self.putDataSource = putDataSource - self.deleteDataSource = deleteDataSource + /// - dataSource: The data source + public init(_ dataSource: D) { + self.dataSource = dataSource } public func get(_ query: Query, operation: Operation = BlankOperation()) -> Future { - guard let dataSource = getDataSource else { - fatalError() - } return dataSource.get(query) } public func getAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<[T]> { - guard let dataSource = getDataSource else { - fatalError() - } return dataSource.getAll(query) } @discardableResult public func put(_ value: T?, in query: Query, operation: Operation = BlankOperation()) -> Future { - guard let dataSource = putDataSource else { - fatalError() - } return dataSource.put(value, in: query) } @discardableResult public func putAll(_ array: [T], in query: Query, operation: Operation = BlankOperation()) -> Future<[T]> { - guard let dataSource = putDataSource else { - fatalError() - } return dataSource.putAll(array, in: query) } @discardableResult public func delete(_ query: Query, operation: Operation = BlankOperation()) -> Future { - guard let dataSource = deleteDataSource else { - fatalError() - } return dataSource.delete(query) } @discardableResult public func deleteAll(_ query: Query, operation: Operation = BlankOperation()) -> Future { - guard let dataSource = deleteDataSource else { - fatalError() - } return dataSource.deleteAll(query) } } -extension DataSourceRepository where Get == Put, Get == Delete { - /// Initializer for a single DataSource - /// - /// - Parameter dataSource: The data source - public convenience init(_ dataSource: Get) { - self.init(get: dataSource, put: dataSource, delete: dataSource) - } -} - extension DataSource { /// Creates a single data source repository from a data source /// diff --git a/MJSwiftCore/Classes/Common/Repository/RetryRepository.swift b/MJSwiftCore/Classes/Common/Repository/RetryRepository.swift index 25eb66f0..b9310d39 100644 --- a/MJSwiftCore/Classes/Common/Repository/RetryRepository.swift +++ b/MJSwiftCore/Classes/Common/Repository/RetryRepository.swift @@ -80,7 +80,7 @@ public class RetryRepository : Repository where T == R.T { if retryOp.canRetry(error) { return self.get(query, operation: retryOp.next()) } else { - return Future(error) + throw error } } default: @@ -95,7 +95,7 @@ public class RetryRepository : Repository where T == R.T { if retryOp.canRetry(error) { return self.getAll(query, operation: retryOp.next()) } else { - return Future(error) + throw error } } default: @@ -111,7 +111,7 @@ public class RetryRepository : Repository where T == R.T { if retryOp.canRetry(error) { return self.put(value, in: query, operation: retryOp.next()) } else { - return Future(error) + throw error } } default: @@ -127,7 +127,7 @@ public class RetryRepository : Repository where T == R.T { if retryOp.canRetry(error) { return self.putAll(array, in: query, operation: retryOp.next()) } else { - return Future(error) + throw error } } default: @@ -143,7 +143,7 @@ public class RetryRepository : Repository where T == R.T { if retryOp.canRetry(error) { return self.delete(query, operation: retryOp.next()) } else { - return Future(error) + throw error } } default: @@ -159,7 +159,7 @@ public class RetryRepository : Repository where T == R.T { if retryOp.canRetry(error) { return self.deleteAll(query, operation: retryOp.next()) } else { - return Future(error) + throw error } } default: diff --git a/MJSwiftCore/Classes/Future/Future+Functional.swift b/MJSwiftCore/Classes/Future/Future+Functional.swift index 5f0464ae..caad7dcd 100644 --- a/MJSwiftCore/Classes/Future/Future+Functional.swift +++ b/MJSwiftCore/Classes/Future/Future+Functional.swift @@ -52,12 +52,17 @@ public extension Future { } /// Intercepts the error (if available) and returns a new future of type T - public func recover(_ closure: @escaping (Error) -> Future) -> Future { + public func recover(_ closure: @escaping (Error) throws -> Future) -> Future { return Future() { resolver in resolve(success: {value in resolver.set(value) }, failure: { error in - resolver.set(closure(error)) + do { + let future = try closure(error) + resolver.set(future) + } catch (let error) { + resolver.set(error) + } }) } } diff --git a/MJSwiftCore/Classes/Future/Observable+Functional.swift b/MJSwiftCore/Classes/Future/Observable+Functional.swift index 55c91440..2e9206a6 100644 --- a/MJSwiftCore/Classes/Future/Observable+Functional.swift +++ b/MJSwiftCore/Classes/Future/Observable+Functional.swift @@ -52,12 +52,17 @@ public extension Observable { } /// Intercepts the error (if available) and returns a new observable of type T - public func recover(_ closure: @escaping (Error) -> Observable) -> Observable { + public func recover(_ closure: @escaping (Error) throws -> Observable) -> Observable { return Observable(parent: self) { resolver in resolve(success: {value in resolver.set(value) }, failure: { error in - resolver.set(closure(error)) + do { + let observable = try closure(error) + resolver.set(observable) + } catch (let error) { + resolver.set(error) + } }) } }