From 740449394c4fe7c00756c635eb6b8b1ebd79acb5 Mon Sep 17 00:00:00 2001 From: Laila Los <44241786+ElectronicBlueberry@users.noreply.github.com> Date: Fri, 15 Mar 2024 16:49:05 +0100 Subject: [PATCH] add edge case for element_count bug --- client/src/stores/collectionElementsStore.ts | 29 ++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/client/src/stores/collectionElementsStore.ts b/client/src/stores/collectionElementsStore.ts index d265becded8b..ac04e1839611 100644 --- a/client/src/stores/collectionElementsStore.ts +++ b/client/src/stores/collectionElementsStore.ts @@ -74,19 +74,25 @@ export const useCollectionElementsStore = defineStore("collectionElementsStore", const collectionKey = getCollectionKey(collection); try { - // We should fetch only missing (placeholder) elements from the range - const firstMissingIndexInRange = storedElements - .slice(offset, offset + limit) - .findIndex((element) => (isPlaceholder(element) && !element.fetching) || isInvalid(element)); + if (storedElements.length !== 0) { + // We should fetch only missing (placeholder) elements from the range + const firstMissingIndexInRange = storedElements + .slice(offset, offset + limit) + .findIndex((element) => (isPlaceholder(element) && !element.fetching) || isInvalid(element)); + + if (firstMissingIndexInRange === -1) { + // All elements in the range are already stored or being fetched + return; + } - if (firstMissingIndexInRange === -1) { - // All elements in the range are already stored or being fetched - return; + // Adjust the offset to the first missing element + offset += firstMissingIndexInRange; + } else { + // Edge case where element_count is incorrect + // TODO: remove me once element_count is reported reliably + offset = 0; } - // Adjust the offset to the first missing element - offset += firstMissingIndexInRange; - set(loadingCollectionElements.value, collectionKey, true); // Mark all elements in the range as fetching storedElements @@ -124,7 +130,8 @@ export const useCollectionElementsStore = defineStore("collectionElementsStore", const to = from + data.fetchedElements.length; for (let index = from; index < to; index++) { - set(storedElements, index, ensureDefined(data.fetchedElements[index - from])); + const element = ensureDefined(data.fetchedElements[index - from]); + set(storedElements, index, element); } set(storedCollectionElements.value, key, storedElements);