-
Notifications
You must be signed in to change notification settings - Fork 429
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
Add merge(with:) operator #600
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
657810a
Add merge(with:) operator
ra1028 ee123bb
Update CHANGELOG
ra1028 22bbee9
Add tests for Signal.merge(with:)
ra1028 ae40e6d
Add tests for SignalProducer.merge(with:)
ra1028 2a48252
Refactor tests for Signal.merge() and SignalProducer.merge()
ra1028 d9935fd
Revert docs for Signal.merge() and SignalProducer.merge()
ra1028 6e4cbfc
Merge master
ra1028 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -820,8 +820,9 @@ class FlattenSpec: QuickSpec { | |
it("should emit values from all signals") { | ||
let (signal1, observer1) = Signal<Int, NoError>.pipe() | ||
let (signal2, observer2) = Signal<Int, NoError>.pipe() | ||
let (signal3, observer3) = Signal<Int, NoError>.pipe() | ||
|
||
let mergedSignals = Signal.merge([signal1, signal2]) | ||
let mergedSignals = Signal.merge([signal1, signal2, signal3]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for making this more complex now 👍 |
||
|
||
var lastValue: Int? | ||
mergedSignals.observeValues { lastValue = $0 } | ||
|
@@ -834,15 +835,19 @@ class FlattenSpec: QuickSpec { | |
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
|
||
observer1.send(value: 3) | ||
observer3.send(value: 3) | ||
expect(lastValue) == 3 | ||
|
||
observer1.send(value: 4) | ||
expect(lastValue) == 4 | ||
} | ||
|
||
it("should not stop when one signal completes") { | ||
let (signal1, observer1) = Signal<Int, NoError>.pipe() | ||
let (signal2, observer2) = Signal<Int, NoError>.pipe() | ||
let (signal3, observer3) = Signal<Int, NoError>.pipe() | ||
|
||
let mergedSignals = Signal.merge([signal1, signal2]) | ||
let mergedSignals = Signal.merge([signal1, signal2, signal3]) | ||
|
||
var lastValue: Int? | ||
mergedSignals.observeValues { lastValue = $0 } | ||
|
@@ -857,13 +862,17 @@ class FlattenSpec: QuickSpec { | |
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
|
||
observer3.send(value: 3) | ||
expect(lastValue) == 3 | ||
} | ||
|
||
it("should complete when all signals complete") { | ||
let (signal1, observer1) = Signal<Int, NoError>.pipe() | ||
let (signal2, observer2) = Signal<Int, NoError>.pipe() | ||
let (signal3, observer3) = Signal<Int, NoError>.pipe() | ||
|
||
let mergedSignals = Signal.merge([signal1, signal2]) | ||
let mergedSignals = Signal.merge([signal1, signal2, signal3]) | ||
|
||
var completed = false | ||
mergedSignals.observeCompleted { completed = true } | ||
|
@@ -876,71 +885,216 @@ class FlattenSpec: QuickSpec { | |
observer1.sendCompleted() | ||
expect(completed) == false | ||
|
||
observer2.sendCompleted() | ||
expect(completed) == false | ||
|
||
observer3.sendCompleted() | ||
expect(completed) == true | ||
} | ||
} | ||
|
||
describe("Signal.merge(with:)") { | ||
it("should emit values from both signals") { | ||
let (signal1, observer1) = Signal<Int, NoError>.pipe() | ||
let (signal2, observer2) = Signal<Int, NoError>.pipe() | ||
|
||
let mergedSignals = signal1.merge(with: signal2) | ||
|
||
var lastValue: Int? | ||
mergedSignals.observeValues { lastValue = $0 } | ||
|
||
expect(lastValue).to(beNil()) | ||
|
||
observer1.send(value: 1) | ||
expect(lastValue) == 1 | ||
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
|
||
observer1.send(value: 3) | ||
expect(lastValue) == 3 | ||
} | ||
|
||
it("should not stop when one signal completes") { | ||
let (signal1, observer1) = Signal<Int, NoError>.pipe() | ||
let (signal2, observer2) = Signal<Int, NoError>.pipe() | ||
|
||
let mergedSignals = signal1.merge(with: signal2) | ||
|
||
var lastValue: Int? | ||
mergedSignals.observeValues { lastValue = $0 } | ||
|
||
expect(lastValue).to(beNil()) | ||
|
||
observer1.send(value: 1) | ||
expect(lastValue) == 1 | ||
|
||
observer1.sendCompleted() | ||
expect(lastValue) == 1 | ||
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
} | ||
|
||
it("should complete when both signals complete") { | ||
let (signal1, observer1) = Signal<Int, NoError>.pipe() | ||
let (signal2, observer2) = Signal<Int, NoError>.pipe() | ||
|
||
let mergedSignals = signal1.merge(with: signal2) | ||
|
||
var completed = false | ||
mergedSignals.observeCompleted { completed = true } | ||
|
||
expect(completed) == false | ||
|
||
observer1.send(value: 1) | ||
expect(completed) == false | ||
|
||
observer1.sendCompleted() | ||
expect(completed) == false | ||
|
||
observer2.sendCompleted() | ||
expect(completed) == true | ||
} | ||
} | ||
|
||
describe("SignalProducer.merge()") { | ||
it("should emit values from all producers") { | ||
let (signal1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (signal2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
let (producer1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (producer2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
let (producer3, observer3) = SignalProducer<Int, NoError>.pipe() | ||
|
||
let mergedSignals = SignalProducer.merge([signal1, signal2]) | ||
let mergedProducer = SignalProducer.merge([producer1, producer2, producer3]) | ||
|
||
var lastValue: Int? | ||
mergedSignals.startWithValues { lastValue = $0 } | ||
mergedProducer.startWithValues { lastValue = $0 } | ||
|
||
expect(lastValue).to(beNil()) | ||
|
||
observer1.send(value: 1) | ||
expect(lastValue) == 1 | ||
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
|
||
observer1.send(value: 3) | ||
observer3.send(value: 3) | ||
expect(lastValue) == 3 | ||
|
||
observer1.send(value: 4) | ||
expect(lastValue) == 4 | ||
} | ||
|
||
it("should not stop when one producer completes") { | ||
let (signal1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (signal2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
let (producer1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (producer2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
let (producer3, observer3) = SignalProducer<Int, NoError>.pipe() | ||
|
||
let mergedSignals = SignalProducer.merge([signal1, signal2]) | ||
let mergedProducer = SignalProducer.merge([producer1, producer2, producer3]) | ||
|
||
var lastValue: Int? | ||
mergedSignals.startWithValues { lastValue = $0 } | ||
mergedProducer.startWithValues { lastValue = $0 } | ||
|
||
expect(lastValue).to(beNil()) | ||
|
||
observer1.send(value: 1) | ||
expect(lastValue) == 1 | ||
|
||
observer1.sendCompleted() | ||
expect(lastValue) == 1 | ||
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
|
||
observer3.send(value: 3) | ||
expect(lastValue) == 3 | ||
} | ||
|
||
it("should complete when all producers complete") { | ||
let (signal1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (signal2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
let (producer1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (producer2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
let (producer3, observer3) = SignalProducer<Int, NoError>.pipe() | ||
|
||
let mergedSignals = SignalProducer.merge([signal1, signal2]) | ||
let mergedProducer = SignalProducer.merge([producer1, producer2, producer3]) | ||
|
||
var completed = false | ||
mergedSignals.startWithCompleted { completed = true } | ||
mergedProducer.startWithCompleted { completed = true } | ||
|
||
expect(completed) == false | ||
|
||
observer1.send(value: 1) | ||
expect(completed) == false | ||
|
||
observer1.sendCompleted() | ||
expect(completed) == false | ||
|
||
|
||
observer2.sendCompleted() | ||
expect(completed) == false | ||
|
||
observer3.sendCompleted() | ||
expect(completed) == true | ||
} | ||
} | ||
|
||
describe("SignalProducer.merge(with:)") { | ||
it("should emit values from both producers") { | ||
let (producer1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (producer2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
|
||
let mergedProducer = producer1.merge(with: producer2) | ||
|
||
var lastValue: Int? | ||
mergedProducer.startWithValues { lastValue = $0 } | ||
|
||
expect(lastValue).to(beNil()) | ||
|
||
observer1.send(value: 1) | ||
expect(lastValue) == 1 | ||
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
|
||
observer1.send(value: 3) | ||
expect(lastValue) == 3 | ||
} | ||
|
||
it("should not stop when one producer completes") { | ||
let (producer1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (producer2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
|
||
let mergedProducer = producer1.merge(with: producer2) | ||
|
||
var lastValue: Int? | ||
mergedProducer.startWithValues { lastValue = $0 } | ||
|
||
expect(lastValue).to(beNil()) | ||
|
||
observer1.send(value: 1) | ||
expect(lastValue) == 1 | ||
|
||
observer1.sendCompleted() | ||
expect(lastValue) == 1 | ||
|
||
observer2.send(value: 2) | ||
expect(lastValue) == 2 | ||
} | ||
|
||
it("should complete when both producers complete") { | ||
let (producer1, observer1) = SignalProducer<Int, NoError>.pipe() | ||
let (producer2, observer2) = SignalProducer<Int, NoError>.pipe() | ||
|
||
let mergedProducer = producer1.merge(with: producer2) | ||
|
||
var completed = false | ||
mergedProducer.startWithCompleted { completed = true } | ||
|
||
expect(completed) == false | ||
|
||
observer1.send(value: 1) | ||
expect(completed) == false | ||
|
||
observer1.sendCompleted() | ||
expect(completed) == false | ||
|
||
observer2.sendCompleted() | ||
expect(completed) == true | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we still ought to have a test for this (and the
SignalProducer
variant below). Would you mind adding one?