Skip to content

Commit

Permalink
Reactive operations also support throwing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Piñera Buendía committed Apr 5, 2016
1 parent da673fa commit a6dcb23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
}
}
Expand Down
18 changes: 9 additions & 9 deletions SugarRecord/Source/Reactive/Rx/ReactiveStorage+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
}
}
Expand Down

0 comments on commit a6dcb23

Please sign in to comment.