Skip to content

Commit

Permalink
remove peek in favor of value property
Browse files Browse the repository at this point in the history
  • Loading branch information
JensRavens committed Sep 26, 2016
1 parent aa80863 commit b4c5332
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Sources/Observable+Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public extension Observable where T : ResultType {
return self
}

public func peekValue() -> T.Value? {
return peek()?.value
public func peek() -> T.Value? {
return self.value?.value
}
}
14 changes: 5 additions & 9 deletions Sources/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct ObservingOptions: OptionSet {
public final class Observable<T> {
fileprivate typealias Observer = (T)->Void
fileprivate var observers = [ObserverToken: Observer]()
fileprivate var lastValue: T?
public private(set) var value: T?
public let options: ObservingOptions
fileprivate let mutex = Mutex()

Expand All @@ -30,7 +30,7 @@ public final class Observable<T> {
public init(_ value: T, options: ObservingOptions = []) {
self.options = options
if !options.contains(.NoInitialValue){
lastValue = value
self.value = value
}
}

Expand All @@ -39,10 +39,10 @@ public final class Observable<T> {
mutex.lock {
let newHashValue = (observers.keys.map({$0.hashValue}).max() ?? -1) + 1
token = ObserverToken(hashValue: newHashValue)
if !(options.contains(.Once) && lastValue != nil) {
if !(options.contains(.Once) && value != nil) {
observers[token] = observer
}
if let value = lastValue , !options.contains(.NoInitialValue) {
if let value = value , !options.contains(.NoInitialValue) {
observer(value)
}
}
Expand All @@ -52,7 +52,7 @@ public final class Observable<T> {
public func update(_ value: T) {
mutex.lock {
if !options.contains(.NoInitialValue) {
lastValue = value
self.value = value
}
for observe in observers.values {
observe(value)
Expand All @@ -62,10 +62,6 @@ public final class Observable<T> {
}
}
}

public func peek() -> T? {
return lastValue
}

public func unsubscribe(_ token: ObserverToken) {
mutex.lock {
Expand Down
8 changes: 4 additions & 4 deletions Tests/ObservableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class ObservableTests: XCTestCase {

func testMappingAnObservable() {
let greeting = Observable("World").map(greeter)
XCTAssertEqual(greeting.peek(), "Hello World")
XCTAssertEqual(greeting.value, "Hello World")
}

func testFlatMappingObservable() {
let greeting = Observable("World").flatMap(greetLater)
XCTAssertEqual(greeting.peek(), "Hello World")
XCTAssertEqual(greeting.value, "Hello World")
}

func testSubscription() {
Expand Down Expand Up @@ -62,9 +62,9 @@ class ObservableTests: XCTestCase {

func testLiveSubscriptions() {
let observable = Observable<String>("Hello", options:[.NoInitialValue])
XCTAssertNil(observable.peek())
XCTAssertNil(observable.value)
observable.update("Hello")
XCTAssertNil(observable.peek())
XCTAssertNil(observable.value)
}

func testUnsubscribe() {
Expand Down
4 changes: 2 additions & 2 deletions Tests/ResultObservableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class ResultObservableTests: XCTestCase {

func testContinuingTheChain() {
let greeting = world.then(greeter).then(throwingGreeter)
XCTAssertEqual(greeting.peekValue(), "Hello Hello World")
XCTAssertEqual(greeting.peek(), "Hello Hello World")
}

func testError() {
let greeting = nothing.then(throwingGreeter).peekValue()
let greeting = nothing.then(throwingGreeter).peek()
XCTAssertNil(greeting)
}
}

0 comments on commit b4c5332

Please sign in to comment.