@@ -316,16 +316,18 @@ async function innerInsertMultipleAsync<T extends AnyOrama>(
316316
317317 const processAllBatches = async ( ) : Promise < void > => {
318318 let currentIndex = 0
319+ const sab = new SharedArrayBuffer ( 4 )
320+ const ia = new Int32Array ( sab )
319321
320322 while ( currentIndex < docs . length ) {
321323 const startTime = Date . now ( )
322-
323324 currentIndex = await processNextBatch ( currentIndex )
324325
325326 if ( timeout > 0 ) {
326327 const elapsedTime = Date . now ( ) - startTime
327- if ( elapsedTime < timeout ) {
328- await new Promise ( ( resolve ) => setTimeout ( resolve , timeout - elapsedTime ) )
328+ const waitTime = timeout - elapsedTime
329+ if ( waitTime > 0 ) {
330+ Atomics . wait ( ia , 0 , 0 , waitTime )
329331 }
330332 }
331333 }
@@ -367,6 +369,8 @@ function innerInsertMultipleSync<T extends AnyOrama>(
367369
368370 function processAllBatches ( ) {
369371 const startTime = Date . now ( )
372+ const sab = new SharedArrayBuffer ( 4 )
373+ const ia = new Int32Array ( sab )
370374
371375 // eslint-disable-next-line no-constant-condition
372376 while ( true ) {
@@ -376,11 +380,9 @@ function innerInsertMultipleSync<T extends AnyOrama>(
376380 if ( timeout > 0 ) {
377381 const elapsedTime = Date . now ( ) - startTime
378382 if ( elapsedTime >= timeout ) {
379- // Synchronously wait for the remaining time
380383 const remainingTime = timeout - ( elapsedTime % timeout )
381- const endTime = Date . now ( ) + remainingTime
382- while ( Date . now ( ) < endTime ) {
383- // Do nothing, just wait
384+ if ( remainingTime > 0 ) {
385+ Atomics . wait ( ia , 0 , 0 , remainingTime )
384386 }
385387 }
386388 }
0 commit comments