Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit d3625c2

Browse files
committed
added core.update({ minLength: n }) and core.waitForLength(n)
1 parent 4227baf commit d3625c2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,28 @@ module.exports = class Hypercore extends EventEmitter {
623623
if (!upgraded) upgraded = this.contiguousLength !== contig
624624
}
625625

626+
if (opts && typeof opts.minLength === 'number') {
627+
await this.waitForLength(opts.minLength)
628+
}
629+
626630
if (!upgraded) return false
627631
if (this.snapshotted) return this._updateSnapshot()
628632
return true
629633
}
630634

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+
631648
async seek (bytes, opts) {
632649
if (this.opened === false) await this.opening
633650

test/replicate.js

+52
Original file line numberDiff line numberDiff 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+
})

0 commit comments

Comments
 (0)