@@ -132,14 +132,36 @@ function setupErrorModal(resp_modal: HTMLElement) {
132
132
return { resp_modal, resp_modal_body, Modal } ;
133
133
}
134
134
135
- async function handleUpdate (
136
- update_link : string ,
137
- x : number ,
138
- y : number ,
139
- value : CellValue | null | undefined ,
140
- customId : string | undefined ,
141
- errorModal : ReturnType < typeof setupErrorModal > ,
142
- ) {
135
+ interface UpdateParams {
136
+ update_link : string ;
137
+ x : number ;
138
+ y : number ;
139
+ value : CellValue | null | undefined ;
140
+ customId : string | undefined ;
141
+ errorModal : ReturnType < typeof setupErrorModal > ;
142
+ }
143
+
144
+ function debounce < T > ( fn : ( items : T [ ] ) => Promise < void > , timeoutMs : number ) {
145
+ let timeout : NodeJS . Timeout | null = null ;
146
+ let items : T [ ] = [ ] ;
147
+
148
+ return ( item : T ) => {
149
+ if ( timeout ) {
150
+ clearTimeout ( timeout ) ;
151
+ }
152
+
153
+ items . push ( item ) ;
154
+
155
+ timeout = setTimeout ( async ( ) => {
156
+ const currentItems = items ;
157
+ items = [ ] ;
158
+ await fn ( currentItems ) ;
159
+ } , timeoutMs ) ;
160
+ } ;
161
+ }
162
+
163
+ const performUpdate = async ( params : UpdateParams ) => {
164
+ const { update_link, x, y, value, customId, errorModal } = params ;
143
165
if ( ! update_link ) return ;
144
166
145
167
const url = new URL ( update_link , window . location . href ) ;
@@ -156,8 +178,18 @@ async function handleUpdate(
156
178
errorModal . resp_modal_body . innerHTML = resp_html ;
157
179
new errorModal . Modal ( errorModal . resp_modal ) . show ( ) ;
158
180
}
181
+ } ;
182
+
183
+ async function processGroupedUpdates ( updates : UpdateParams [ ] ) {
184
+ const grouped = new Map (
185
+ updates . map ( ( update ) => [ `${ update . x } -${ update . y } ` , update ] ) ,
186
+ ) ;
187
+ const uniques = Array . from ( grouped . values ( ) ) ;
188
+ await Promise . all ( uniques . map ( performUpdate ) ) ;
159
189
}
160
190
191
+ const handleUpdate = debounce ( processGroupedUpdates , 50 ) ;
192
+
161
193
const CSS_VARS = getComputedStyle ( document . documentElement ) ;
162
194
163
195
function cellFromProps ( props : CellProps [ ] ) {
@@ -259,7 +291,15 @@ function handleSetRangeValues(
259
291
const rowIdx = Number . parseInt ( row ) ;
260
292
const colIdx = Number . parseInt ( col ) ;
261
293
const customId = cellIdMap . get ( rowIdx , colIdx ) ;
262
- handleUpdate ( update_link , colIdx , rowIdx , value , customId , errorModal ) ;
294
+
295
+ handleUpdate ( {
296
+ update_link,
297
+ x : colIdx ,
298
+ y : rowIdx ,
299
+ value,
300
+ customId,
301
+ errorModal,
302
+ } ) ;
263
303
}
264
304
}
265
305
}
0 commit comments