Skip to content

Commit

Permalink
Keep old reference in the state if they have not changed (#2114)
Browse files Browse the repository at this point in the history
* Keep old references in the tableData

* Make update function more compact
  • Loading branch information
sroy3 authored Jul 29, 2022
1 parent 3ecca80 commit ed4c976
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
28 changes: 26 additions & 2 deletions webview/src/experiments/components/table/tableDataSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { TableData } from 'dvc/src/experiments/webview/contract'
import { FilteredCounts } from 'dvc/src/experiments/model/filterBy/collect'
import { SortDefinition } from 'dvc/src/experiments/model/sortBy'
import { Column, Row, TableData } from 'dvc/src/experiments/webview/contract'
import { keepEqualOldReferencesInArray } from '../../../util/array'
import { keepReferenceIfEqual } from '../../../util/objects'

export interface TableDataState extends TableData {
hasData?: boolean
Expand Down Expand Up @@ -32,7 +36,27 @@ export const tableDataSlice = createSlice({
return {
...state,
...action.payload,
hasData: true
columnWidths: keepReferenceIfEqual(
state.columnWidths,
action.payload.columnWidths
) as Record<string, number>,
columns: keepEqualOldReferencesInArray(
state.columns,
action.payload.columns
) as Column[],
filteredCounts: keepReferenceIfEqual(
state.filteredCounts,
action.payload.filteredCounts
) as FilteredCounts,
hasData: true,
rows: keepEqualOldReferencesInArray(
state.rows,
action.payload.rows
) as Row[],
sorts: keepEqualOldReferencesInArray(
state.sorts,
action.payload.sorts
) as SortDefinition[]
}
}
return tableDataInitialState
Expand Down
9 changes: 9 additions & 0 deletions webview/src/util/array.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import isEqual from 'lodash.isequal'
import { BaseType } from './objects'

export const pushIf = <T>(array: T[], condition: boolean, elements: T[]) =>
condition && array.push(...elements)

export const keepEqualOldReferencesInArray = (
oldArray: BaseType[],
newArray: BaseType[]
) =>
newArray.map(item => oldArray.find(oldItem => isEqual(oldItem, item)) || item)
6 changes: 5 additions & 1 deletion webview/src/util/objects.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import isEqual from 'lodash.isequal'
import { reorderObjectList } from 'dvc/src/util/array'

export const performOrderedUpdate = (
Expand All @@ -22,6 +23,9 @@ export const performSimpleOrderedUpdate = (
return [...newOrder]
}

type BaseType = string | number | boolean | Object | undefined | null
export type BaseType = string | number | boolean | Object | undefined | null

export type Any = BaseType | BaseType[]

export const keepReferenceIfEqual = (old: BaseType, recent: BaseType) =>
isEqual(old, recent) ? old : recent

0 comments on commit ed4c976

Please sign in to comment.