From e4c2fd8f67d0359cb6fa7fd75043b0477bfe0371 Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Wed, 10 May 2017 17:46:20 +0300 Subject: [PATCH] PR notes #1 --- .../Classes/Cursors/FixedPageCursor.swift | 26 ++++++++++--------- .../Classes/Cursors/SingleLoadCursor.swift | 14 +++++----- .../Classes/Cursors/StaticCursor.swift | 18 +++++++------ 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/LeadKit/Sources/Classes/Cursors/FixedPageCursor.swift b/LeadKit/Sources/Classes/Cursors/FixedPageCursor.swift index 2d04a61e..22a6a1b8 100644 --- a/LeadKit/Sources/Classes/Cursors/FixedPageCursor.swift +++ b/LeadKit/Sources/Classes/Cursors/FixedPageCursor.swift @@ -50,22 +50,24 @@ public class FixedPageCursor: 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..: 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]) { diff --git a/LeadKit/Sources/Classes/Cursors/StaticCursor.swift b/LeadKit/Sources/Classes/Cursors/StaticCursor.swift index 60289d52..144cba0c 100644 --- a/LeadKit/Sources/Classes/Cursors/StaticCursor.swift +++ b/LeadKit/Sources/Classes/Cursors/StaticCursor.swift @@ -47,15 +47,17 @@ public class StaticCursor: 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) } }