import { diffGunCRDT , GunGraphAdapter , GunGraphData , mergeGraph } from '@chaingun/sea-client' ; export function batchWriter ( persist , interval = 500 ) { let batch = { } ; function queueDiff ( changes ) { const diff = diffGunCRDT ( changes , batch ) ; batch = diff ? mergeGraph ( batch , diff , 'mutable' ) : batch ; return diff ; } async function writeBatch ( ) { if ( ! Object . keys ( batch ) . length ) { return null ; } const toWrite = batch ; batch = { } ; await Promise . all ( Object . entries ( toWrite ) . map ( async ( [ soul , node ] ) => { try { await persist . put ( { [ soul ] : node } ) ; } catch ( e ) { console . error ( "Error saving" , soul , e . stack ) ; queueDiff ( { [ soul ] : node } ) ; } } ) ) ; } async function onInterval ( ) { await writeBatch ( ) ; setTimeout ( onInterval , interval ) ; } setTimeout ( onInterval , interval ) ; return { queueDiff , writeBatch } ; }