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

Commit 78672a4

Browse files
committed
snapshots and sessions inherit sparse setting
1 parent 66865d1 commit 78672a4

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ module.exports = class Hypercore extends EventEmitter {
113113
indent + ' opened: ' + opts.stylize(this.opened, 'boolean') + '\n' +
114114
indent + ' closed: ' + opts.stylize(this.closed, 'boolean') + '\n' +
115115
indent + ' snapshotted: ' + opts.stylize(this.snapshotted, 'boolean') + '\n' +
116+
indent + ' sparse: ' + opts.stylize(this.sparse, 'boolean') + '\n' +
116117
indent + ' writable: ' + opts.stylize(this.writable, 'boolean') + '\n' +
117118
indent + ' length: ' + opts.stylize(this.length, 'number') + '\n' +
118119
indent + ' byteLength: ' + opts.stylize(this.byteLength, 'number') + '\n' +
@@ -182,9 +183,11 @@ module.exports = class Hypercore extends EventEmitter {
182183
throw SESSION_CLOSED('Cannot make sessions on a closing core')
183184
}
184185

186+
const sparse = opts.sparse === false ? false : this.sparse
185187
const Clz = opts.class || Hypercore
186188
const s = new Clz(this.storage, this.key, {
187189
...opts,
190+
sparse,
188191
_opening: this.opening,
189192
_sessions: this.sessions
190193
})
@@ -324,14 +327,27 @@ module.exports = class Hypercore extends EventEmitter {
324327
}
325328
}
326329

327-
_updateSnapshot () {
328-
const prev = this._snapshot
329-
const next = this._snapshot = {
330-
length: this.core.tree.length,
331-
byteLength: this.core.tree.byteLength,
330+
_getSnapshot () {
331+
if (this.sparse) {
332+
return {
333+
length: this.core.tree.length,
334+
byteLength: this.core.tree.byteLength,
335+
fork: this.core.tree.fork,
336+
compatLength: this.core.tree.length
337+
}
338+
}
339+
340+
return {
341+
length: this.core.header.contiguousLength,
342+
byteLength: 0,
332343
fork: this.core.tree.fork,
333-
compatLength: this.core.tree.length
344+
compatLength: this.core.header.contiguousLength
334345
}
346+
}
347+
348+
_updateSnapshot () {
349+
const prev = this._snapshot
350+
const next = this._snapshot = this._getSnapshot()
335351

336352
if (!prev) return true
337353
return prev.length !== next.length || prev.fork !== next.fork

0 commit comments

Comments
 (0)