Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace start and observe overloads with new Observer API #55

Merged
merged 1 commit into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Foundation/Association.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public func associatedProperty<T: AnyObject>(host: AnyObject, keyPath: StaticStr
public func associatedProperty<T>(host: AnyObject, key: UnsafePointer<()>, initial: () -> T, setter: T -> ()) -> MutableProperty<T> {
return associatedObject(host, key: key) {
let property = MutableProperty(initial())
property.producer.start(next: setter)
property.producer.start(Observer(next: setter))
return property
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Foundation/NSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension NSObject {
public func rex_valueProperty<T>(key: UnsafePointer<()>, _ initial: () -> T, _ setter: T -> ()) -> MutableProperty<T> {
return associatedObject(self, key: key) {
let property = MutableProperty(initial())
property.producer.start(next: setter)
property.producer.start(Observer(next: setter))
return property
}
}
Expand Down
22 changes: 2 additions & 20 deletions Source/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,11 @@ import ReactiveCocoa

extension SignalType {

/// Bring back the `observe` overload. The `observeNext` or pattern matching
/// on `observe(Event)` is still annoying in practice and more verbose. This is
/// also likely to change in a later RAC 4 alpha.
internal func observe(next next: (Value -> ())? = nil, error: (Error -> ())? = nil, completed: (() -> ())? = nil, interrupted: (() -> ())? = nil) -> Disposable? {
return self.observe { (event: Event<Value, Error>) in
switch event {
case let .Next(value):
next?(value)
case let .Error(err):
error?(err)
case .Completed:
completed?()
case .Interrupted:
interrupted?()
}
}
}

/// Applies `transform` to values from `signal` with non-`nil` results unwrapped and
/// forwared on the returned signal.
public func filterMap<U>(transform: Value -> U?) -> Signal<U, Error> {
return Signal<U, Error> { observer in
return self.observe(next: { value in
return self.observe(Observer(next: { value in
if let val = transform(value) {
observer.sendNext(val)
}
Expand All @@ -42,7 +24,7 @@ extension SignalType {
observer.sendCompleted()
}, interrupted: {
observer.sendInterrupted()
})
}))
}
}

Expand Down
22 changes: 2 additions & 20 deletions Source/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ import ReactiveCocoa

extension SignalProducerType {

/// Bring back the `start` overload. The `startNext` or pattern matching
/// on `start(Event)` is annoying in practice and more verbose. This is also
/// likely to change in a later RAC 4 alpha.
internal func start(next next: (Value -> ())? = nil, error: (Error -> ())? = nil, completed: (() -> ())? = nil, interrupted: (() -> ())? = nil) -> Disposable? {
return self.start { (event: Event<Value, Error>) in
switch event {
case let .Next(value):
next?(value)
case let .Error(err):
error?(err)
case .Completed:
completed?()
case .Interrupted:
interrupted?()
}
}
}

/// Buckets each received value into a group based on the key returned
/// from `grouping`. Termination events on the original signal are
/// also forwarded to each producer group.
Expand All @@ -38,7 +20,7 @@ extension SignalProducerType {
let lock = NSRecursiveLock()
lock.name = "me.neilpa.rex.groupBy"

self.start(next: { value in
self.start(Observer(next: { value in
let key = grouping(value)

lock.lock()
Expand All @@ -65,7 +47,7 @@ extension SignalProducerType {
}, interrupted: { _ in
observer.sendInterrupted()
groups.values.forEach { $0.sendInterrupted() }
})
}))
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/UIKit/UIBarButtonItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ extension UIBarButtonItem {
let initial = CocoaAction.rex_disabled
let property = MutableProperty(initial)

property.producer.start(next: { next in
property.producer.start(Observer(next: { next in
self?.target = next
self?.action = CocoaAction.selector
})
}))

if let strongSelf = self {
strongSelf.rex_enabled <~ property.producer.flatMap(.Latest) { $0.rex_enabledProducer }
Expand Down
4 changes: 2 additions & 2 deletions Source/UIKit/UIButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ extension UIButton {

property.producer
.combinePrevious(initial)
.start(next: { previous, next in
.start(Observer(next: { previous, next in
self?.removeTarget(previous, action: CocoaAction.selector, forControlEvents: .TouchUpInside)
self?.addTarget(next, action: CocoaAction.selector, forControlEvents: .TouchUpInside)
})
}))

if let strongSelf = self {
strongSelf.rex_enabled <~ property.producer.flatMap(.Latest) { $0.rex_enabledProducer }
Expand Down
4 changes: 2 additions & 2 deletions Tests/Foundation/NSObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright (c) 2015 Neil Pankey. All rights reserved.
//

@testable import Rex
import Rex
import ReactiveCocoa
import XCTest

Expand All @@ -16,7 +16,7 @@ final class NSObjectTests: XCTestCase {
let object = Object()
var value: String = ""

object.rex_producerForKeyPath("string").start(next: { value = $0 })
object.rex_producerForKeyPath("string").start(Observer(next: { value = $0 }))
XCTAssertEqual(value, "foo")

object.string = "bar"
Expand Down
10 changes: 5 additions & 5 deletions Tests/SignalProducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright (c) 2015 Neil Pankey. All rights reserved.
//

@testable import Rex
import Rex
import ReactiveCocoa
import XCTest

Expand All @@ -22,17 +22,17 @@ final class SignalProducerTests: XCTestCase {

disposable += producer
.groupBy { $0 % 2 == 0 }
.start(next: { key, group in
.start(Observer(next: { key, group in
if key {
group.start(next: { evens.append($0) })
group.start(Observer(next: { evens.append($0) }))
} else {
group.start(next: { odds.append($0) })
group.start(Observer(next: { odds.append($0) }))
}
},completed: {
completed = true
}, interrupted: {
interrupted = true
})
}))

sink.sendNext(1)
XCTAssert(evens == [])
Expand Down
24 changes: 12 additions & 12 deletions Tests/SignalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright (c) 2015 Neil Pankey. All rights reserved.
//

@testable import Rex
import Rex
import ReactiveCocoa
import XCTest

Expand All @@ -20,7 +20,7 @@ final class SignalTests: XCTestCase {
.filterMap {
return $0 % 2 == 0 ? String($0) : nil
}
.observe(next: { values.append($0) })
.observe(Observer(next: { values.append($0) }))

sink.sendNext(1)
XCTAssert(values == [])
Expand All @@ -41,9 +41,9 @@ final class SignalTests: XCTestCase {

signal
.ignoreError()
.observe(completed: {
.observe(Observer(completed: {
completed = true
})
}))

sink.sendNext(1)
XCTAssertFalse(completed)
Expand All @@ -58,9 +58,9 @@ final class SignalTests: XCTestCase {

signal
.ignoreError(replacement: .Interrupted)
.observe(interrupted: {
.observe(Observer(interrupted: {
interrupted = true
})
}))

sink.sendNext(1)
XCTAssertFalse(interrupted)
Expand All @@ -77,10 +77,10 @@ final class SignalTests: XCTestCase {

signal
.timeoutAfter(2, withEvent: .Interrupted, onScheduler: scheduler)
.observe(
.observe(Observer(
completed: { completed = true },
interrupted: { interrupted = true }
)
))

scheduler.scheduleAfter(1) { sink.sendCompleted() }

Expand All @@ -100,10 +100,10 @@ final class SignalTests: XCTestCase {

signal
.timeoutAfter(2, withEvent: .Interrupted, onScheduler: scheduler)
.observe(
.observe(Observer(
completed: { completed = true },
interrupted: { interrupted = true }
)
))

scheduler.scheduleAfter(3) { sink.sendCompleted() }

Expand All @@ -121,9 +121,9 @@ final class SignalTests: XCTestCase {

signal
.uncollect()
.observe(next: {
.observe(Observer(next: {
values.append($0)
})
}))

sink.sendNext([])
XCTAssert(values.isEmpty)
Expand Down