From 007af77912b39d84857a8e90eecdd02dd20164de Mon Sep 17 00:00:00 2001 From: Krunoslav Zaher Date: Sat, 11 Feb 2017 22:10:16 +0100 Subject: [PATCH] Renames `abstractMethod` to `rxAbstractMethod`. --- RxCocoa/RxCocoa.swift | 20 +++++++++++++------ .../RxCollectionViewDataSourceProxy.swift | 2 +- .../Proxies/RxTableViewDataSourceProxy.swift | 2 +- RxSwift/Observable.swift | 2 +- .../Implementations/CombineLatest.swift | 2 +- .../ConnectableObservable.swift | 2 +- RxSwift/Observables/Implementations/Do.swift | 5 ++++- .../Observables/Implementations/Merge.swift | 2 +- .../Implementations/Producer.swift | 2 +- .../Observables/Implementations/Switch.swift | 2 +- RxSwift/Observables/Implementations/Zip.swift | 4 ++-- RxSwift/Observers/ObserverBase.swift | 2 +- RxSwift/Observers/TailRecursiveSink.swift | 4 ++-- RxSwift/Rx.swift | 14 ++++++++++--- RxSwift/Subjects/ReplaySubject.swift | 10 +++++----- 15 files changed, 47 insertions(+), 28 deletions(-) diff --git a/RxCocoa/RxCocoa.swift b/RxCocoa/RxCocoa.swift index 0bd1b35b..37fb82e7 100644 --- a/RxCocoa/RxCocoa.swift +++ b/RxCocoa/RxCocoa.swift @@ -73,14 +73,22 @@ func bindingErrorToInterface(_ error: Swift.Error) { #endif } -// MARK: Abstract methods - -func rxAbstractMethodWithMessage(_ message: String) -> Swift.Never { - rxFatalError(message) +/// Swift does not implement abstract methods. This method is used as a runtime check to ensure that methods which intended to be abstract (i.e., they should be implemented in subclasses) are not called directly on the superclass. +func rxAbstractMethod(message: String = "Abstract method", file: StaticString = #file, line: UInt = #line) -> Swift.Never { + rxFatalError(message, file: file, line: line) } -func rxAbstractMethod() -> Swift.Never { - rxFatalError("Abstract method") +func rxFatalError(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) -> Swift.Never { + // The temptation to comment this line is great, but please don't, it's for your own good. The choice is yours. + fatalError(lastMessage(), file: file, line: line) +} + +func rxFatalErrorInDebug(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) { + #if DEBUG + fatalError(lastMessage(), file: file, line: line) + #else + print("\(file):\(line): \(lastMessage())") + #endif } // MARK: casts or fatal error diff --git a/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift b/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift index 9834c217..071d1211 100644 --- a/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift +++ b/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift @@ -26,7 +26,7 @@ final class CollectionViewDataSourceNotSet // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - rxAbstractMethodWithMessage(dataSourceNotSet) + rxAbstractMethod(message: dataSourceNotSet) } } diff --git a/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift b/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift index f3df05f9..177a4efb 100644 --- a/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift +++ b/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift @@ -24,7 +24,7 @@ final class TableViewDataSourceNotSet } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - rxAbstractMethodWithMessage(dataSourceNotSet) + rxAbstractMethod(message: dataSourceNotSet) } } diff --git a/RxSwift/Observable.swift b/RxSwift/Observable.swift index e1a03f23..44aa1deb 100644 --- a/RxSwift/Observable.swift +++ b/RxSwift/Observable.swift @@ -20,7 +20,7 @@ public class Observable : ObservableType { } public func subscribe(_ observer: O) -> Disposable where O.E == E { - abstractMethod() + rxAbstractMethod() } public func asObservable() -> Observable { diff --git a/RxSwift/Observables/Implementations/CombineLatest.swift b/RxSwift/Observables/Implementations/CombineLatest.swift index 25b0da73..8c03e8c3 100644 --- a/RxSwift/Observables/Implementations/CombineLatest.swift +++ b/RxSwift/Observables/Implementations/CombineLatest.swift @@ -34,7 +34,7 @@ class CombineLatestSink } func getResult() throws -> Element { - abstractMethod() + rxAbstractMethod() } func next(_ index: Int) { diff --git a/RxSwift/Observables/Implementations/ConnectableObservable.swift b/RxSwift/Observables/Implementations/ConnectableObservable.swift index 69689525..5557a897 100644 --- a/RxSwift/Observables/Implementations/ConnectableObservable.swift +++ b/RxSwift/Observables/Implementations/ConnectableObservable.swift @@ -19,7 +19,7 @@ public class ConnectableObservable - returns: Disposable used to disconnect the observable wrapper from its source, causing subscribed observer to stop receiving values from the underlying observable sequence. */ public func connect() -> Disposable { - abstractMethod() + rxAbstractMethod() } } diff --git a/RxSwift/Observables/Implementations/Do.swift b/RxSwift/Observables/Implementations/Do.swift index 28a4c495..db33ebaa 100644 --- a/RxSwift/Observables/Implementations/Do.swift +++ b/RxSwift/Observables/Implementations/Do.swift @@ -38,12 +38,14 @@ final class Do : Producer { fileprivate let _source: Observable fileprivate let _eventHandler: EventHandler fileprivate let _onSubscribe: (() -> ())? + fileprivate let _onSubscribed: (() -> ())? fileprivate let _onDispose: (() -> ())? - init(source: Observable, eventHandler: @escaping EventHandler, onSubscribe: (() -> ())?, onDispose: (() -> ())?) { + init(source: Observable, eventHandler: @escaping EventHandler, onSubscribe: (() -> ())?, onSubscribed: (() -> ())?, onDispose: (() -> ())?) { _source = source _eventHandler = eventHandler _onSubscribe = onSubscribe + _onSubscribed = onSubscribed _onDispose = onDispose } @@ -51,6 +53,7 @@ final class Do : Producer { _onSubscribe?() let sink = DoSink(parent: self, observer: observer, cancel: cancel) let subscription = _source.subscribe(sink) + _onSubscribed?() let onDispose = _onDispose let allSubscriptions = Disposables.create { subscription.dispose() diff --git a/RxSwift/Observables/Implementations/Merge.swift b/RxSwift/Observables/Implementations/Merge.swift index e81812f6..aa18ae24 100644 --- a/RxSwift/Observables/Implementations/Merge.swift +++ b/RxSwift/Observables/Implementations/Merge.swift @@ -291,7 +291,7 @@ class MergeSink } func performMap(_ element: SourceType) throws -> S { - abstractMethod() + rxAbstractMethod() } func on(_ event: Event) { diff --git a/RxSwift/Observables/Implementations/Producer.swift b/RxSwift/Observables/Implementations/Producer.swift index c23ba3cf..996b0110 100644 --- a/RxSwift/Observables/Implementations/Producer.swift +++ b/RxSwift/Observables/Implementations/Producer.swift @@ -32,7 +32,7 @@ class Producer : Observable { } func run(_ observer: O, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where O.E == Element { - abstractMethod() + rxAbstractMethod() } } diff --git a/RxSwift/Observables/Implementations/Switch.swift b/RxSwift/Observables/Implementations/Switch.swift index 187ad045..af1ccadf 100644 --- a/RxSwift/Observables/Implementations/Switch.swift +++ b/RxSwift/Observables/Implementations/Switch.swift @@ -38,7 +38,7 @@ class SwitchSink } func performMap(_ element: SourceType) throws -> S { - abstractMethod() + rxAbstractMethod() } func _synchronized_on(_ event: Event) { diff --git a/RxSwift/Observables/Implementations/Zip.swift b/RxSwift/Observables/Implementations/Zip.swift index 0daad0f3..a283bf2b 100644 --- a/RxSwift/Observables/Implementations/Zip.swift +++ b/RxSwift/Observables/Implementations/Zip.swift @@ -31,11 +31,11 @@ class ZipSink : Sink, ZipSinkProtocol { } func getResult() throws -> Element { - abstractMethod() + rxAbstractMethod() } func hasElements(_ index: Int) -> Bool { - abstractMethod() + rxAbstractMethod() } func next(_ index: Int) { diff --git a/RxSwift/Observers/ObserverBase.swift b/RxSwift/Observers/ObserverBase.swift index 08cd068c..96e5fdde 100644 --- a/RxSwift/Observers/ObserverBase.swift +++ b/RxSwift/Observers/ObserverBase.swift @@ -28,7 +28,7 @@ class ObserverBase : Disposable, ObserverType { } func onCore(_ event: Event) { - abstractMethod() + rxAbstractMethod() } func dispose() { diff --git a/RxSwift/Observers/TailRecursiveSink.swift b/RxSwift/Observers/TailRecursiveSink.swift index 4c992103..332e6d2e 100644 --- a/RxSwift/Observers/TailRecursiveSink.swift +++ b/RxSwift/Observers/TailRecursiveSink.swift @@ -62,7 +62,7 @@ class TailRecursiveSink } func extract(_ observable: Observable) -> SequenceGenerator? { - abstractMethod() + rxAbstractMethod() } // should be done on gate locked @@ -131,7 +131,7 @@ class TailRecursiveSink } func subscribeToNext(_ source: Observable) -> Disposable { - abstractMethod() + rxAbstractMethod() } func disposeCommand() { diff --git a/RxSwift/Rx.swift b/RxSwift/Rx.swift index b5f31690..93b066e7 100644 --- a/RxSwift/Rx.swift +++ b/RxSwift/Rx.swift @@ -33,13 +33,21 @@ #endif /// Swift does not implement abstract methods. This method is used as a runtime check to ensure that methods which intended to be abstract (i.e., they should be implemented in subclasses) are not called directly on the superclass. -func abstractMethod(file: StaticString = #file, line: UInt = #line) -> Swift.Never { +func rxAbstractMethod(file: StaticString = #file, line: UInt = #line) -> Swift.Never { rxFatalError("Abstract method", file: file, line: line) } -func rxFatalError(_ lastMessage: String, file: StaticString = #file, line: UInt = #line) -> Swift.Never { +func rxFatalError(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) -> Swift.Never { // The temptation to comment this line is great, but please don't, it's for your own good. The choice is yours. - fatalError(lastMessage, file: file, line: line) + fatalError(lastMessage(), file: file, line: line) +} + +func rxFatalErrorInDebug(_ lastMessage: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) { + #if DEBUG + fatalError(lastMessage(), file: file, line: line) + #else + print("\(file):\(line): \(lastMessage())") + #endif } func incrementChecked(_ i: inout Int) throws -> Int { diff --git a/RxSwift/Subjects/ReplaySubject.swift b/RxSwift/Subjects/ReplaySubject.swift index b8a1f5fe..250439a3 100644 --- a/RxSwift/Subjects/ReplaySubject.swift +++ b/RxSwift/Subjects/ReplaySubject.swift @@ -40,7 +40,7 @@ public class ReplaySubject fileprivate var _observers = Observers() func unsubscribe(_ key: DisposeKey) { - abstractMethod() + rxAbstractMethod() } final var isStopped: Bool { @@ -51,7 +51,7 @@ public class ReplaySubject /// /// - parameter event: Event to send to the observers. public func on(_ event: Event) { - abstractMethod() + rxAbstractMethod() } /// Returns observer interface for subject. @@ -99,15 +99,15 @@ fileprivate class ReplayBufferBase , SynchronizedUnsubscribeType { func trim() { - abstractMethod() + rxAbstractMethod() } func addValueToBuffer(_ value: Element) { - abstractMethod() + rxAbstractMethod() } func replayBuffer(_ observer: O) where O.E == Element { - abstractMethod() + rxAbstractMethod() } override func on(_ event: Event) {