Improves `elementAt` to be platform consistent

This commit is contained in:
Junior B 2015-10-23 11:03:39 +02:00 committed by Krunoslav Zaher
parent 97e807080d
commit 61e8e33cd7
4 changed files with 24 additions and 4 deletions

View File

@ -26,13 +26,24 @@ class ElementAtSink<SourceType, O: ObserverType where O.E == SourceType> : Sink<
switch event {
case .Next(_):
if i < 0 {
rxFatalError("index can't be negative")
}
if (i == 0) {
observer?.on(event)
observer?.on(.Completed)
self.dispose()
}
i--
do {
try decrementChecked(&i)
} catch(let e) {
observer?.onError(e)
dispose()
return
}
case .Error(let e):
observer?.on(.Error(e))
self.dispose()

View File

@ -194,6 +194,6 @@ extension ObservableType {
@warn_unused_result(message="http://git.io/rxs.uo")
public func elementAt(index: Int)
-> Observable<E> {
return ElementAt(source: self.asObservable(), index: index, throwOnEmpty: false)
return ElementAt(source: self.asObservable(), index: index, throwOnEmpty: true)
}
}

View File

@ -37,6 +37,15 @@ func incrementChecked(inout i: Int) throws -> Int {
return result
}
func decrementChecked(inout i: Int) throws -> Int {
if i == Int.min {
throw RxError.OverflowError
}
let result = i
i -= 1
return result
}
extension NSObject {
func rx_synchronized<T>(@noescape action: () -> T) -> T {
objc_sync_enter(self)

View File

@ -3708,7 +3708,7 @@ extension ObservableStandardSequenceOperators {
}
XCTAssertEqual(res.messages, [
completed(320)
error(320, RxError.ArgumentOutOfRange)
])
XCTAssertEqual(xs.subscriptions, [