Prevent extra fetches when not required

This commit is contained in:
Adria Navarro 2023-09-21 13:15:42 +02:00
parent 1191e71a15
commit 2ef014b070
1 changed files with 18 additions and 7 deletions

View File

@ -45,15 +45,26 @@
$: expandedDefaultValue = expand(defaultValue)
$: primaryDisplay = tableDefinition?.primaryDisplay || "_id"
let prevTerm
let lastFetchedTerm
$: {
// Check if the fetch changed, ignoring differences between empty/null/undefined
if (primaryDisplay && prevTerm != fetchTerm) {
prevTerm = fetchTerm
const termChangedSinceFetch = (lastFetchedTerm || "") !== (fetchTerm || "")
const allRowsFetched =
!lastFetchedTerm && $fetch.rows.length === $fetch.totalRows
if (!allRowsFetched && termChangedSinceFetch) {
debugger
// Don't request until we have the primary display
if (primaryDisplay) {
lastFetchedTerm = fetchTerm
fetch.update({
query: { string: { [primaryDisplay]: fetchTerm } },
})
}
} else {
options = fetchTerm
? $fetch.rows.filter(row => row[primaryDisplay].includes(fetchTerm))
: $fetch.rows
}
}
const flatten = values => {