Skip to content

Commit 9d8a646

Browse files
committed
Converting to Swift 3 and RxSwift develop
1 parent c23b381 commit 9d8a646

File tree

14 files changed

+1358
-630
lines changed

14 files changed

+1358
-630
lines changed

Action.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Pod::Spec.new do |s|
2121
s.source_files = "*.{swift}"
2222

2323
s.frameworks = "Foundation"
24-
s.dependency "RxSwift", '~> 2.6'
25-
s.dependency "RxCocoa", '~> 2.6'
24+
s.dependency "RxSwift", 'develop'
25+
s.dependency "RxCocoa", 'develop'
2626

2727
s.watchos.exclude_files = "UIButton+Rx.swift", "UIBarButtonItem+Action.swift", "AlertAction.swift"
2828
s.osx.exclude_files = "UIButton+Rx.swift", "UIBarButtonItem+Action.swift", "AlertAction.swift"

Action.swift

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import RxCocoa
66
public typealias CocoaAction = Action<Void, Void>
77

88
/// Possible errors from invoking execute()
9-
public enum ActionError: ErrorType {
10-
case NotEnabled
11-
case UnderlyingError(ErrorType)
9+
public enum ActionError: Error {
10+
case notEnabled
11+
case underlyingError(Error)
1212
}
1313

1414
/// TODO: Add some documentation.
1515
public final class Action<Input, Element> {
16-
public typealias WorkFactory = Input -> Observable<Element>
16+
public typealias WorkFactory = (Input) -> Observable<Element>
1717

1818
public let _enabledIf: Observable<Bool>
1919
public let workFactory: WorkFactory
@@ -23,83 +23,75 @@ public final class Action<Input, Element> {
2323
/// All inputs are always appear in this subject even if the action is not enabled.
2424
/// Thus, inputs count equals elements count + errors count.
2525
public let inputs = PublishSubject<Input>()
26-
private let _completed = PublishSubject<Void>()
26+
fileprivate let _completed = PublishSubject<Void>()
2727

2828
/// Errors aggrevated from invocations of execute().
2929
/// Delivered on whatever scheduler they were sent from.
3030
public var errors: Observable<ActionError> {
3131
return self._errors.asObservable()
3232
}
33-
private let _errors = PublishSubject<ActionError>()
33+
fileprivate let _errors = PublishSubject<ActionError>()
3434

3535
/// Whether or not we're currently executing.
3636
/// Delivered on whatever scheduler they were sent from.
3737
public var elements: Observable<Element> {
3838
return self._elements.asObservable()
3939
}
40-
private let _elements = PublishSubject<Element>()
40+
fileprivate let _elements = PublishSubject<Element>()
4141

4242
/// Whether or not we're currently executing.
4343
/// Always observed on MainScheduler.
4444
public var executing: Observable<Bool> {
4545
return self._executing.asObservable().observeOn(MainScheduler.instance)
4646
}
47-
private let _executing = Variable(false)
47+
fileprivate let _executing = Variable(false)
4848

4949
/// Observables returned by the workFactory.
5050
/// Useful for sending results back from work being completed
5151
/// e.g. response from a network call.
5252
public var executionObservables: Observable<Observable<Element>> {
5353
return self._executionObservables.asObservable().observeOn(MainScheduler.instance)
5454
}
55-
private let _executionObservables = PublishSubject<Observable<Element>>()
55+
fileprivate let _executionObservables = PublishSubject<Observable<Element>>()
5656

5757
/// Whether or not we're enabled. Note that this is a *computed* sequence
5858
/// property based on enabledIf initializer and if we're currently executing.
5959
/// Always observed on MainScheduler.
6060
public var enabled: Observable<Bool> {
6161
return _enabled.asObservable().observeOn(MainScheduler.instance)
6262
}
63-
public private(set) var _enabled = BehaviorSubject(value: true)
63+
public fileprivate(set) var _enabled = BehaviorSubject(value: true)
6464

65-
private let executingQueue = dispatch_queue_create("com.ashfurrow.Action.executingQueue", DISPATCH_QUEUE_SERIAL)
66-
private let disposeBag = DisposeBag()
65+
fileprivate let executingQueue = DispatchQueue(label: "com.ashfurrow.Action.executingQueue", attributes: [])
66+
fileprivate let disposeBag = DisposeBag()
6767

68-
public init<B: BooleanType>(enabledIf: Observable<B>, workFactory: WorkFactory) {
69-
self._enabledIf = enabledIf.map { booleanType in
70-
return booleanType.boolValue
68+
public init<B: ExpressibleByBooleanLiteral>(enabledIf: Observable<B> = Observable.just(true), workFactory: @escaping WorkFactory) {
69+
self._enabledIf = enabledIf.map { (booleanLiteral) -> Bool in
70+
return booleanLiteral as! Bool
7171
}
72+
7273
self.workFactory = workFactory
7374

7475
Observable.combineLatest(self._enabledIf, self.executing) { (enabled, executing) -> Bool in
7576
return enabled && !executing
7677
}.bindTo(_enabled).addDisposableTo(disposeBag)
7778

78-
self.inputs
79-
.subscribeNext { [weak self] input in
80-
self?._execute(input)
81-
}
82-
.addDisposableTo(disposeBag)
79+
self.inputs.subscribe(onNext: { [weak self] (input) in
80+
self?._execute(input)
81+
}, onError: nil, onCompleted: nil, onDisposed: nil).addDisposableTo(disposeBag)
8382
}
8483
}
8584

86-
// MARK: Convenience initializers.
87-
public extension Action {
88-
89-
/// Always enabled.
90-
public convenience init(workFactory: WorkFactory) {
91-
self.init(enabledIf: .just(true), workFactory: workFactory)
92-
}
93-
}
9485

9586
// MARK: Execution!
9687
public extension Action {
9788

98-
public func execute(input: Input) -> Observable<Element> {
89+
@discardableResult
90+
public func execute(_ input: Input) -> Observable<Element> {
9991
let buffer = ReplaySubject<Element>.createUnbounded()
10092
let error = errors
10193
.flatMap { error -> Observable<Element> in
102-
if case .UnderlyingError(let error) = error {
94+
if case .underlyingError(let error) = error {
10395
throw error
10496
} else {
10597
return Observable.empty()
@@ -118,7 +110,8 @@ public extension Action {
118110
return buffer.asObservable()
119111
}
120112

121-
private func _execute(input: Input) -> Observable<Element> {
113+
@discardableResult
114+
fileprivate func _execute(_ input: Input) -> Observable<Element> {
122115

123116
// Buffer from the work to a replay subject.
124117
let buffer = ReplaySubject<Element>.createUnbounded()
@@ -134,9 +127,9 @@ public extension Action {
134127

135128
// Make sure we started executing and we're accidentally disabled.
136129
guard startedExecuting else {
137-
let error = ActionError.NotEnabled
130+
let error = ActionError.notEnabled
138131
self._errors.onNext(error)
139-
buffer.onError(error)
132+
buffer.onError(error as Error)
140133

141134
return buffer
142135
}
@@ -153,7 +146,7 @@ public extension Action {
153146
self?._elements.onNext(element)
154147
},
155148
onError: {[weak self] error in
156-
self?._errors.onNext(ActionError.UnderlyingError(error))
149+
self?._errors.onNext(ActionError.underlyingError(error))
157150
},
158151
onCompleted: {[weak self] in
159152
self?._completed.onNext()
@@ -169,12 +162,12 @@ public extension Action {
169162
}
170163

171164
private extension Action {
172-
private func doLocked(closure: () -> Void) {
173-
dispatch_sync(executingQueue, closure)
165+
func doLocked(_ closure: () -> Void) {
166+
executingQueue.sync(execute: closure)
174167
}
175168
}
176169

177-
internal extension BehaviorSubject where Element: BooleanLiteralConvertible {
170+
internal extension BehaviorSubject where Element: ExpressibleByBooleanLiteral {
178171
var valueOrFalse: Element {
179172
guard let value = try? value() else { return false }
180173

Action.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
PRODUCT_NAME = "$(TARGET_NAME)";
290290
SKIP_INSTALL = YES;
291291
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
292-
SWIFT_VERSION = 2.3;
292+
SWIFT_VERSION = 3.0;
293293
};
294294
name = Debug;
295295
};
@@ -312,7 +312,7 @@
312312
PRODUCT_BUNDLE_IDENTIFIER = com.ashfurrow.Action;
313313
PRODUCT_NAME = "$(TARGET_NAME)";
314314
SKIP_INSTALL = YES;
315-
SWIFT_VERSION = 2.3;
315+
SWIFT_VERSION = 3.0;
316316
};
317317
name = Release;
318318
};

AlertAction.swift

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

55
public extension UIAlertAction {
66

7-
public static func Action(title: String?, style: UIAlertActionStyle) -> UIAlertAction {
7+
public static func Action(_ title: String?, style: UIAlertActionStyle) -> UIAlertAction {
88
return UIAlertAction(title: title, style: style, handler: { action in
99
action.rx_action?.execute()
1010
})
@@ -48,17 +48,17 @@ extension UIAlertAction {
4848
MainScheduler.ensureExecutingOnScheduler()
4949

5050
switch event {
51-
case .Next(let value):
52-
self?.enabled = value
53-
case .Error(let error):
51+
case .next(let value):
52+
self?.isEnabled = value
53+
case .error(let error):
5454
let error = "Binding error to UI: \(error)"
5555
#if DEBUG
5656
rxFatalError(error)
5757
#else
5858
print(error)
5959
#endif
6060
break
61-
case .Completed:
61+
case .completed:
6262
break
6363
}
6464
}

Cartfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "ReactiveX/RxSwift" ~> 2.0
1+
github "ReactiveX/RxSwift" "develop"

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "ReactiveX/RxSwift" "2.6.0"
1+
github "ReactiveX/RxSwift" "c99a49aa66ea830780bd3be1f11b3bac2b690c90"

0 commit comments

Comments
 (0)