@@ -5,6 +5,8 @@ import type { EmptyObject } from "type-fest";
5
5
import type { DataGrid } from "../data-grid" ;
6
6
import type { GridItem , GridRowId , GridRows } from "../types" ;
7
7
8
+ import { cached } from "@/utils/weakCache" ;
9
+
8
10
/**
9
11
* Enables removing and adding rows from a grid.
10
12
*
@@ -23,8 +25,6 @@ export class DataGridRowsController implements ReactiveController {
23
25
addRows ?: DataGrid [ "addRows" ] ;
24
26
} ;
25
27
26
- readonly #rowIds = new Map < GridItem , GridRowId > ( ) ;
27
-
28
28
#prevItems?: GridItem [ ] ;
29
29
30
30
public rows : GridRows = new Map < GridRowId , GridItem > ( ) ;
@@ -55,25 +55,12 @@ export class DataGridRowsController implements ReactiveController {
55
55
item [ rowKey as unknown as string ] as GridRowId ,
56
56
item ,
57
57
] )
58
- : items . map ( ( item ) => [ this . getCachedId ( item ) , item ] ) ,
58
+ : items . map (
59
+ cached ( ( item ) => [ nanoid ( ) , item ] , { cacheConstructor : Map } ) ,
60
+ ) ,
59
61
) ;
60
62
}
61
63
62
- /**
63
- * Use item reference to get a persistent generated ID.
64
- */
65
- private getCachedId ( item : GridItem ) : GridRowId {
66
- const id = this . #rowIds. get ( item ) ;
67
-
68
- if ( id ) return id ;
69
-
70
- const newId = nanoid ( ) ;
71
-
72
- this . #rowIds. set ( item , newId ) ;
73
-
74
- return newId ;
75
- }
76
-
77
64
public setItems ( items : GridItem [ ] ) {
78
65
if ( ! this . #prevItems || items !== this . #prevItems) {
79
66
this . setRowsFromItems ( items ) ;
0 commit comments