Breaking: upgrade to abstract-level 2 · Level/memory-level@77dac64
@@ -62,19 +62,19 @@ class MemoryIterator extends AbstractIterator {
6262this[kInit](db[kTree], options)
6363}
646465-_next (callback) {
66-if (!this[kIterator].valid) return this.nextTick(callback)
65+async _next () {
66+if (!this[kIterator].valid) return undefined
67676868const key = this[kIterator].key
6969const value = this[kIterator].value
707071-if (!this[kTest](key)) return this.nextTick(callback)
71+if (!this[kTest](key)) return undefined
72727373this[kIterator][this[kAdvance]]()
74-this.nextTick(callback, null, key, value)
74+return [key, value]
7575}
767677-_nextv (size, options, callback) {
77+async _nextv (size, options) {
7878const it = this[kIterator]
7979const entries = []
8080@@ -83,10 +83,10 @@ class MemoryIterator extends AbstractIterator {
8383it[this[kAdvance]]()
8484}
858586-this.nextTick(callback, null, entries)
86+return entries
8787}
888889-_all (options, callback) {
89+async _all (options) {
9090const size = this.limit - this.count
9191const it = this[kIterator]
9292const entries = []
@@ -96,7 +96,7 @@ class MemoryIterator extends AbstractIterator {
9696it[this[kAdvance]]()
9797}
989899-this.nextTick(callback, null, entries)
99+return entries
100100}
101101}
102102@@ -106,17 +106,17 @@ class MemoryKeyIterator extends AbstractKeyIterator {
106106this[kInit](db[kTree], options)
107107}
108108109-_next (callback) {
110-if (!this[kIterator].valid) return this.nextTick(callback)
109+async _next () {
110+if (!this[kIterator].valid) return undefined
111111112112const key = this[kIterator].key
113-if (!this[kTest](key)) return this.nextTick(callback)
113+if (!this[kTest](key)) return undefined
114114115115this[kIterator][this[kAdvance]]()
116-this.nextTick(callback, null, key)
116+return key
117117}
118118119-_nextv (size, options, callback) {
119+async _nextv (size, options) {
120120const it = this[kIterator]
121121const keys = []
122122@@ -125,10 +125,10 @@ class MemoryKeyIterator extends AbstractKeyIterator {
125125it[this[kAdvance]]()
126126}
127127128-this.nextTick(callback, null, keys)
128+return keys
129129}
130130131-_all (options, callback) {
131+async _all (options) {
132132const size = this.limit - this.count
133133const it = this[kIterator]
134134const keys = []
@@ -138,7 +138,7 @@ class MemoryKeyIterator extends AbstractKeyIterator {
138138it[this[kAdvance]]()
139139}
140140141-this.nextTick(callback, null, keys)
141+return keys
142142}
143143}
144144@@ -148,19 +148,19 @@ class MemoryValueIterator extends AbstractValueIterator {
148148this[kInit](db[kTree], options)
149149}
150150151-_next (callback) {
152-if (!this[kIterator].valid) return this.nextTick(callback)
151+async _next (options) {
152+if (!this[kIterator].valid) return undefined
153153154154const key = this[kIterator].key
155155const value = this[kIterator].value
156156157-if (!this[kTest](key)) return this.nextTick(callback)
157+if (!this[kTest](key)) return undefined
158158159159this[kIterator][this[kAdvance]]()
160-this.nextTick(callback, null, value)
160+return value
161161}
162162163-_nextv (size, options, callback) {
163+async _nextv (size, options) {
164164const it = this[kIterator]
165165const values = []
166166@@ -169,10 +169,10 @@ class MemoryValueIterator extends AbstractValueIterator {
169169it[this[kAdvance]]()
170170}
171171172-this.nextTick(callback, null, values)
172+return values
173173}
174174175-_all (options, callback) {
175+async _all (options) {
176176const size = this.limit - this.count
177177const it = this[kIterator]
178178const values = []
@@ -182,7 +182,7 @@ class MemoryValueIterator extends AbstractValueIterator {
182182it[this[kAdvance]]()
183183}
184184185-this.nextTick(callback, null, values)
185+return values
186186}
187187}
188188@@ -270,6 +270,7 @@ class MemoryLevel extends AbstractLevel {
270270}
271271272272// To help migrating from level-mem to abstract-level
273+// TODO (v2): remove
273274if (typeof location === 'function' || typeof options === 'function' || typeof _ === 'function') {
274275throw new ModuleError('The levelup-style callback argument has been removed', {
275276code: 'LEVEL_LEGACY'
@@ -291,45 +292,40 @@ class MemoryLevel extends AbstractLevel {
291292permanence: false,
292293createIfMissing: false,
293294errorIfExists: false,
294-encodings: { [storeEncoding]: true }
295+encodings: { [storeEncoding]: true },
296+signals: {
297+// Would have no value here because the operations are synchronous
298+iterators: false
299+}
295300}, forward)
296301297302this[kTree] = createRBT(compare)
298303}
299304300-_put (key, value, options, callback) {
305+async _put (key, value, options) {
301306const it = this[kTree].find(key)
302307303308if (it.valid) {
304309this[kTree] = it.update(value)
305310} else {
306311this[kTree] = this[kTree].insert(key, value)
307312}
308-309-this.nextTick(callback)
310313}
311314312-_get (key, options, callback) {
313-const value = this[kTree].get(key)
314-315-if (typeof value === 'undefined') {
316-// TODO: use error code (not urgent, abstract-level normalizes this)
317-return this.nextTick(callback, new Error('NotFound'))
318-}
319-320-this.nextTick(callback, null, value)
315+async _get (key, options) {
316+// Is undefined if not found
317+return this[kTree].get(key)
321318}
322319323-_getMany (keys, options, callback) {
324-this.nextTick(callback, null, keys.map(key => this[kTree].get(key)))
320+async _getMany (keys, options) {
321+return keys.map(key => this[kTree].get(key))
325322}
326323327-_del (key, options, callback) {
324+async _del (key, options) {
328325this[kTree] = this[kTree].remove(key)
329-this.nextTick(callback)
330326}
331327332-_batch (operations, options, callback) {
328+async _batch (operations, options) {
333329let tree = this[kTree]
334330335331for (const op of operations) {
@@ -344,38 +340,35 @@ class MemoryLevel extends AbstractLevel {
344340}
345341346342this[kTree] = tree
347-this.nextTick(callback)
348343}
349344350-_clear (options, callback) {
345+async _clear (options) {
351346if (options.limit === -1 && !Object.keys(options).some(isRangeOption)) {
352347// Delete everything by creating a new empty tree.
353348this[kTree] = createRBT(compare)
354-return this.nextTick(callback)
349+return
355350}
356351357352const iterator = this._keys({ ...options })
358353const limit = iterator.limit
359354360355let count = 0
361356362-const loop = () => {
357+while (true) {
363358// TODO: add option to control "batch size"
364359for (let i = 0; i < 500; i++) {
365-if (++count > limit) return callback()
366-if (!iterator[kIterator].valid) return callback()
367-if (!iterator[kTest](iterator[kIterator].key)) return callback()
360+if (++count > limit) return
361+if (!iterator[kIterator].valid) return
362+if (!iterator[kTest](iterator[kIterator].key)) return
368363369364// Must also include changes made in parallel to clear()
370365this[kTree] = this[kTree].remove(iterator[kIterator].key)
371366iterator[kIterator][iterator[kAdvance]]()
372367}
373368374369// Some time to breathe
375-this.nextTick(loop)
370+await breathe()
376371}
377-378-this.nextTick(loop)
379372}
380373381374_iterator (options) {
@@ -393,18 +386,17 @@ class MemoryLevel extends AbstractLevel {
393386394387exports.MemoryLevel = MemoryLevel
395388396-// Use setImmediate() in Node.js to allow IO in between our callbacks
389+let breathe
390+391+// Use setImmediate() in Node.js to allow IO in between work
397392if (typeof process !== 'undefined' && !process.browser && typeof global !== 'undefined' && typeof global.setImmediate === 'function') {
398393const setImmediate = global.setImmediate
399394400-// Automatically applies to iterators, sublevels and chained batches as well
401-MemoryLevel.prototype.nextTick = function (fn, ...args) {
402-if (args.length === 0) {
403-setImmediate(fn)
404-} else {
405-setImmediate(() => fn(...args))
406-}
395+breathe = function () {
396+return new Promise(setImmediate)
407397}
398+} else {
399+breathe = async function () {}
408400}
409401410402function isRangeOption (k) {