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

View File

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