Skip to content

Commit

Permalink
Merge pull request ReactiveCocoa#2601 from Adlai-Holler/UpdateAtomic
Browse files Browse the repository at this point in the history
Add Support for Rethrowing in Atomic Closure Methods
  • Loading branch information
NachoSoto committed Dec 5, 2015
2 parents 921b088 + 754e9c6 commit 028a086
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions ReactiveCocoa/Swift/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,23 @@ public final class Atomic<Value> {
/// Atomically modifies the variable.
///
/// Returns the old value.
public func modify(@noescape action: Value -> Value) -> Value {
public func modify(@noescape action: (Value) throws -> Value) rethrows -> Value {
lock()
defer { unlock() }

let oldValue = _value
_value = action(_value)
unlock()

_value = try action(_value)
return oldValue
}

/// Atomically performs an arbitrary action using the current value of the
/// variable.
///
/// Returns the result of the action.
public func withValue<U>(@noescape action: Value -> U) -> U {
public func withValue<Result>(@noescape action: (Value) throws -> Result) rethrows -> Result {
lock()
let result = action(_value)
unlock()

return result
defer { unlock() }

return try action(_value)
}
}

0 comments on commit 028a086

Please sign in to comment.