Improve performance by removing searches through the full row array

This commit is contained in:
Andrew Kingston 2023-03-12 16:25:39 +00:00
parent e76c541627
commit 8a8152168d
2 changed files with 14 additions and 12 deletions

View File

@ -32,11 +32,11 @@ export const createRowsStore = context => {
// Generate a lookup map to quick find a row by ID
const rowLookupMap = derived(
enrichedRows,
rows,
$rows => {
let map = {}
for (let row of $rows) {
map[row._id] = row.__idx
for (let i = 0; i < $rows.length; i++) {
map[$rows[i]._id] = i
}
return map
},
@ -259,11 +259,6 @@ export const createRowsStore = context => {
rows.update(state => {
return state.filter(row => !deletedIds.includes(row._id))
})
// If we ended up with no rows, try getting the next page
if (!get(rows).length) {
loadNextPage()
}
}
// Loads the next page of data if available
@ -276,6 +271,11 @@ export const createRowsStore = context => {
return await get(fetch)?.refreshDefinition()
}
// Checks if we have a row with a certain ID
const hasRow = id => {
return get(rowLookupMap)[id] != null
}
return {
rows: {
...rows,
@ -284,6 +284,7 @@ export const createRowsStore = context => {
addRow,
updateRow,
deleteRows,
hasRow,
loadNextPage,
refreshRow,
refreshData,

View File

@ -19,19 +19,20 @@ export const createUIStores = context => {
)
// Ensure we clear invalid rows from state if they disappear
rows.subscribe($rows => {
rows.subscribe(() => {
const $selectedCellId = get(selectedCellId)
const $selectedRows = get(selectedRows)
const $hoveredRowId = get(hoveredRowId)
const hasRow = rows.actions.hasRow
// Check selected cell
const selectedRowId = $selectedCellId?.split("-")[0]
if (selectedRowId && !$rows.find(row => row._id === selectedRowId)) {
if (selectedRowId && !hasRow(selectedRowId)) {
selectedCellId.set(null)
}
// Check hovered row
if ($hoveredRowId && !$rows.find(row => row._id === $hoveredRowId)) {
if ($hoveredRowId && !hasRow($hoveredRowId)) {
hoveredRowId.set(null)
}
@ -40,7 +41,7 @@ export const createUIStores = context => {
let selectedRowsNeedsUpdate = false
const selectedIds = Object.keys($selectedRows)
for (let i = 0; i < selectedIds.length; i++) {
if (!$rows.find(row => row._id === selectedIds[i])) {
if (!hasRow(selectedIds[i])) {
delete newSelectedRows[selectedIds[i]]
selectedRowsNeedsUpdate = true
}