Skip to content

Commit

Permalink
Add concat operator to Completable
Browse files Browse the repository at this point in the history
  • Loading branch information
hagmas authored and kzaher committed Jun 15, 2017
1 parent 46325a0 commit 1a8e7f4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
## [3.5.0](https://github.com/ReactiveX/RxSwift/releases/tag/3.5.0)

* Adds `from` operator on "SharedSequence"
* Adds `concat` operator on "Completable"
* Adds `merge` operator on "Completable"
* Adds `using` operator on "PrimitiveSequence"
* Adds `concatMap` operator.
Expand Down
52 changes: 52 additions & 0 deletions RxSwift/Traits/PrimitiveSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,20 @@ extension PrimitiveSequenceType where TraitType == MaybeTrait {
}
}

extension PrimitiveSequence where Trait == CompletableTrait, Element == Never {
/**
Concatenates the second observable sequence to `self` upon successful termination of `self`.

- seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)

- parameter second: Second observable sequence.
- returns: An observable sequence that contains the elements of `self`, followed by those of the second sequence.
*/
public func concat(_ second: PrimitiveSequence<CompletableTrait, Never>) -> PrimitiveSequence<CompletableTrait, Never> {
return Completable.concat(self, second)
}
}

extension PrimitiveSequenceType where TraitType == CompletableTrait, ElementType == Never {
/**
Returns an empty observable sequence, using the specified scheduler to send out the single `Completed` message.
Expand All @@ -639,6 +653,44 @@ extension PrimitiveSequenceType where TraitType == CompletableTrait, ElementType
return PrimitiveSequence(raw: Observable.empty())
}

/**
Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully.

- seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)

- returns: An observable sequence that contains the elements of each given sequence, in sequential order.
*/
public static func concat<S: Sequence>(_ sequence: S) -> PrimitiveSequence<CompletableTrait, Never>
where S.Iterator.Element == PrimitiveSequence<CompletableTrait, Never> {
let source = Observable.concat(sequence.lazy.map { $0.asObservable() })
return PrimitiveSequence<CompletableTrait, Never>(raw: source)
}

/**
Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully.

- seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)

- returns: An observable sequence that contains the elements of each given sequence, in sequential order.
*/
public static func concat<C: Collection>(_ collection: C) -> PrimitiveSequence<CompletableTrait, Never>
where C.Iterator.Element == PrimitiveSequence<CompletableTrait, Never> {
let source = Observable.concat(collection.map { $0.asObservable() })
return PrimitiveSequence<CompletableTrait, Never>(raw: source)
}

/**
Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully.

- seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)

- returns: An observable sequence that contains the elements of each given sequence, in sequential order.
*/
public static func concat(_ sources: PrimitiveSequence<CompletableTrait, Never> ...) -> PrimitiveSequence<CompletableTrait, Never> {
let source = Observable.concat(sources.map { $0.asObservable() })
return PrimitiveSequence<CompletableTrait, Never>(raw: source)
}

/**
Merges elements from all observable sequences from collection into a single observable sequence.

Expand Down
1 change: 1 addition & 0 deletions Sources/AllTestz/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ final class PrimitiveSequenceTest_ : PrimitiveSequenceTest, RxTestCase {
("testAsCompletable_subscribeOnCompleted", PrimitiveSequenceTest.testAsCompletable_subscribeOnCompleted),
("testAsCompletable_subscribeOnError", PrimitiveSequenceTest.testAsCompletable_subscribeOnError),
("testCompletable_merge", PrimitiveSequenceTest.testCompletable_merge),
("testCompletable_concat", PrimitiveSequenceTest.testCompletable_concat),
("testDebug_producesSingleElement", PrimitiveSequenceTest.testDebug_producesSingleElement),
] }
}
Expand Down
40 changes: 40 additions & 0 deletions Tests/RxSwiftTests/PrimitiveSequenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,46 @@ extension PrimitiveSequenceTest {
])
}
}

func testCompletable_concat() {
let factories: [(Completable, Completable) -> Completable] =
[
{ ys1, ys2 in Completable.concat(ys1, ys2) },
{ ys1, ys2 in Completable.concat([ys1, ys2]) },
{ ys1, ys2 in Completable.concat(AnyCollection([ys1, ys2])) },
{ ys1, ys2 in ys1.concat(ys2) }
]

for factory in factories {
let scheduler = TestScheduler(initialClock: 0)

let ys1 = scheduler.createHotObservable([
completed(250, Never.self),
error(260, testError)
])

let ys2 = scheduler.createHotObservable([
completed(300, Never.self)
])

let res = scheduler.start { () -> Observable<Never> in
let completable: Completable = factory(ys1.asCompletable(), ys2.asCompletable())
return completable.asObservable()
}

XCTAssertEqual(res.events, [
completed(300)
])

XCTAssertEqual(ys1.subscriptions, [
Subscription(200, 250),
])

XCTAssertEqual(ys2.subscriptions, [
Subscription(250, 300),
])
}
}
}

extension PrimitiveSequenceTest {
Expand Down

0 comments on commit 1a8e7f4

Please sign in to comment.