Skip to content

Commit 646a78a

Browse files
committed
fetch function refactoring
1 parent 6258ffb commit 646a78a

File tree

1 file changed

+52
-58
lines changed

1 file changed

+52
-58
lines changed

src/ui-scroll.js

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,6 @@ angular.module('ui.scroll', [])
787787
fetch(rid);
788788
adapter.loading(true);
789789
}
790-
791790
} else if (viewport.shouldLoadTop() && (keepFetching || pending[0])) {
792791
// pending[0] = true means that previous fetch was appending. We need to force at least one prepend
793792
// BTW there will always be at least 1 element in the pending array because bottom is fetched first
@@ -796,92 +795,87 @@ angular.module('ui.scroll', [])
796795
adapter.loading(true);
797796
}
798797
}
799-
800798
}
801799

802800
function adjustBuffer(rid) {
803801
// We need the item bindings to be processed before we can do adjustment
804-
return $timeout(() => {
805-
processBufferedItems(rid);
802+
$timeout(() => {
806803

804+
processBufferedItems(rid);
807805
enqueueFetch(rid, true);
808806

809807
if (!pending.length) {
810-
return adapter.calculateProperties();
808+
adapter.calculateProperties();
811809
}
812810
});
813811
}
814812

815813
function adjustBufferAfterFetch(rid) {
816814
// We need the item bindings to be processed before we can do adjustment
817-
return $timeout(() => {
815+
$timeout(() => {
818816

819817
enqueueFetch(rid, processBufferedItems(rid));
820818
pending.shift();
821819

822-
if (!pending.length) {
820+
if (pending.length)
821+
fetch(rid);
822+
else {
823823
adapter.loading(false);
824824
bindEvents();
825-
return adapter.calculateProperties();
825+
adapter.calculateProperties();
826826
}
827-
828-
return fetch(rid);
829827
});
830828
}
831829

832830
function fetch(rid) {
833831
if (pending[0]) {// scrolling down
834832
if (buffer.length && !viewport.shouldLoadBottom()) {
835-
return adjustBufferAfterFetch(rid);
836-
}
837-
838-
return fetchNext((result) => {
839-
if ((rid && rid !== ridActual) || $scope.$$destroyed) {
840-
return;
841-
}
842-
843-
if (result.length < bufferSize) {
844-
buffer.eof = true;
845-
// log 'eof is reached'
846-
}
847-
848-
if (result.length > 0) {
849-
viewport.clipTop();
850-
buffer.append(result);
851-
}
852-
853-
buffer.setUpper();
854-
855-
return adjustBufferAfterFetch(rid);
856-
});
857-
}
858-
859-
// scrolling up
860-
if (buffer.length && !viewport.shouldLoadTop()) {
861-
return adjustBufferAfterFetch(rid);
862-
}
863-
864-
return fetchPrevious((result) => {
865-
if ((rid && rid !== ridActual) || $scope.$$destroyed) {
866-
return;
867-
}
868-
869-
if (result.length < bufferSize) {
870-
buffer.bof = true;
871-
// log 'bof is reached'
833+
adjustBufferAfterFetch(rid);
834+
} else {
835+
fetchNext((result) => {
836+
if ((rid && rid !== ridActual) || $scope.$$destroyed) {
837+
return;
838+
}
839+
840+
if (result.length < bufferSize) {
841+
buffer.eof = true;
842+
}
843+
844+
if (result.length > 0) {
845+
viewport.clipTop();
846+
buffer.append(result);
847+
buffer.setUpper();
848+
}
849+
850+
adjustBufferAfterFetch(rid);
851+
});
872852
}
873-
874-
if (result.length > 0) {
875-
if (buffer.length) {
876-
viewport.clipBottom();
877-
}
878-
buffer.prepend(result);
853+
} else { // scrolling up
854+
if (buffer.length && !viewport.shouldLoadTop()) {
855+
adjustBufferAfterFetch(rid);
856+
} else {
857+
fetchPrevious((result) => {
858+
if ((rid && rid !== ridActual) || $scope.$$destroyed) {
859+
return;
860+
}
861+
862+
if (result.length < bufferSize) {
863+
buffer.bof = true;
864+
// log 'bof is reached'
865+
}
866+
867+
if (result.length > 0) {
868+
if (buffer.length) {
869+
viewport.clipBottom();
870+
}
871+
buffer.prepend(result);
872+
buffer.setLower();
873+
}
874+
875+
adjustBufferAfterFetch(rid);
876+
});
879877
}
880-
881-
buffer.setLower();
882-
883-
return adjustBufferAfterFetch(rid);
884-
});
878+
}
885879
}
886880

887881
function resizeAndScrollHandler() {

0 commit comments

Comments
 (0)