Skip to content

Commit 397bf20

Browse files
authored
Make redux utils generic (#4511)
1 parent b9f63bd commit 397bf20

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

webview/src/experiments/state/rowSelectionSlice.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
22
import { ExecutorStatus } from 'dvc/src/cli/dvc/contract'
33
import { getCompositeId } from '../util/rows'
4-
import { keepEqualOldReferencesInArray } from '../../util/array'
4+
import { keepReferenceIfEqual } from '../../util/objects'
55

66
export type SelectedRow = {
77
depth: number
@@ -80,10 +80,7 @@ export const rowSelectionSlice = createSlice({
8080
},
8181
updateRowOrder(state, action: PayloadAction<SelectedRow[]>) {
8282
const newOrder = action.payload
83-
state.rowOrder = keepEqualOldReferencesInArray(
84-
state.rowOrder,
85-
newOrder
86-
) as SelectedRow[]
83+
state.rowOrder = keepReferenceIfEqual(state.rowOrder, newOrder)
8784
}
8885
}
8986
})

webview/src/experiments/state/tableDataSlice.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const tableDataSlice = createSlice({
126126
state.columnWidths = keepReferenceIfEqual(
127127
state.columnWidths,
128128
action.payload
129-
) as Record<string, number>
129+
)
130130
},
131131
updateColumns: (state, action: PayloadAction<ExtensionColumn[]>) => {
132132
if (isEqual(state.columns, action.payload)) {
@@ -161,7 +161,7 @@ export const tableDataSlice = createSlice({
161161
state.hasMoreCommits = keepReferenceIfEqual(
162162
state.hasMoreCommits,
163163
action.payload
164-
) as Record<string, boolean>
164+
)
165165
},
166166
updateHasRunningWorkspaceExperiment: (
167167
state,
@@ -176,13 +176,10 @@ export const tableDataSlice = createSlice({
176176
state.isShowingMoreCommits = keepReferenceIfEqual(
177177
state.isShowingMoreCommits,
178178
action.payload
179-
) as Record<string, boolean>
179+
)
180180
},
181181
updateRows: (state, action: PayloadAction<Experiment[]>) => {
182-
state.rows = keepEqualOldReferencesInArray(
183-
state.rows,
184-
action.payload
185-
) as Experiment[]
182+
state.rows = keepEqualOldReferencesInArray(state.rows, action.payload)
186183
},
187184
updateSelectedBranches: (state, action: PayloadAction<string[]>) => {
188185
state.selectedBranches = action.payload
@@ -194,10 +191,7 @@ export const tableDataSlice = createSlice({
194191
state.showOnlyChanged = action.payload
195192
},
196193
updateSorts: (state, action: PayloadAction<SortDefinition[]>) => {
197-
state.sorts = keepEqualOldReferencesInArray(
198-
state.sorts,
199-
action.payload
200-
) as SortDefinition[]
194+
state.sorts = keepEqualOldReferencesInArray(state.sorts, action.payload)
201195
}
202196
}
203197
})

webview/src/util/array.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { DraggedInfo } from '../shared/components/dragDrop/dragDropSlice'
55
export const pushIf = <T>(array: T[], condition: boolean, elements: T[]) =>
66
condition && array.push(...elements)
77

8-
export const keepEqualOldReferencesInArray = (
9-
oldArray: BaseType[],
10-
newArray: BaseType[]
11-
) =>
8+
export const keepEqualOldReferencesInArray = <T extends BaseType>(
9+
oldArray: T[],
10+
newArray: T[]
11+
): T[] =>
1212
newArray.map(item => oldArray.find(oldItem => isEqual(oldItem, item)) || item)
1313

1414
export const changeOrderWithDraggedInfo = (

webview/src/util/objects.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ export type Any = BaseType | BaseType[]
1313

1414
type Obj = { [key: string]: Any }
1515

16-
export const keepReferenceIfEqual = (old: BaseType, recent: BaseType) =>
17-
isEqual(old, recent) ? old : recent
16+
export const keepReferenceIfEqual = <T extends BaseType>(
17+
old: T,
18+
recent: T
19+
): T => (isEqual(old, recent) ? old : recent)

0 commit comments

Comments
 (0)