Skip to content

Commit d6e7309

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

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

Sources/Signal.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,20 +1444,33 @@ 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`.
1453+
///
1454+
/// - returns: A signal that sends tuples that contain previous and current
1455+
/// sent values of `self`.
1456+
public func combinePrevious(_ initial: Value) -> Signal<(Value, Value), Error> {
1457+
return scan((initial, initial)) { previousCombinedValues, newValue in
1458+
return (previousCombinedValues.1, newValue)
1459+
}
1460+
}
1461+
1462+
/// Forward events from `self` with history: values of the returned signal
1463+
/// are a tuples whose first member is the previous value and whose second member
1464+
/// is the current value.
1465+
///
1466+
/// The returned `Signal` would not emit any tuple until it has received at least two
1467+
/// values.
14551468
///
14561469
/// - returns: A signal that sends tuples that contain previous and current
14571470
/// sent values of `self`.
1458-
public func combinePrevious(_ initial: Value? = nil) -> Signal<(Value, Value), Error> {
1471+
public func combinePrevious() -> Signal<(Value, Value), Error> {
14591472
return Signal<(Value, Value), Error> { observer in
1460-
var previous = initial
1473+
var previous: Value?
14611474

14621475
return self.observe { event in
14631476
switch event {

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)