Skip to content
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

Allow work factories with other return types than Observable #125

Merged
merged 4 commits into from
Nov 20, 2017
Merged
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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

Current master
--------------
- Add convenience initializer with work factories returning `PrimitiveSequence` or any other `ObservableConvertibleType` [#125](https://github.com/RxSwiftCommunity/Action/pull/125)
- Introduce `CompletableAction`, a typealias for action that only completes without emitting any elements [#125](https://github.com/RxSwiftCommunity/Action/pull/125)

3.4.0
-----
Expand Down
21 changes: 16 additions & 5 deletions Sources/Action/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import RxCocoa

/// Typealias for compatibility with UIButton's rx.action property.
public typealias CocoaAction = Action<Void, Void>
/// Typealias for actions with work factory returns `Completable`.
public typealias CompletableAction<Input> = Action<Input, Never>

/// Possible errors from invoking execute()
public enum ActionError: Error {
Expand Down Expand Up @@ -52,10 +54,19 @@ public final class Action<Input, Element> {

private let disposeBag = DisposeBag()

public convenience init<O: ObservableConvertibleType>(
enabledIf: Observable<Bool> = Observable.just(true),
workFactory: @escaping (Input) -> O
) where O.E == Element {
self.init(enabledIf: enabledIf) {
workFactory($0).asObservable()
}
}

public init(
enabledIf: Observable<Bool> = Observable.just(true),
workFactory: @escaping WorkFactory) {

self._enabledIf = enabledIf
self.workFactory = workFactory

Expand All @@ -64,7 +75,7 @@ public final class Action<Input, Element> {

let errorsSubject = PublishSubject<ActionError>()
errors = errorsSubject.asObservable()

executionObservables = inputs
.withLatestFrom(enabled) { input, enabled in (input, enabled) }
.flatMap { input, enabled -> Observable<Observable<Element>> in
Expand Down Expand Up @@ -108,10 +119,10 @@ public final class Action<Input, Element> {
}

let subject = ReplaySubject<Element>.createUnbounded()

let work = executionObservables
.map { $0.catchError { throw ActionError.underlyingError($0) } }

let error = errors
.map { Observable<Element>.error($0) }

Expand All @@ -120,7 +131,7 @@ public final class Action<Input, Element> {
.flatMap { $0 }
.subscribe(subject)
.disposed(by: disposeBag)

return subject.asObservable()
}
}
30 changes: 29 additions & 1 deletion Tests/ActionTests/ActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,35 @@ class ActionTests: QuickSpec {
scheduler = TestScheduler(initialClock: 0)
disposeBag = DisposeBag()
}


describe("completable action") {
var action: CompletableAction<String>!
beforeEach {
let work: Completable = Observable<Never>.empty().asCompletable()
action = CompletableAction {_ in work }
scheduler.scheduleAt(10) { action.inputs.onNext("a") }
scheduler.scheduleAt(20) { action.inputs.onNext("b") }
}
afterEach {
action = nil
}
it("inputs subject receives generated inputs") {
let inputs = scheduler.createObserver(String.self)
action.inputs.bind(to: inputs).disposed(by: disposeBag)
scheduler.start()
XCTAssertEqual(inputs.events, [
next(10, "a"),
next(20, "b"),
])
}
it("emits nothing on `elements`") {
let elements = scheduler.createObserver(Never.self)
action.elements.bind(to: elements).disposed(by: disposeBag)
scheduler.start()
XCTAssertEqual(elements.events.count, 0)
}
}

describe("action properties") {
var inputs: TestableObserver<String>!
var elements: TestableObserver<String>!
Expand Down