Closed ccollie closed 2 years ago
Thinking about it, iterators are possible, but currently async _seek
is not
The result of a seek doesn't matter until next() is called, so it can be async behind the scenes.
The result of a seek doesn't matter until next() is called, so it can be async behind the scenes.
I worked around it by storing the seek
parameters and handling them if they exist in the other calls
Ctor.prototype[kInit] = function (db, options) {
const reverse = options.reverse
this[kDb] = db
this[kReverse] = reverse
this[kOptions] = options
this[kLocation] = db[kLocation]
this[kAdvance] = this[kReverse] ? this[kAdvancePrev] : this[kAdvanceNext]
this[kIndexPromise] = cacache.ls(location).then(entries => {
const keys = entries.map(entry => entry.key).sort(compare);
this[kEntries] = entries
const { start, end, lowerBound, upperBound } = parseBounds(keys, options)
this[kCursor] = 0
this[kLowerBound] = lowerBound
this[kUpperBound] = upperBound
this[kIndexKeys] = keys.splice(start, end - start + 1)
});
}
Ctor.prototype._seek = function (target, options) {
this[kSeekOptions] = {
...options,
target
}
}
Ctor.prototype[kNext] = function (callback) {
this[kAwaitIndex]().then(() => {
const key = this[kAdvance]()
if (!key) return this.nextTick(callback)
getValue(this[kLocation], key, (err, value) => {
callback(err, key, value.data)
})
}).catch(callback)
}
Ctor.prototype[kAwaitIndex] = function () {
const options = this[kSeekOptions]
if (!options) {
return this[kIndexPromise]
}
this[kSeekOptions] = null
// handle seek
return this[kIndexPromise].then(() =>
{
this[kCursor] = parseSeekParams(this, options)
}).catch(err => {
this[kDone] = true
this[kCursor] = -1
return err
});
}
I'm writing a module implementing
abstract-level
over cacache. One issue is that the api call incacache
to get index entries returns apromise
, so it's currently not possible AFAICS to implement iterators given thatseek
is not async.Is there a workaround using a
DeferredIterator
?