Skip to content

Commit 99cfe89

Browse files
committed
Resurrect the original combinePrevious.
1 parent df14b40 commit 99cfe89

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

Sources/Signal.swift

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,18 +1444,37 @@ extension Signal {
14441444

14451445
/// Forward events from `self` with history: values of the returned signal
14461446
/// are a tuples whose first member is the previous value and whose second member
1447-
/// is the current value.
1448-
///
1449-
/// If an initial value is given, the returned `Signal` would emit tuples as soon as
1450-
/// the first value is received. If `initial` is nil, the returned `Signal` would not
1451-
/// emit any tuple until it has received at least two values.
1447+
/// is the current value. `initial` is supplied as the first member when `self`
1448+
/// sends its first value.
14521449
///
14531450
/// - parameters:
1454-
/// - initial: An optional initial value.
1451+
/// - initial: A value that will be combined with the first value sent by
1452+
/// `self`.
14551453
///
14561454
/// - returns: A signal that sends tuples that contain previous and current
14571455
/// sent values of `self`.
1458-
public func combinePrevious(_ initial: Value? = nil) -> Signal<(Value, Value), Error> {
1456+
public func combinePrevious(_ initial: Value) -> Signal<(Value, Value), Error> {
1457+
return combinePrevious(initial: initial)
1458+
}
1459+
1460+
/// Forward events from `self` with history: values of the returned signal
1461+
/// are a tuples whose first member is the previous value and whose second member
1462+
/// is the current value.
1463+
///
1464+
/// The returned `Signal` would not emit any tuple until it has received at least two
1465+
/// values.
1466+
///
1467+
/// - returns: A signal that sends tuples that contain previous and current
1468+
/// sent values of `self`.
1469+
public func combinePrevious() -> Signal<(Value, Value), Error> {
1470+
return combinePrevious(initial: nil)
1471+
}
1472+
1473+
/// Implementation detail of `combinePrevious`. A default argument of a `nil` initial
1474+
/// is deliberately avoided, since in the case of `Value` being an optional, the
1475+
/// `nil` literal would be materialized as `Optional<Value>.none` instead of `Value`,
1476+
/// thus changing the semantic.
1477+
private func combinePrevious(initial: Value?) -> Signal<(Value, Value), Error> {
14591478
return Signal<(Value, Value), Error> { observer in
14601479
var previous = initial
14611480

Sources/SignalProducer.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -978,21 +978,32 @@ extension SignalProducer {
978978
return liftRight(Signal.skip(until:))(trigger.producer)
979979
}
980980

981+
/// Forward events from `self` with history: values of the returned signal
982+
/// are a tuples whose first member is the previous value and whose second member
983+
/// is the current value. `initial` is supplied as the first member when `self`
984+
/// sends its first value.
985+
///
986+
/// - parameters:
987+
/// - initial: A value that will be combined with the first value sent by
988+
/// `self`.
989+
///
990+
/// - returns: A signal that sends tuples that contain previous and current
991+
/// sent values of `self`.
992+
public func combinePrevious(_ initial: Value) -> SignalProducer<(Value, Value), Error> {
993+
return lift { $0.combinePrevious(initial) }
994+
}
995+
981996
/// Forward events from `self` with history: values of the produced signal
982997
/// are a tuples whose first member is the previous value and whose second member
983998
/// is the current value.
984999
///
985-
/// If an initial value is given, the produced `Signal` would emit tuples as soon as
986-
/// the first value is received. If `initial` is nil, the produced `Signal` would not
987-
/// emit any tuple until it has received at least two values.
988-
///
989-
/// - parameters:
990-
/// - initial: An optional initial value.
1000+
/// The produced `Signal` would not emit any tuple until it has received at least two
1001+
/// values.
9911002
///
9921003
/// - returns: A producer that sends tuples that contain previous and current
9931004
/// sent values of `self`.
994-
public func combinePrevious(_ initial: Value? = nil) -> SignalProducer<(Value, Value), Error> {
995-
return lift { $0.combinePrevious(initial) }
1005+
public func combinePrevious() -> SignalProducer<(Value, Value), Error> {
1006+
return lift { $0.combinePrevious() }
9961007
}
9971008

9981009
/// Combine all values from `self`, and forward the final result.

Tests/ReactiveSwiftTests/SignalSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,7 @@ class SignalSpec: QuickSpec {
27682768
(signal, observer) = (baseSignal, baseObserver)
27692769
}
27702770

2771-
it("should forward the latest value with previous value with the given initial value") {
2771+
it("should forward the latest value with previous value with an initial value") {
27722772
signal.combinePrevious(initialValue).observeValues { latestValues = $0 }
27732773

27742774
expect(latestValues).to(beNil())

0 commit comments

Comments
 (0)