Skip to content

Commit

Permalink
fix: hierarchical table updates (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson authored Oct 7, 2023
1 parent f6bb90a commit 09d4854
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions v3/src/components/case-table/use-rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,32 @@ export const useRows = () => {
}, { attachAfter: false })
const afterAnyActionDisposer = data && onAnyAction(data, action => {
prf.measure("Table.useRows[onAnyAction]", () => {
const isHierarchical = !!data?.collections.length
const alwaysResetRowCacheActions = ["addAttribute", "removeAttribute", "setFormat"]
const hierarchicalResetRowCacheActions = ["addCases", "setCaseValues", "removeCases"]
let updateRows = false

const getCasesToUpdate = (_cases: ICase[], index?: number) => {
lowestIndex.current = index != null ? index : data.cases.length
const casesToUpdate = []
for (let i=0; i<_cases.length; ++i) {
lowestIndex.current = Math.min(lowestIndex.current, data.caseIndexFromID(_cases[i].__id__))
}
for (let j=lowestIndex.current; j < data.cases.length; ++j) {
casesToUpdate.push(data.cases[j])
}
return casesToUpdate
// some actions (more with hierarchical data sets) require rebuilding the entire row cache
if (alwaysResetRowCacheActions.includes(action.name) ||
(isHierarchical && hierarchicalResetRowCacheActions.includes(action.name))) {
resetRowCache()
updateRows = true
}

if (!data?.collections.length) {
updateRows = true
// non-hierarchical data sets can respond more efficiently to some actions
if (!isHierarchical && hierarchicalResetRowCacheActions.includes(action.name)) {
const getCasesToUpdate = (_cases: ICase[], index?: number) => {
lowestIndex.current = index != null ? index : data.cases.length
const casesToUpdate = []
for (let i=0; i<_cases.length; ++i) {
lowestIndex.current = Math.min(lowestIndex.current, data.caseIndexFromID(_cases[i].__id__))
}
for (let j=lowestIndex.current; j < data.cases.length; ++j) {
casesToUpdate.push(data.cases[j])
}
return casesToUpdate
}

if (isAddCasesAction(action)) {
const [_cases] = action.args
// update cache only for entries after the added cases
Expand All @@ -166,9 +176,7 @@ export const useRows = () => {
const casesToUpdate = getCasesToUpdate([], lowestIndex.current)
casesToUpdate.forEach(({ __id__ }) => rowCache.set(__id__, { __id__ }))
}
else {
updateRows = false
}
updateRows = true
}

if (updateRows) {
Expand Down

0 comments on commit 09d4854

Please sign in to comment.