Reactive operations also support throwing errors

This commit is contained in:
Pedro Piñera Buendía 2016-04-05 10:13:30 +02:00
parent da673fa8d5
commit a6dcb234c1
2 changed files with 21 additions and 21 deletions

View File

@ -6,11 +6,11 @@ public extension Storage {
// MARK: - Operation
func rac_operation(op: (context: Context, save: () -> Void) -> Void) -> SignalProducer<Void, Error> {
func rac_operation(op: (context: Context, save: () -> Void) throws -> Void) -> SignalProducer<Void, Error> {
return SignalProducer { (observer, disposable) in
do {
try self.operation { (context, saver) in
op(context: context, save: {
try self.operation { (context, saver) throws in
try op(context: context, save: {
saver()
})
observer.sendCompleted()
@ -22,20 +22,20 @@ public extension Storage {
}
}
func rac_operation(op: (context: Context) -> Void) -> SignalProducer<Void, Error> {
return self.rac_operation { (context, saver) in
op(context: context)
func rac_operation(op: (context: Context) throws -> Void) -> SignalProducer<Void, Error> {
return self.rac_operation { (context, saver) throws in
try op(context: context)
saver()
}
}
func rac_backgroundOperation(op: (context: Context, save: () -> Void) -> Void) -> SignalProducer<Void, Error> {
func rac_backgroundOperation(op: (context: Context, save: () -> Void) throws -> Void) -> SignalProducer<Void, Error> {
return SignalProducer { (observer, disposable) in
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
do {
try self.operation { (context, saver) in
op(context: context, save: {
try self.operation { (context, saver) throws in
try op(context: context, save: {
saver()
})
observer.sendCompleted()
@ -48,9 +48,9 @@ public extension Storage {
}
}
func rac_backgroundOperation(op: (context: Context) -> Void) -> SignalProducer<Void, Error> {
return rac_backgroundOperation { (context, save) in
op(context: context)
func rac_backgroundOperation(op: (context: Context) throws -> Void) -> SignalProducer<Void, Error> {
return rac_backgroundOperation { (context, save) throws in
try op(context: context)
save()
}
}

View File

@ -3,11 +3,11 @@ import RxSwift
public extension Storage {
func rx_operation(op: (context: Context, save: () -> Void) -> Void) -> Observable<Void> {
func rx_operation(op: (context: Context, save: () -> Void) throws -> Void) -> Observable<Void> {
return Observable.create { (observer) -> RxSwift.Disposable in
do {
try self.operation { (context, saver) -> Void in
op(context: context, save: { () -> Void in
try self.operation { (context, saver) throws -> Void in
try op(context: context, save: { () -> Void in
saver()
})
observer.onCompleted()
@ -27,11 +27,11 @@ public extension Storage {
}
}
func rx_backgroundOperation(op: (context: Context, save: () -> Void) -> Void) -> Observable<Void> {
func rx_backgroundOperation(op: (context: Context, save: () -> Void) throws -> Void) -> Observable<Void> {
return Observable.create { (observer) -> RxSwift.Disposable in
do {
try self.operation { (context, saver) in
op(context: context, save: { () -> Void in
try self.operation { (context, saver) throws in
try op(context: context, save: { () -> Void in
saver()
})
observer.onCompleted()
@ -44,9 +44,9 @@ public extension Storage {
}
}
func rx_backgroundOperation(op: (context: Context) -> Void) -> Observable<Void> {
return rx_backgroundOperation { (context, save) in
op(context: context)
func rx_backgroundOperation(op: (context: Context) throws -> Void) -> Observable<Void> {
return rx_backgroundOperation { (context, save) throws in
try op(context: context)
save()
}
}