This repository was archived by the owner on Dec 28, 2022. It is now read-only.
File tree 2 files changed +69
-0
lines changed
2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -623,11 +623,28 @@ module.exports = class Hypercore extends EventEmitter {
623
623
if ( ! upgraded ) upgraded = this . contiguousLength !== contig
624
624
}
625
625
626
+ if ( opts && typeof opts . minLength === 'number' ) {
627
+ await this . waitForLength ( opts . minLength )
628
+ }
629
+
626
630
if ( ! upgraded ) return false
627
631
if ( this . snapshotted ) return this . _updateSnapshot ( )
628
632
return true
629
633
}
630
634
635
+ waitForLength ( minLength = 0 ) {
636
+ return new Promise ( resolve => {
637
+ if ( this . length >= minLength ) return resolve ( this . length )
638
+ const onAppend = ( ) => {
639
+ if ( this . length >= minLength ) {
640
+ this . removeListener ( 'append' , onAppend )
641
+ resolve ( this . length )
642
+ }
643
+ }
644
+ this . on ( 'append' , onAppend )
645
+ } )
646
+ }
647
+
631
648
async seek ( bytes , opts ) {
632
649
if ( this . opened === false ) await this . opening
633
650
Original file line number Diff line number Diff line change @@ -840,3 +840,55 @@ test('sparse replication without gossiping', async function (t) {
840
840
t . alike ( await c . seek ( 4 ) , [ 4 , 0 ] )
841
841
} )
842
842
} )
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