@@ -18,13 +18,15 @@ const kClosed = Symbol('closed')
18
18
const kCloseCallbacks = Symbol ( 'closeCallbacks' )
19
19
const kKeyEncoding = Symbol ( 'keyEncoding' )
20
20
const kValueEncoding = Symbol ( 'valueEncoding' )
21
+ const kAbortOnClose = Symbol ( 'abortOnClose' )
21
22
const kLegacy = Symbol ( 'legacy' )
22
23
const kKeys = Symbol ( 'keys' )
23
24
const kValues = Symbol ( 'values' )
24
25
const kLimit = Symbol ( 'limit' )
25
26
const kCount = Symbol ( 'count' )
26
27
27
28
const emptyOptions = Object . freeze ( { } )
29
+ const noop = ( ) => { }
28
30
let warnedEnd = false
29
31
30
32
// This class is an internal utility for common functionality between AbstractIterator,
@@ -55,6 +57,12 @@ class CommonIterator {
55
57
this [ kLimit ] = Number . isInteger ( options . limit ) && options . limit >= 0 ? options . limit : Infinity
56
58
this [ kCount ] = 0
57
59
60
+ // Undocumented option to abort pending work on close(). Used by the
61
+ // many-level module as a temporary solution to a blocked close().
62
+ // TODO (next major): consider making this the default behavior. Native
63
+ // implementations should have their own logic to safely close iterators.
64
+ this [ kAbortOnClose ] = ! ! options . abortOnClose
65
+
58
66
this . db = db
59
67
this . db . attachResource ( this )
60
68
this . nextTick = db . nextTick
@@ -219,6 +227,9 @@ class CommonIterator {
219
227
[ kFinishWork ] ( ) {
220
228
const cb = this [ kCallback ]
221
229
230
+ // Callback will be null if work was aborted on close
231
+ if ( this [ kAbortOnClose ] && cb === null ) return noop
232
+
222
233
this [ kWorking ] = false
223
234
this [ kCallback ] = null
224
235
@@ -277,6 +288,13 @@ class CommonIterator {
277
288
278
289
if ( ! this [ kWorking ] ) {
279
290
this . _close ( this [ kHandleClose ] )
291
+ } else if ( this [ kAbortOnClose ] ) {
292
+ // Don't wait for work to finish. Subsequently ignore the result.
293
+ const cb = this [ kFinishWork ] ( )
294
+
295
+ cb ( new ModuleError ( 'Aborted on iterator close()' , {
296
+ code : 'LEVEL_ITERATOR_NOT_OPEN'
297
+ } ) )
280
298
}
281
299
}
282
300
0 commit comments