This repository was archived by the owner on Dec 28, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -626,11 +626,28 @@ module.exports = class Hypercore extends EventEmitter {
626626 if ( ! upgraded ) upgraded = this . contiguousLength !== contig
627627 }
628628
629+ if ( opts && typeof opts . minLength === 'number' ) {
630+ await this . waitForLength ( opts . minLength )
631+ }
632+
629633 if ( ! upgraded ) return false
630634 if ( this . snapshotted ) return this . _updateSnapshot ( )
631635 return true
632636 }
633637
638+ waitForLength ( minLength = 0 ) {
639+ return new Promise ( resolve => {
640+ if ( this . length >= minLength ) return resolve ( this . length )
641+ const onAppend = ( ) => {
642+ if ( this . length >= minLength ) {
643+ this . removeListener ( 'append' , onAppend )
644+ resolve ( this . length )
645+ }
646+ }
647+ this . on ( 'append' , onAppend )
648+ } )
649+ }
650+
634651 async seek ( bytes , opts ) {
635652 if ( this . opened === false ) await this . opening
636653
Original file line number Diff line number Diff line change @@ -840,3 +840,55 @@ test('sparse replication without gossiping', async function (t) {
840840 t . alike ( await c . seek ( 4 ) , [ 4 , 0 ] )
841841 } )
842842} )
843+
844+ test ( 'sparse update with minLength' , async function ( t ) {
845+ const a = await create ( )
846+ const b = await create ( a . key )
847+ replicate ( a , b , t )
848+
849+ await a . append ( [ '1' , '2' ] )
850+ await b . update ( )
851+ t . is ( b . length , 2 )
852+
853+ const updateLength5 = t . test ( 'updateLength5' )
854+ updateLength5 . plan ( 1 )
855+
856+ b . update ( { minLength : 5 } ) . then ( ( ) => {
857+ updateLength5 . pass ( )
858+ } )
859+
860+ await a . append ( [ '3' ] )
861+ await eventFlush ( )
862+ await a . append ( [ '4' ] )
863+ await eventFlush ( )
864+ await a . append ( [ '5' ] )
865+
866+ await updateLength5
867+ t . is ( b . length , 5 )
868+ } )
869+
870+ test ( 'non-sparse update with minLength' , async function ( t ) {
871+ const a = await create ( )
872+ const b = await create ( a . key , { sparse : false } )
873+ replicate ( a , b , t )
874+
875+ await a . append ( [ '1' , '2' ] )
876+ await b . update ( )
877+ t . is ( b . length , 2 )
878+
879+ const updateLength5 = t . test ( 'updateLength5' )
880+ updateLength5 . plan ( 1 )
881+
882+ b . update ( { minLength : 5 } ) . then ( ( ) => {
883+ updateLength5 . pass ( )
884+ } )
885+
886+ await a . append ( [ '3' ] )
887+ await eventFlush ( )
888+ await a . append ( [ '4' ] )
889+ await eventFlush ( )
890+ await a . append ( [ '5' ] )
891+
892+ await updateLength5
893+ t . is ( b . length , 5 )
894+ } )
You can’t perform that action at this time.
0 commit comments