@@ -132,14 +132,36 @@ function setupErrorModal(resp_modal: HTMLElement) {
132132 return { resp_modal, resp_modal_body, Modal } ;
133133}
134134
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 ;
143165 if ( ! update_link ) return ;
144166
145167 const url = new URL ( update_link , window . location . href ) ;
@@ -156,8 +178,18 @@ async function handleUpdate(
156178 errorModal . resp_modal_body . innerHTML = resp_html ;
157179 new errorModal . Modal ( errorModal . resp_modal ) . show ( ) ;
158180 }
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 ) ) ;
159189}
160190
191+ const handleUpdate = debounce ( processGroupedUpdates , 50 ) ;
192+
161193const CSS_VARS = getComputedStyle ( document . documentElement ) ;
162194
163195function cellFromProps ( props : CellProps [ ] ) {
@@ -259,7 +291,15 @@ function handleSetRangeValues(
259291 const rowIdx = Number . parseInt ( row ) ;
260292 const colIdx = Number . parseInt ( col ) ;
261293 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+ } ) ;
263303 }
264304 }
265305}
0 commit comments