'use strict' const { AbstractKeyIterator, AbstractValueIterator } = require('../abstract-iterator') const kIterator = Symbol('iterator') const kCallback = Symbol('callback') const kHandleOne = Symbol('handleOne') const kHandleMany = Symbol('handleMany') class DefaultKeyIterator extends AbstractKeyIterator { constructor (db, options) { super(db, options) this[kIterator] = db.iterator({ ...options, keys: true, values: false }) this[kHandleOne] = this[kHandleOne].bind(this) this[kHandleMany] = this[kHandleMany].bind(this) } } class DefaultValueIterator extends AbstractValueIterator { constructor (db, options) { super(db, options) this[kIterator] = db.iterator({ ...options, keys: false, values: true }) this[kHandleOne] = this[kHandleOne].bind(this) this[kHandleMany] = this[kHandleMany].bind(this) } } for (const Iterator of [DefaultKeyIterator, DefaultValueIterator]) { const keys = Iterator === DefaultKeyIterator const mapEntry = keys ? (entry) => entry[0] : (entry) => entry[1] Iterator.prototype._next = function (callback) { this[kCallback] = callback this[kIterator].next(this[kHandleOne]) } Iterator.prototype[kHandleOne] = function (err, key, value) { const callback = this[kCallback] if (err) callback(err) else callback(null, keys ? key : value) } Iterator.prototype._nextv = function (size, options, callback) { this[kCallback] = callback this[kIterator].nextv(size, options, this[kHandleMany]) } Iterator.prototype._all = function (options, callback) { this[kCallback] = callback this[kIterator].all(options, this[kHandleMany]) } Iterator.prototype[kHandleMany] = function (err, entries) { const callback = this[kCallback] if (err) callback(err) else callback(null, entries.map(mapEntry)) } Iterator.prototype._seek = function (target, options) { this[kIterator].seek(target, options) } Iterator.prototype._close = function (callback) { this[kIterator].close(callback) } } // Internal utilities, should be typed as AbstractKeyIterator and AbstractValueIterator exports.DefaultKeyIterator = DefaultKeyIterator exports.DefaultValueIterator = DefaultValueIterator