Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Updating podspec. Testing source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Martin committed Jul 2, 2018
1 parent 8ebe047 commit 2ff4242
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 54 deletions.
4 changes: 2 additions & 2 deletions Example/MJSwiftCore/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import MJSwiftCore
import MJCocoaCore

/// Creates and returns a new future, which is resolved 2 seconds after
func fut() -> Future<Int> {
func future() -> Future<Int> {
return Future() { resolver in
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: {
resolver.set(2)
Expand All @@ -20,7 +20,7 @@ func fut() -> Future<Int> {
}

/// Creates and returns a new observable, which is triggered 2 seconds after
func obs() -> Observable<Int> {
func observable() -> Observable<Int> {
return Observable<Int>() { resolver in
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: {
resolver.set(1)
Expand Down
6 changes: 3 additions & 3 deletions Example/MJSwiftCore/Core/DI/StorageAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -61,12 +61,12 @@ class StorageAssembly: Assembly {
})


// In-Memory key value storage
// In-Memory key value storage
// container.register(AnyDataSource<ItemEntity>.self, name: Names.storageRepository, factory: { r in
// return InMemoryDataSource<ItemEntity>().asAnyDataSource()
// })

// User defaults key value storge
// User defaults key value storge
// container.register(AnyDataSource<ItemEntity>.self, name: Names.storageRepository, factory: { r in
// let userDefaultsDataSource = UserDefaultsDataSource<Data>(UserDefaults.standard, prefix: "ItemEntity")
// let dataSource = DataSourceMapper(dataSource: userDefaultsDataSource,
Expand Down
2 changes: 1 addition & 1 deletion MJSwiftCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 5 additions & 38 deletions MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Get: GetDataSource,Put: PutDataSource,Delete: DeleteDataSource,T> : Repository where Get.T == T, Put.T == T, Delete.T == T {
public class DataSourceRepository<D: DataSource,T> : 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<T> {
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<T> {
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<Void> {
guard let dataSource = deleteDataSource else {
fatalError()
}
return dataSource.delete(query)
}

@discardableResult
public func deleteAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> {
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
///
Expand Down
12 changes: 6 additions & 6 deletions MJSwiftCore/Classes/Common/Repository/RetryRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class RetryRepository <R: Repository,T> : Repository where T == R.T {
if retryOp.canRetry(error) {
return self.get(query, operation: retryOp.next())
} else {
return Future(error)
throw error
}
}
default:
Expand All @@ -95,7 +95,7 @@ public class RetryRepository <R: Repository,T> : Repository where T == R.T {
if retryOp.canRetry(error) {
return self.getAll(query, operation: retryOp.next())
} else {
return Future(error)
throw error
}
}
default:
Expand All @@ -111,7 +111,7 @@ public class RetryRepository <R: Repository,T> : 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:
Expand All @@ -127,7 +127,7 @@ public class RetryRepository <R: Repository,T> : 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:
Expand All @@ -143,7 +143,7 @@ public class RetryRepository <R: Repository,T> : Repository where T == R.T {
if retryOp.canRetry(error) {
return self.delete(query, operation: retryOp.next())
} else {
return Future(error)
throw error
}
}
default:
Expand All @@ -159,7 +159,7 @@ public class RetryRepository <R: Repository,T> : Repository where T == R.T {
if retryOp.canRetry(error) {
return self.deleteAll(query, operation: retryOp.next())
} else {
return Future(error)
throw error
}
}
default:
Expand Down
9 changes: 7 additions & 2 deletions MJSwiftCore/Classes/Future/Future+Functional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>) -> Future<T> {
public func recover(_ closure: @escaping (Error) throws -> Future<T>) -> Future<T> {
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)
}
})
}
}
Expand Down
9 changes: 7 additions & 2 deletions MJSwiftCore/Classes/Future/Observable+Functional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>) -> Observable<T> {
public func recover(_ closure: @escaping (Error) throws -> Observable<T>) -> Observable<T> {
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)
}
})
}
}
Expand Down

0 comments on commit 2ff4242

Please sign in to comment.