Skip to content

Commit aac1d75

Browse files
fortmarekandersio
andauthored
Add variadic sugar for boolean static methods. (#801)
* Add variadic sugar for boolean static methods. * Fix typo. * Edit changelog. Co-authored-by: Anders Ha <[email protected]>
1 parent 2d46703 commit aac1d75

File tree

7 files changed

+72
-7
lines changed

7 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# 6.4.0
77
1. Bump min. deployment target to iOS 9 when using swift packages to silence Xcode 12 warnings. Update Quick & Nibmle to the latest version when using swift packages.
88

9+
1. Add variadic sugar for boolean static methods such as `Property.any(boolProperty1, boolProperty2, boolProperty3)` (#801, kudos to @fortmarek)
910
1. Fix a debug assertion in `Lock.try()` that could be raised in earlier OS versions (< iOS 10.0, < macOS 10.12). (#747, #788)
1011

1112
Specifically, ReactiveSwift now recognizes `EDEADLK` as expected error code from `pthread_mutex_trylock` alongside `0`, `EBUSY` and `EAGAIN`.

Sources/Property.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ extension PropertyProtocol where Value == Bool {
443443
public static func all<P: PropertyProtocol, Properties: Collection>(_ properties: Properties) -> Property<Value> where P.Value == Value, Properties.Element == P {
444444
return Property(initial: properties.map { $0.value }.reduce(true) { $0 && $1 }, then: SignalProducer.all(properties))
445445
}
446+
447+
/// Create a property that computes a logical AND between the latest values of `properties`.
448+
///
449+
/// - parameters:
450+
/// - property: Properties to be combined.
451+
///
452+
/// - returns: A property that contains the logical AND results.
453+
public static func all<P: PropertyProtocol>(_ properties: P...) -> Property<Value> where P.Value == Value {
454+
return .all(properties)
455+
}
446456

447457
/// Create a property that computes a logical OR between the latest values of `self`
448458
/// and `property`.
@@ -464,6 +474,16 @@ extension PropertyProtocol where Value == Bool {
464474
public static func any<P: PropertyProtocol, Properties: Collection>(_ properties: Properties) -> Property<Value> where P.Value == Value, Properties.Element == P {
465475
return Property(initial: properties.map { $0.value }.reduce(false) { $0 || $1 }, then: SignalProducer.any(properties))
466476
}
477+
478+
/// Create a property that computes a logical OR between the latest values of `properties`.
479+
///
480+
/// - parameters:
481+
/// - properties: Properties to be combined.
482+
///
483+
/// - returns: A property that contains the logical OR results.
484+
public static func any<P: PropertyProtocol>(_ properties: P...) -> Property<Value> where P.Value == Value {
485+
return .any(properties)
486+
}
467487
}
468488

469489
/// A read-only property that can be observed for its changes over time. There

Sources/Signal.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,16 @@ extension Signal where Value == Bool {
22102210
public static func all<BooleansCollection: Collection>(_ booleans: BooleansCollection) -> Signal<Value, Error> where BooleansCollection.Element == Signal<Value, Error> {
22112211
return combineLatest(booleans).map { $0.reduce(true) { $0 && $1 } }
22122212
}
2213+
2214+
/// Create a signal that computes a logical AND between the latest values of `booleans`.
2215+
///
2216+
/// - parameters:
2217+
/// - booleans: Boolean signals to be combined.
2218+
///
2219+
/// - returns: A signal that emits the logical AND results.
2220+
public static func all(_ booleans: Signal<Value, Error>...) -> Signal<Value, Error> {
2221+
return .all(booleans)
2222+
}
22132223

22142224
/// Create a signal that computes a logical OR between the latest values of `self`
22152225
/// and `signal`.
@@ -2230,7 +2240,17 @@ extension Signal where Value == Bool {
22302240
/// - returns: A signal that emits the logical OR results.
22312241
public static func any<BooleansCollection: Collection>(_ booleans: BooleansCollection) -> Signal<Value, Error> where BooleansCollection.Element == Signal<Value, Error> {
22322242
return combineLatest(booleans).map { $0.reduce(false) { $0 || $1 } }
2233-
}
2243+
}
2244+
2245+
/// Create a signal that computes a logical OR between the latest values of `booleans`.
2246+
///
2247+
/// - parameters:
2248+
/// - booleans: Boolean signals to be combined.
2249+
///
2250+
/// - returns: A signal that emits the logical OR results.
2251+
public static func any(_ booleans: Signal<Value, Error>...) -> Signal<Value, Error> {
2252+
return .any(booleans)
2253+
}
22342254
}
22352255

22362256
extension Signal {

Sources/SignalProducer.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,6 +2770,18 @@ extension SignalProducer where Value == Bool {
27702770
public static func all<BooleansCollection: Collection>(_ booleans: BooleansCollection) -> SignalProducer<Value, Error> where BooleansCollection.Element == SignalProducer<Value, Error> {
27712771
return combineLatest(booleans, emptySentinel: []).map { $0.reduce(true) { $0 && $1 } }
27722772
}
2773+
2774+
/// Create a producer that computes a logical AND between the latest values of `booleans`.
2775+
///
2776+
/// If no producer is given in `booleans`, the resulting producer constantly emits `true`.
2777+
///
2778+
/// - parameters:
2779+
/// - booleans: Boolean producers to be combined.
2780+
///
2781+
/// - returns: A producer that emits the logical AND results.
2782+
public static func all(_ booleans: SignalProducer<Value, Error>...) -> SignalProducer<Value, Error> {
2783+
return .all(booleans)
2784+
}
27732785

27742786
/// Create a producer that computes a logical AND between the latest values of `booleans`.
27752787
///
@@ -2816,6 +2828,18 @@ extension SignalProducer where Value == Bool {
28162828
public static func any<BooleansCollection: Collection>(_ booleans: BooleansCollection) -> SignalProducer<Value, Error> where BooleansCollection.Element == SignalProducer<Value, Error> {
28172829
return combineLatest(booleans, emptySentinel: []).map { $0.reduce(false) { $0 || $1 } }
28182830
}
2831+
2832+
/// Create a producer that computes a logical OR between the latest values of `booleans`.
2833+
///
2834+
/// If no producer is given in `booleans`, the resulting producer constantly emits `false`.
2835+
///
2836+
/// - parameters:
2837+
/// - booleans: Boolean producers to be combined.
2838+
///
2839+
/// - returns: A producer that emits the logical OR results.
2840+
public static func any(_ booleans: SignalProducer<Value, Error>...) -> SignalProducer<Value, Error> {
2841+
return .any(booleans)
2842+
}
28192843

28202844
/// Create a producer that computes a logical OR between the latest values of `booleans`.
28212845
///

Tests/ReactiveSwiftTests/PropertySpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,11 +1789,11 @@ class PropertySpec: QuickSpec {
17891789
expect(Property.any([property1, property2, property3]).value).to(beTrue())
17901790
}
17911791

1792-
it("should emit false when all properties in array contain false") {
1792+
it("should emit false when all properties contain false") {
17931793
let property1 = MutableProperty(false)
17941794
let property2 = MutableProperty(false)
17951795
let property3 = MutableProperty(false)
1796-
expect(Property.any([property1, property2, property3]).value).to(beFalse())
1796+
expect(Property.any(property1, property2, property3).value).to(beFalse())
17971797
}
17981798

17991799
it("should emit false when array of properties is empty") {

Tests/ReactiveSwiftTests/SignalProducerSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,7 +3692,7 @@ class SignalProducerSpec: QuickSpec {
36923692
}
36933693
}
36943694

3695-
it("should emit false when all producers in array emit false") {
3695+
it("should emit false when all producers emit false") {
36963696
let producer1 = SignalProducer<Bool, Never> { observer, _ in
36973697
observer.send(value: false)
36983698
observer.sendCompleted()
@@ -3706,7 +3706,7 @@ class SignalProducerSpec: QuickSpec {
37063706
observer.sendCompleted()
37073707
}
37083708

3709-
SignalProducer.any([producer1, producer2, producer3]).startWithValues { value in
3709+
SignalProducer.any(producer1, producer2, producer3).startWithValues { value in
37103710
expect(value).to(beFalse())
37113711
}
37123712
}

Tests/ReactiveSwiftTests/SignalSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3896,11 +3896,11 @@ class SignalSpec: QuickSpec {
38963896
observer3.sendCompleted()
38973897
}
38983898

3899-
it("should emit false when all signals in array emits false") {
3899+
it("should emit false when all signals emits false") {
39003900
let (signal1, observer1) = Signal<Bool, Never>.pipe()
39013901
let (signal2, observer2) = Signal<Bool, Never>.pipe()
39023902
let (signal3, observer3) = Signal<Bool, Never>.pipe()
3903-
Signal.any([signal1, signal2, signal3]).observeValues { value in
3903+
Signal.any(signal1, signal2, signal3).observeValues { value in
39043904
expect(value).to(beFalse())
39053905
}
39063906
observer1.send(value: false)

0 commit comments

Comments
 (0)