hypercore 10.0.0-alpha.44 → 10.0.0-alpha.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +16 -4
- package/lib/core.js +6 -2
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -297,7 +297,8 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
297
297
|
crypto: this.crypto,
|
|
298
298
|
legacy: opts.legacy,
|
|
299
299
|
auth: opts.auth,
|
|
300
|
-
onupdate: this._oncoreupdate.bind(this)
|
|
300
|
+
onupdate: this._oncoreupdate.bind(this),
|
|
301
|
+
oncontigupdate: this._oncorecontigupdate.bind(this)
|
|
301
302
|
})
|
|
302
303
|
|
|
303
304
|
if (opts.userData) {
|
|
@@ -484,9 +485,10 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
484
485
|
if (s._snapshot && bitfield.start < s._snapshot.compatLength) s._snapshot.compatLength = bitfield.start
|
|
485
486
|
}
|
|
486
487
|
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
488
|
+
// For sparse sessions, immediately emit appends. Non-sparse sessions
|
|
489
|
+
// are handled separately and only emit appends when their contiguous
|
|
490
|
+
// length is updated.
|
|
491
|
+
if (appended && s.sparse) s.emit('append')
|
|
490
492
|
}
|
|
491
493
|
|
|
492
494
|
this.replicator.onupgrade()
|
|
@@ -505,6 +507,16 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
505
507
|
}
|
|
506
508
|
}
|
|
507
509
|
|
|
510
|
+
_oncorecontigupdate () {
|
|
511
|
+
// For non-sparse sessions, emit appends only when the contiguous length is
|
|
512
|
+
// updated.
|
|
513
|
+
for (let i = 0; i < this.sessions.length; i++) {
|
|
514
|
+
const s = this.sessions[i]
|
|
515
|
+
|
|
516
|
+
if (!s.sparse) s.emit('append')
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
508
520
|
_onpeerupdate (added, peer) {
|
|
509
521
|
const name = added ? 'peer-add' : 'peer-remove'
|
|
510
522
|
|
package/lib/core.js
CHANGED
|
@@ -9,8 +9,9 @@ const { BAD_ARGUMENT, STORAGE_EMPTY, STORAGE_CONFLICT, INVALID_SIGNATURE } = req
|
|
|
9
9
|
const m = require('./messages')
|
|
10
10
|
|
|
11
11
|
module.exports = class Core {
|
|
12
|
-
constructor (header, crypto, oplog, tree, blocks, bitfield, auth, legacy, onupdate) {
|
|
12
|
+
constructor (header, crypto, oplog, tree, blocks, bitfield, auth, legacy, onupdate, oncontigupdate) {
|
|
13
13
|
this.onupdate = onupdate
|
|
14
|
+
this.oncontigupdate = oncontigupdate
|
|
14
15
|
this.header = header
|
|
15
16
|
this.crypto = crypto
|
|
16
17
|
this.oplog = oplog
|
|
@@ -164,7 +165,7 @@ module.exports = class Core {
|
|
|
164
165
|
}
|
|
165
166
|
}
|
|
166
167
|
|
|
167
|
-
return new this(header, crypto, oplog, tree, blocks, bitfield, auth, !!opts.legacy, opts.onupdate || noop)
|
|
168
|
+
return new this(header, crypto, oplog, tree, blocks, bitfield, auth, !!opts.legacy, opts.onupdate || noop, opts.oncontigupdate || noop)
|
|
168
169
|
}
|
|
169
170
|
|
|
170
171
|
_shouldFlush () {
|
|
@@ -202,6 +203,7 @@ module.exports = class Core {
|
|
|
202
203
|
while (this.bitfield.get(i)) i++
|
|
203
204
|
|
|
204
205
|
this.header.contiguousLength = i
|
|
206
|
+
this.oncontigupdate()
|
|
205
207
|
}
|
|
206
208
|
}
|
|
207
209
|
|
|
@@ -285,6 +287,7 @@ module.exports = class Core {
|
|
|
285
287
|
batch.commit()
|
|
286
288
|
|
|
287
289
|
this.header.contiguousLength = batch.length
|
|
290
|
+
this.oncontigupdate()
|
|
288
291
|
this.header.tree.length = batch.length
|
|
289
292
|
this.header.tree.rootHash = hash
|
|
290
293
|
this.header.tree.signature = batch.signature
|
|
@@ -472,6 +475,7 @@ module.exports = class Core {
|
|
|
472
475
|
const appended = batch.length > batch.ancestors
|
|
473
476
|
|
|
474
477
|
this.header.contiguousLength = Math.min(batch.ancestors, this.header.contiguousLength)
|
|
478
|
+
this.oncontigupdate()
|
|
475
479
|
this.header.tree.fork = batch.fork
|
|
476
480
|
this.header.tree.length = batch.length
|
|
477
481
|
this.header.tree.rootHash = batch.hash()
|