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

Commit c4aa50e

Browse files
authored
allow setting defaults for wait (#126)
1 parent 4227baf commit c4aa50e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module.exports = class Hypercore extends EventEmitter {
6969
this.auth = opts.auth || null
7070
this.autoClose = !!opts.autoClose
7171
this.onwait = opts.onwait || null
72+
this.wait = opts.wait !== false
7273

7374
this.closing = null
7475
this.opening = this._openSession(key, storage, opts)
@@ -194,11 +195,13 @@ module.exports = class Hypercore extends EventEmitter {
194195
}
195196

196197
const sparse = opts.sparse === false ? false : this.sparse
198+
const wait = opts.wait === false ? false : this.wait
197199
const onwait = opts.onwait === undefined ? this.onwait : opts.onwait
198200
const Clz = opts.class || Hypercore
199201
const s = new Clz(this.storage, this.key, {
200202
...opts,
201203
sparse,
204+
wait,
202205
onwait,
203206
_opening: this.opening,
204207
_sessions: this.sessions
@@ -682,6 +685,7 @@ module.exports = class Hypercore extends EventEmitter {
682685
if (this.cache) this.cache.set(index, block)
683686
} else {
684687
if (opts && opts.wait === false) return null
688+
if (this.wait === false && (!opts || !opts.wait)) return null
685689
if (opts && opts.onwait) opts.onwait(index, this)
686690
if (this.onwait) this.onwait(index, this)
687691

test/basic.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,34 @@ test('read ahead', async function (t) {
201201

202202
t.alike(await blk, 'b')
203203
})
204+
205+
test('defaults for wait', async function (t) {
206+
t.plan(5)
207+
208+
const core = new Hypercore(RAM, Buffer.alloc(32), { valueEncoding: 'utf-8' })
209+
210+
const a = core.get(1)
211+
212+
a.catch(function (err) {
213+
t.ok(err, 'a failed')
214+
})
215+
216+
t.is(await core.get(1, { wait: false }), null)
217+
218+
const s = core.session({ wait: false })
219+
220+
const b = s.get(1, { wait: true })
221+
222+
b.catch(function (err) {
223+
t.ok(err, 'b failed')
224+
})
225+
226+
t.is(await s.get(1), null)
227+
228+
const s2 = s.session() // check if wait is inherited
229+
230+
t.is(await s2.get(1), null)
231+
232+
await s.close()
233+
await core.close()
234+
})

0 commit comments

Comments
 (0)