Skip to content

Commit 112cf45

Browse files
authored
Merge pull request #125 from mosamer/primitive-sequence-init
Allow work factories with other return types than `Observable`
2 parents d4152d3 + f90e164 commit 112cf45

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

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

79
3.4.0
810
-----

Sources/Action/Action.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import RxCocoa
44

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

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

5355
private let disposeBag = DisposeBag()
5456

57+
public convenience init<O: ObservableConvertibleType>(
58+
enabledIf: Observable<Bool> = Observable.just(true),
59+
workFactory: @escaping (Input) -> O
60+
) where O.E == Element {
61+
self.init(enabledIf: enabledIf) {
62+
workFactory($0).asObservable()
63+
}
64+
}
65+
5566
public init(
5667
enabledIf: Observable<Bool> = Observable.just(true),
5768
workFactory: @escaping WorkFactory) {
58-
69+
5970
self._enabledIf = enabledIf
6071
self.workFactory = workFactory
6172

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

6576
let errorsSubject = PublishSubject<ActionError>()
6677
errors = errorsSubject.asObservable()
67-
78+
6879
executionObservables = inputs
6980
.withLatestFrom(enabled) { input, enabled in (input, enabled) }
7081
.flatMap { input, enabled -> Observable<Observable<Element>> in
@@ -108,10 +119,10 @@ public final class Action<Input, Element> {
108119
}
109120

110121
let subject = ReplaySubject<Element>.createUnbounded()
111-
122+
112123
let work = executionObservables
113124
.map { $0.catchError { throw ActionError.underlyingError($0) } }
114-
125+
115126
let error = errors
116127
.map { Observable<Element>.error($0) }
117128

@@ -120,7 +131,7 @@ public final class Action<Input, Element> {
120131
.flatMap { $0 }
121132
.subscribe(subject)
122133
.disposed(by: disposeBag)
123-
134+
124135
return subject.asObservable()
125136
}
126137
}

Tests/ActionTests/ActionTests.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,35 @@ class ActionTests: QuickSpec {
1414
scheduler = TestScheduler(initialClock: 0)
1515
disposeBag = DisposeBag()
1616
}
17-
17+
18+
describe("completable action") {
19+
var action: CompletableAction<String>!
20+
beforeEach {
21+
let work: Completable = Observable<Never>.empty().asCompletable()
22+
action = CompletableAction {_ in work }
23+
scheduler.scheduleAt(10) { action.inputs.onNext("a") }
24+
scheduler.scheduleAt(20) { action.inputs.onNext("b") }
25+
}
26+
afterEach {
27+
action = nil
28+
}
29+
it("inputs subject receives generated inputs") {
30+
let inputs = scheduler.createObserver(String.self)
31+
action.inputs.bind(to: inputs).disposed(by: disposeBag)
32+
scheduler.start()
33+
XCTAssertEqual(inputs.events, [
34+
next(10, "a"),
35+
next(20, "b"),
36+
])
37+
}
38+
it("emits nothing on `elements`") {
39+
let elements = scheduler.createObserver(Never.self)
40+
action.elements.bind(to: elements).disposed(by: disposeBag)
41+
scheduler.start()
42+
XCTAssertEqual(elements.events.count, 0)
43+
}
44+
}
45+
1846
describe("action properties") {
1947
var inputs: TestableObserver<String>!
2048
var elements: TestableObserver<String>!

0 commit comments

Comments
 (0)