use deferred

This commit is contained in:
Ivan Smolin 2016-11-23 18:51:29 +03:00
parent e69531c9ae
commit 71862c7f0f
2 changed files with 7 additions and 16 deletions

View File

@ -38,11 +38,9 @@ public class FixedPageCursor<Cursor: CursorType>: CursorType where Cursor.LoadRe
}
public func loadNextBatch() -> Observable<LoadResultType> {
return Observable.create { observer in
return Observable.deferred {
if self.exhausted {
observer.onError(CursorError.exhausted)
return Disposables.create()
throw CursorError.exhausted
}
let restOfLoaded = self.cursor.count - self.count
@ -51,14 +49,11 @@ public class FixedPageCursor<Cursor: CursorType>: CursorType where Cursor.LoadRe
let startIndex = self.count
self.count += min(restOfLoaded, self.pageSize)
observer.onNext(startIndex..<self.count)
return Disposables.create()
return Observable.just(startIndex..<self.count)
}
return self.cursor.loadNextBatch()
.map { _ in self.loadNextBatch() }
.subscribe()
.flatMap { _ in self.loadNextBatch() }
}
}

View File

@ -31,20 +31,16 @@ public class StaticCursor<Element>: CursorType {
}
public func loadNextBatch() -> Observable<LoadResultType> {
return Observable.create { observer in
return Observable.deferred {
if self.exhausted {
observer.onError(CursorError.exhausted)
return Disposables.create()
throw CursorError.exhausted
}
self.count = self.content.count
self.exhausted = true
observer.onNext(0..<self.count)
return Disposables.create()
return Observable.just(0..<self.count)
}
}