Skip to content

Commit 6258ffb

Browse files
committed
enqueue fetch refactoring
1 parent 7af1e99 commit 6258ffb

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/ui-scroll.js

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -690,16 +690,6 @@ angular.module('ui.scroll', [])
690690
return adjustBuffer(ridActual);
691691
}
692692

693-
function enqueueFetch(rid, direction) {
694-
if (!adapter.isLoading) {
695-
adapter.loading(true);
696-
}
697-
698-
if (pending.push(direction) === 1) {
699-
return fetch(rid);
700-
}
701-
}
702-
703693
function isElementVisible(wrapper) {
704694
return wrapper.element.height() && wrapper.element[0].offsetParent;
705695
}
@@ -790,16 +780,31 @@ angular.module('ui.scroll', [])
790780
return keepFetching;
791781
}
792782

783+
function enqueueFetch(rid, keepFetching) {
784+
if (viewport.shouldLoadBottom() && keepFetching) {
785+
// keepFetching = true means that at least one item app/prepended in the last batch had height > 0
786+
if (pending.push(true) === 1) {
787+
fetch(rid);
788+
adapter.loading(true);
789+
}
790+
791+
} else if (viewport.shouldLoadTop() && (keepFetching || pending[0])) {
792+
// pending[0] = true means that previous fetch was appending. We need to force at least one prepend
793+
// BTW there will always be at least 1 element in the pending array because bottom is fetched first
794+
if (pending.push(false) === 1) {
795+
fetch(rid);
796+
adapter.loading(true);
797+
}
798+
}
799+
800+
}
801+
793802
function adjustBuffer(rid) {
794803
// We need the item bindings to be processed before we can do adjustment
795804
return $timeout(() => {
796805
processBufferedItems(rid);
797806

798-
if (viewport.shouldLoadBottom()) {
799-
enqueueFetch(rid, true);
800-
} else if (viewport.shouldLoadTop()) {
801-
enqueueFetch(rid, false);
802-
}
807+
enqueueFetch(rid, true);
803808

804809
if (!pending.length) {
805810
return adapter.calculateProperties();
@@ -810,17 +815,8 @@ angular.module('ui.scroll', [])
810815
function adjustBufferAfterFetch(rid) {
811816
// We need the item bindings to be processed before we can do adjustment
812817
return $timeout(() => {
813-
let keepFetching = processBufferedItems(rid);
814-
815-
if (viewport.shouldLoadBottom() && keepFetching) {
816-
// keepFetching = true means that at least one item app/prepended in the last batch had height > 0
817-
enqueueFetch(rid, true);
818-
} else if (viewport.shouldLoadTop() && (keepFetching || pending[0])) {
819-
// pending[0] = true means that previous fetch was appending. We need to force at least one prepend
820-
// BTW there will always be at least 1 element in the pending array because bottom is fetched first
821-
enqueueFetch(rid, false);
822-
}
823818

819+
enqueueFetch(rid, processBufferedItems(rid));
824820
pending.shift();
825821

826822
if (!pending.length) {
@@ -890,11 +886,8 @@ angular.module('ui.scroll', [])
890886

891887
function resizeAndScrollHandler() {
892888
if (!$rootScope.$$phase && !adapter.isLoading) {
893-
if (viewport.shouldLoadBottom()) {
894-
enqueueFetch(ridActual, true);
895-
} else if (viewport.shouldLoadTop()) {
896-
enqueueFetch(ridActual, false);
897-
}
889+
890+
enqueueFetch(ridActual, true);
898891

899892
if (pending.length) {
900893
unbindEvents();

0 commit comments

Comments
 (0)