This commit is contained in:
Ivan Smolin 2017-05-10 17:46:20 +03:00
parent 72c0d4fc6e
commit e4c2fd8f67
3 changed files with 32 additions and 26 deletions

View File

@ -50,22 +50,24 @@ public class FixedPageCursor<Cursor: CursorType>: CursorType {
}
public func loadNextBatch() -> Single<[Cursor.Element]> {
if exhausted {
return .error(CursorError.exhausted)
}
return Single.deferred {
if self.exhausted {
return .error(CursorError.exhausted)
}
let restOfLoaded = cursor.count - count
let restOfLoaded = self.cursor.count - self.count
if restOfLoaded >= pageSize || cursor.exhausted {
let startIndex = count
count += min(restOfLoaded, pageSize)
if restOfLoaded >= self.pageSize || self.cursor.exhausted {
let startIndex = self.count
self.count += min(restOfLoaded, self.pageSize)
return .just(cursor[startIndex..<count])
}
return .just(self.cursor[startIndex..<self.count])
}
return cursor.loadNextBatch()
.flatMap { _ in
self.loadNextBatch()
return self.cursor.loadNextBatch()
.flatMap { _ in
self.loadNextBatch()
}
}
}

View File

@ -51,13 +51,15 @@ public class SingleLoadCursor<Element>: ResettableCursorType {
}
public func loadNextBatch() -> Single<[Element]> {
if exhausted {
return .error(CursorError.exhausted)
}
return Single.deferred {
if self.exhausted {
return .error(CursorError.exhausted)
}
return loadingObservable.do(onNext: { [weak self] newItems in
self?.onGot(result: newItems)
})
return self.loadingObservable.do(onNext: { [weak self] newItems in
self?.onGot(result: newItems)
})
}
}
private func onGot(result: [Element]) {

View File

@ -47,15 +47,17 @@ public class StaticCursor<Element>: ResettableCursorType {
}
public func loadNextBatch() -> Single<[Element]> {
if exhausted {
return .error(CursorError.exhausted)
return Single.deferred {
if self.exhausted {
return .error(CursorError.exhausted)
}
self.count = self.content.count
self.exhausted = true
return .just(self.content)
}
count = content.count
exhausted = true
return .just(content)
}
}