Skip to content

Commit 9abcdc3

Browse files
committed
Updated for Swift 3.0 Preview 6.
1 parent 6d8d3f8 commit 9abcdc3

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

ReactiveCocoa/Swift/Signal.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,6 @@ public final class Signal<Value, Error: Swift.Error> {
177177
}
178178
}
179179

180-
// Testability
181-
extension Signal {
182-
internal var test_observerCount: Int? {
183-
return state.withValue { $0?.observers.count }
184-
}
185-
}
186-
187180
private struct SignalState<Value, Error: Swift.Error> {
188181
var observers: Bag<Signal<Value, Error>.Observer> = Bag()
189182
var retainedSignal: Signal<Value, Error>?

ReactiveCocoa/Swift/UnidirectionalBinding.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import enum Result.NoError
22

3-
infix operator <~ {
4-
associativity right
3+
precedencegroup BindingPrecedence {
4+
associativity: right
55

66
// Binds tighter than assignment but looser than everything else
7-
precedence 93
7+
higherThan: AssignmentPrecedence
88
}
99

10-
/// Describes a target to which can be bond.
10+
infix operator <~ : BindingPrecedence
11+
12+
/// Describes a target to which can be bound.
1113
public protocol BindingTarget: class {
1214
associatedtype ValueType
1315

@@ -48,7 +50,7 @@ public protocol BindingTarget: class {
4850
/// deinitialization of the target or the signal's `completed`
4951
/// event.
5052
@discardableResult
51-
static func <~ <Source: SignalProtocol where Source.Value == ValueType, Source.Error == NoError>(target: Self, signal: Source) -> Disposable?
53+
static func <~ <Source: SignalProtocol>(target: Self, signal: Source) -> Disposable? where Source.Value == ValueType, Source.Error == NoError
5254
}
5355

5456
extension BindingTarget {
@@ -82,7 +84,7 @@ extension BindingTarget {
8284
/// deinitialization of the target or the signal's `completed`
8385
/// event.
8486
@discardableResult
85-
public static func <~ <Source: SignalProtocol where Source.Value == ValueType, Source.Error == NoError>(target: Self, signal: Source) -> Disposable? {
87+
public static func <~ <Source: SignalProtocol>(target: Self, signal: Source) -> Disposable? where Source.Value == ValueType, Source.Error == NoError {
8688
return signal
8789
.take(during: target.lifetime)
8890
.observeNext { [weak target] value in
@@ -121,7 +123,7 @@ extension BindingTarget {
121123
/// deinitialization of the target or the producer's `completed
122124
/// event.
123125
@discardableResult
124-
public static func <~ <Source: SignalProducerProtocol where Source.Value == ValueType, Source.Error == NoError>(target: Self, producer: Source) -> Disposable {
126+
public static func <~ <Source: SignalProducerProtocol>(target: Self, producer: Source) -> Disposable where Source.Value == ValueType, Source.Error == NoError {
125127
var disposable: Disposable!
126128

127129
producer
@@ -163,7 +165,7 @@ extension BindingTarget {
163165
/// - returns: A disposable that can be used to terminate binding before the
164166
/// deinitialization of the target or the source property.
165167
@discardableResult
166-
public static func <~ <Source: PropertyProtocol where Source.Value == ValueType>(target: Self, property: Source) -> Disposable {
168+
public static func <~ <Source: PropertyProtocol>(target: Self, property: Source) -> Disposable where Source.Value == ValueType {
167169
return target <~ property.producer
168170
}
169171
}
@@ -199,7 +201,7 @@ extension BindingTarget where ValueType: OptionalProtocol {
199201
/// deinitialization of the target or the signal's `completed`
200202
/// event.
201203
@discardableResult
202-
public static func <~ <Source: SignalProtocol where Source.Value == ValueType.Wrapped, Source.Error == NoError>(target: Self, signal: Source) -> Disposable? {
204+
public static func <~ <Source: SignalProtocol>(target: Self, signal: Source) -> Disposable? where Source.Value == ValueType.Wrapped, Source.Error == NoError {
203205
return target <~ signal.map(ValueType.init(reconstructing:))
204206
}
205207

@@ -234,7 +236,7 @@ extension BindingTarget where ValueType: OptionalProtocol {
234236
/// deinitialization of the target or the producer's `completed`
235237
/// event.
236238
@discardableResult
237-
public static func <~ <Source: SignalProducerProtocol where Source.Value == ValueType.Wrapped, Source.Error == NoError>(target: Self, producer: Source) -> Disposable {
239+
public static func <~ <Source: SignalProducerProtocol>(target: Self, producer: Source) -> Disposable where Source.Value == ValueType.Wrapped, Source.Error == NoError {
238240
return target <~ producer.map(ValueType.init(reconstructing:))
239241
}
240242

@@ -270,16 +272,16 @@ extension BindingTarget where ValueType: OptionalProtocol {
270272
/// - returns: A disposable that can be used to terminate binding before the
271273
/// deinitialization of the target or the source property.
272274
@discardableResult
273-
public static func <~ <Source: PropertyProtocol where Source.Value == ValueType.Wrapped>(target: Self, property: Source) -> Disposable {
275+
public static func <~ <Source: PropertyProtocol>(target: Self, property: Source) -> Disposable where Source.Value == ValueType.Wrapped {
274276
return target <~ property.producer
275277
}
276278
}
277279

278280
/// A type-erased view of a binding consumer.
279-
public class AnyBindingTarget<Value>: BindingTarget {
281+
public final class AnyBindingTarget<Value>: BindingTarget {
280282
public typealias ValueType = Value
281283

282-
let sink: (Value) -> ()
284+
private let sink: @escaping (Value) -> ()
283285

284286
/// The lifetime of this binding consumer. The binding operators use this to
285287
/// determine whether or not the binding should be teared down.
@@ -289,7 +291,7 @@ public class AnyBindingTarget<Value>: BindingTarget {
289291
///
290292
/// - parameter:
291293
/// - consumer: The binding consumer to be wrapped.
292-
public init<U: BindingTarget where U.ValueType == Value>(_ consumer: U) {
294+
public init<U: BindingTarget>(_ consumer: U) where U.ValueType == Value {
293295
self.sink = consumer.consume
294296
self.lifetime = consumer.lifetime
295297
}
@@ -299,7 +301,7 @@ public class AnyBindingTarget<Value>: BindingTarget {
299301
/// - parameter:
300302
/// - sink: The sink to receive the values from the binding.
301303
/// - lifetime: The lifetime of the binding consumer.
302-
public init(sink: (Value) -> (), lifetime: Lifetime) {
304+
public init(sink: @escaping (Value) -> (), lifetime: Lifetime) {
303305
self.sink = sink
304306
self.lifetime = lifetime
305307
}

ReactiveCocoaTests/Swift/PropertySpec.swift

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,28 +1606,27 @@ class PropertySpec: QuickSpec {
16061606
expect(mutableProperty.value) == initialPropertyValue
16071607
}
16081608

1609-
it("should tear down the binding when bound signal is completed") {
1610-
let (signal, observer) = Signal<String, NoError>.pipe()
1611-
1612-
let mutableProperty = MutableProperty(initialPropertyValue)
1613-
1614-
mutableProperty <~ signal
1615-
expect(mutableProperty.lifetime.ended.test_observerCount) == 1
1616-
1617-
observer.sendCompleted()
1618-
expect(mutableProperty.lifetime.ended.test_observerCount) == 0
1619-
}
1620-
16211609
it("should tear down the binding when the property deallocates") {
1622-
let (signal, _) = Signal<String, NoError>.pipe()
1610+
var signal: Signal<String, NoError>? = {
1611+
let (signal, _) = Signal<String, NoError>.pipe()
1612+
return signal
1613+
}()
1614+
weak var weakSignal = signal
16231615

16241616
var mutableProperty: MutableProperty<String>? = MutableProperty(initialPropertyValue)
16251617

1626-
mutableProperty! <~ signal
1627-
expect(signal.test_observerCount) == 1
1618+
mutableProperty! <~ signal!
1619+
signal = nil
16281620

1621+
// The binding attached an observer to the signal, so it cannot
1622+
// deinitialize.
1623+
expect(weakSignal).toNot(beNil())
1624+
1625+
// The deinitialization should tear down the binding, which would
1626+
// remove the last observer from the signal, causing it to
1627+
// dispose of itself.
16291628
mutableProperty = nil
1630-
expect(signal.test_observerCount) == 0
1629+
expect(weakSignal).to(beNil())
16311630
}
16321631
}
16331632

@@ -1655,19 +1654,6 @@ class PropertySpec: QuickSpec {
16551654
expect(mutableProperty.value) == initialPropertyValue
16561655
}
16571656

1658-
it("should tear down the binding when bound signal is completed") {
1659-
let (signalProducer, observer) = SignalProducer<String, NoError>.pipe()
1660-
1661-
let mutableProperty = MutableProperty(initialPropertyValue)
1662-
1663-
var isDisposed = false
1664-
mutableProperty <~ signalProducer.on(disposed: { isDisposed = true })
1665-
expect(isDisposed) == false
1666-
1667-
observer.sendCompleted()
1668-
expect(isDisposed) == true
1669-
}
1670-
16711657
it("should tear down the binding when the property deallocates") {
16721658
let (signal, _) = Signal<String, NoError>.pipe()
16731659
let signalProducer = SignalProducer(signal: signal)
@@ -1733,7 +1719,7 @@ class PropertySpec: QuickSpec {
17331719
var destinationProperty: MutableProperty<String>? = MutableProperty("")
17341720

17351721
var isDisposed = false
1736-
let bindingDisposable = destinationProperty! <~ sourceProperty.producer.on(disposed: { isDisposed = true })
1722+
destinationProperty! <~ sourceProperty.producer.on(disposed: { isDisposed = true })
17371723
expect(isDisposed) == false
17381724

17391725
destinationProperty = nil

0 commit comments

Comments
 (0)