Skip to content

Add convinience Infallible operator for setting SchedulerType(subscribe(on:), observe(on:)) #2576

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* Add convinience `Infallible` operator for setting `SchedulerType`(`subscribe(on:)`, `observe(on:)`). #3340
* Use `AtomicInt` for `BooleanDisposable`s to prevent potential rase condition. #2419
* Renames 'OSX' to 'macOS' in Availability Check.
* Renames 'OSXApplicationExtension' to 'macOSApplicationExtension' in Availability Check.
Expand Down
41 changes: 40 additions & 1 deletion RxSwift/Traits/Infallible/Infallible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public protocol InfallibleType: ObservableConvertibleType {}
/// Unlike `SharedSequence`, it does not share its resources or
/// replay its events, but acts as a standard `Observable`.
public struct Infallible<Element>: InfallibleType {
private let source: Observable<Element>
let source: Observable<Element>

init(_ source: Observable<Element>) {
self.source = source
Expand Down Expand Up @@ -100,3 +100,42 @@ extension InfallibleType {
return self.asObservable().subscribe(eventHandler)
}
}


extension Infallible {
/**
Wraps the source sequence in order to run its subscription and unsubscription logic on the specified
scheduler.

This operation is not commonly used.

This only performs the side-effects of subscription and unsubscription on the specified scheduler.

In order to invoke observer callbacks on a `scheduler`, use `observeOn`.

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

- parameter scheduler: Scheduler to perform subscription and unsubscription actions on.
- returns: The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.
*/
public func subscribe(on scheduler: ImmediateSchedulerType)
-> Infallible<Element> {
Infallible(self.source.subscribe(on: scheduler))
}

/**
Wraps the source sequence in order to run its observer callbacks on the specified scheduler.

This only invokes observer callbacks on a `scheduler`. In case the subscription and/or unsubscription
actions have side-effects that require to be run on a scheduler, use `subscribeOn`.

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

- parameter scheduler: Scheduler to notify observers on.
- returns: The source sequence whose observations happen on the specified scheduler.
*/
public func observe(on scheduler: ImmediateSchedulerType)
-> Infallible<Element> {
Infallible(self.source.observe(on: scheduler))
}
}
26 changes: 26 additions & 0 deletions Tests/RxSwiftTests/Infallible+Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,32 @@ extension InfallibleTest {
testObject = nil
XCTAssertNil(testObject)
}

func test_observeOn() {
let scheduler = TestScheduler(initialClock: 0)

let res = scheduler.start {
Infallible.just(1).observe(on:scheduler)
}

XCTAssertEqual(res.events, [
.next(201, 1),
.completed(202)
])
}

func test_subscribeOn() {
let scheduler = TestScheduler(initialClock: 0)

let res = scheduler.start {
Infallible.just(1).subscribe(on: scheduler)
}

XCTAssertEqual(res.events, [
.next(201, 1),
.completed(201)
])
}
}

private class TestObject: NSObject {
Expand Down