Skip to content

Commit

Permalink
Manage fetch concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVelezLl committed Dec 17, 2024
1 parent b7fa010 commit cefb966
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,8 @@ def recommendations_for(self, request, **kwargs):
# run into an issue where we hit a SQL parameters limit in the queries in here.
# If we find that this page size is too high, we should lower it, but for the reasons noted above, we
# should not raise it.
NUM_CHILDREN = 12
NUM_GRANDCHILDREN_PER_CHILD = 12
NUM_CHILDREN = 2
NUM_GRANDCHILDREN_PER_CHILD = 2


class TreeQueryMixin(object):
Expand Down
31 changes: 29 additions & 2 deletions kolibri/plugins/coach/assets/src/composables/useFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export default function useFetch(options) {
const count = ref(null);
const loadingMore = ref(false);

// useFetch metadata to manage synchronization of fetches
const _fetchCount = ref(0);

const hasMore = computed(() => moreParams.value != null);

const _setData = (response, loadingMore) => {
Expand All @@ -80,30 +83,54 @@ export default function useFetch(options) {

const fetchData = async (...args) => {
loading.value = true;
loadingMore.value = false; // Reset loading more state
error.value = null;
_fetchCount.value += 1;
const currentFetchCount = _fetchCount.value;

// If the fetch count has changed, it means that a new fetch has been triggered
// and this fetch is no longer relevant
const newFetchHasStarted = () => currentFetchCount !== _fetchCount.value;

try {
const response = await fetchMethod(...args);
if (newFetchHasStarted()) {
return;
}
_setData(response);
} catch (err) {
if (newFetchHasStarted()) {
return;
}
error.value = err;
}

loading.value = false;
};

const fetchMore = async (...args) => {
if (!moreParams.value || !fetchMoreMethod) {
if (!moreParams.value || !fetchMoreMethod || loadingMore.value) {
return;
}

loadingMore.value = true;
error.value = null;
const currentFetchCount = _fetchCount.value;

// If the fetch count or fetch more count has changed, it means that a new fetch has been
// triggered and this fetch is no longer relevant
const newFetchHasStarted = () => currentFetchCount !== _fetchCount.value;

try {
const response = await fetchMoreMethod(moreParams.value, ...args);
_setData(response, loadingMore.value);
if (newFetchHasStarted()) {
return;
}
_setData(response, true);
} catch (err) {
if (newFetchHasStarted()) {
return;
}
error.value = err;
}

Expand Down

0 comments on commit cefb966

Please sign in to comment.