This repository was archived by the owner on Dec 28, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ module.exports = class Hypercore extends EventEmitter {
69
69
this . auth = opts . auth || null
70
70
this . autoClose = ! ! opts . autoClose
71
71
this . onwait = opts . onwait || null
72
+ this . wait = opts . wait !== false
72
73
73
74
this . closing = null
74
75
this . opening = this . _openSession ( key , storage , opts )
@@ -194,11 +195,13 @@ module.exports = class Hypercore extends EventEmitter {
194
195
}
195
196
196
197
const sparse = opts . sparse === false ? false : this . sparse
198
+ const wait = opts . wait === false ? false : this . wait
197
199
const onwait = opts . onwait === undefined ? this . onwait : opts . onwait
198
200
const Clz = opts . class || Hypercore
199
201
const s = new Clz ( this . storage , this . key , {
200
202
...opts ,
201
203
sparse,
204
+ wait,
202
205
onwait,
203
206
_opening : this . opening ,
204
207
_sessions : this . sessions
@@ -682,6 +685,7 @@ module.exports = class Hypercore extends EventEmitter {
682
685
if ( this . cache ) this . cache . set ( index , block )
683
686
} else {
684
687
if ( opts && opts . wait === false ) return null
688
+ if ( this . wait === false && ( ! opts || ! opts . wait ) ) return null
685
689
if ( opts && opts . onwait ) opts . onwait ( index , this )
686
690
if ( this . onwait ) this . onwait ( index , this )
687
691
Original file line number Diff line number Diff line change @@ -201,3 +201,34 @@ test('read ahead', async function (t) {
201
201
202
202
t . alike ( await blk , 'b' )
203
203
} )
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
+ } )
You can’t perform that action at this time.
0 commit comments