Handle crash when other user deletes a row that is either the source or target of cell selection

This commit is contained in:
Andrew Kingston 2024-06-24 15:25:00 +01:00
parent 5c6cb0a73a
commit e0c38d7fbe
No known key found for this signature in database
2 changed files with 22 additions and 27 deletions

View File

@ -638,14 +638,6 @@ export const createActions = context => {
get(fetch)?.nextPage()
}
// Checks if we have a row with a certain ID
const hasRow = id => {
if (id === NewRowID) {
return true
}
return get(rowLookupMap)[id] != null
}
// Cleans a row by removing any internal grid metadata from it.
// Call this before passing a row to any sort of external flow.
const cleanRow = row => {
@ -667,7 +659,6 @@ export const createActions = context => {
updateValue,
applyRowChanges,
deleteRows,
hasRow,
loadNextPage,
refreshRow,
replaceRow,

View File

@ -117,8 +117,11 @@ export const deriveStores = context => {
}
// Row indices
const sourceRowIndex = $rowLookupMap[sourceInfo.rowId].__idx
const targetRowIndex = $rowLookupMap[targetInfo.rowId].__idx
const sourceRowIndex = $rowLookupMap[sourceInfo.rowId]?.__idx
const targetRowIndex = $rowLookupMap[targetInfo.rowId]?.__idx
if (sourceRowIndex == null || targetRowIndex == null) {
return []
}
const lowerRowIndex = Math.min(sourceRowIndex, targetRowIndex)
const upperRowIndex = Math.max(sourceRowIndex, targetRowIndex)
@ -305,7 +308,7 @@ export const initialise = context => {
focusedRowId,
previousFocusedRowId,
previousFocusedCellId,
rows,
rowLookupMap,
focusedCellId,
selectedRows,
hoveredRowId,
@ -320,18 +323,17 @@ export const initialise = context => {
} = context
// Ensure we clear invalid rows from state if they disappear
rows.subscribe(async () => {
rowLookupMap.subscribe(async $rowLookupMap => {
// We tick here to ensure other derived stores have properly updated.
// We depend on the row lookup map which is a derived store,
await tick()
const $focusedCellId = get(focusedCellId)
const $focusedRowId = get(focusedRowId)
const $selectedRows = get(selectedRows)
const $hoveredRowId = get(hoveredRowId)
const hasRow = rows.actions.hasRow
const hasRow = id => $rowLookupMap[id] != null
// Check selected cell
const selectedRowId = parseCellID($focusedCellId).rowId
if (selectedRowId && !hasRow(selectedRowId)) {
// Check focused cell
if ($focusedRowId && !hasRow($focusedRowId)) {
focusedCellId.set(null)
}
@ -341,9 +343,10 @@ export const initialise = context => {
}
// Check selected rows
const selectedIds = Object.keys($selectedRows)
if (selectedIds.length) {
let newSelectedRows = { ...$selectedRows }
let selectedRowsNeedsUpdate = false
const selectedIds = Object.keys($selectedRows)
for (let i = 0; i < selectedIds.length; i++) {
if (!hasRow(selectedIds[i])) {
delete newSelectedRows[selectedIds[i]]
@ -353,6 +356,7 @@ export const initialise = context => {
if (selectedRowsNeedsUpdate) {
selectedRows.set(newSelectedRows)
}
}
})
// Remember the last focused row ID so that we can store the previous one