Level / abstract-level

Abstract class for a lexicographically sorted key-value database.
MIT License
128 stars 8 forks source link

Add experimental support of `AbortSignal` #55

Closed vweevers closed 2 years ago

vweevers commented 2 years ago

In #50 I made an early start with this, but I made two mistakes:

  1. Putting the signal in per-method options, e.g. iterator.next({ signal }), rather than in constructor options, i.e. db.iterator({ signal }). The former would not be accessible in a for await...of and come with a performance penalty.
  2. Thinking it could replace abortOnClose (#21) which is a separate issue. I'll leave it at that, because it only matters for many-level and can be solved and explained later.

Anyway, this PR enables the following:

const abortController = new AbortController()
const signal = abortController.signal

// Will result in 'aborted' log
abortController.abort()

try {
  for await (const entry of db.iterator({ signal })) {
    console.log(entry)
  }
} catch (err) {
  if (err.code === 'LEVEL_ABORTED') {
    console.log('aborted')
  }
}

Further explained in the README, for the public API and the private API (second paragraph).

This PR also fixes small bugs in the open() and close() methods of multiple classes, as follow-up for #50.