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

added core.update({ minLength: n }) and core.waitForLength(n) #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,28 @@ module.exports = class Hypercore extends EventEmitter {
if (!upgraded) upgraded = this.contiguousLength !== contig
}

if (opts && typeof opts.minLength === 'number') {
await this.waitForLength(opts.minLength)
}

if (!upgraded) return false
if (this.snapshotted) return this._updateSnapshot()
return true
}

waitForLength (minLength = 0) {
return new Promise(resolve => {
if (this.length >= minLength) return resolve(this.length)
const onAppend = () => {
if (this.length >= minLength) {
this.removeListener('append', onAppend)
resolve(this.length)
}
}
this.on('append', onAppend)
})
}

async seek (bytes, opts) {
if (this.opened === false) await this.opening

Expand Down
52 changes: 52 additions & 0 deletions test/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,55 @@ test('sparse replication without gossiping', async function (t) {
t.alike(await c.seek(4), [4, 0])
})
})

test('sparse update with minLength', async function (t) {
const a = await create()
const b = await create(a.key)
replicate(a, b, t)

await a.append(['1', '2'])
await b.update()
t.is(b.length, 2)

const updateLength5 = t.test('updateLength5')
updateLength5.plan(1)

b.update({ minLength: 5 }).then(() => {
updateLength5.pass()
})

await a.append(['3'])
await eventFlush()
await a.append(['4'])
await eventFlush()
await a.append(['5'])

await updateLength5
t.is(b.length, 5)
})

test('non-sparse update with minLength', async function (t) {
const a = await create()
const b = await create(a.key, { sparse: false })
replicate(a, b, t)

await a.append(['1', '2'])
await b.update()
t.is(b.length, 2)

const updateLength5 = t.test('updateLength5')
updateLength5.plan(1)

b.update({ minLength: 5 }).then(() => {
updateLength5.pass()
})

await a.append(['3'])
await eventFlush()
await a.append(['4'])
await eventFlush()
await a.append(['5'])

await updateLength5
t.is(b.length, 5)
})