Skip to content

Commit

Permalink
Add signals (#22)
Browse files Browse the repository at this point in the history
Mainly for tests. This way I can add support of signals to
`classic-level` and add tests for it in `abstract-level` that do:

```
if (db.supports.signals?.iterators) {
  test('abort', function (t) {
    // ..
  })
}
```
  • Loading branch information
vweevers authored Jan 27, 2024
1 parent d8893b0 commit 74b1706
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@ if (db.supports.additionalMethods.foo) {

For future extensibility, the properties of `additionalMethods` should be taken as truthy rather than strictly typed booleans. We may add additional metadata (see [#1](https:/Level/supports/issues/1)).

### `signals` (object)

Which methods or method groups take a `signal` option? At the time of writing there is only one method group: `iterators`. This includes `db.iterator()`, `db.keys()` and `db.values()`. For example:

```js
if (db.supports.signals.iterators) {
const ac = new AbortController()
const iterator = db.keys({ signal: ac.signal })

ac.abort()
}
```

## Install

With [npm](https://npmjs.org) do:
Expand Down
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ export interface IManifest {
* ```
*/
additionalMethods: Record<string, boolean>

/**
* Which methods or method groups take a `signal` option? At the time of writing there
* is only one method group: `iterators`. This includes `db.iterator()`, `db.keys()` and
* `db.values()`. For example:
*
* ```js
* if (db.supports.signals.iterators) {
* const ac = new AbortController()
* const iterator = db.keys({ signal: ac.signal })
*
* ac.abort()
* }
* ```
*/
signals: Record<string, boolean>
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports.supports = function supports (...manifests) {
streams: manifest.streams || false,
encodings: Object.assign({}, manifest.encodings),
events: Object.assign({}, manifest.events),
additionalMethods: Object.assign({}, manifest.additionalMethods)
additionalMethods: Object.assign({}, manifest.additionalMethods),
signals: Object.assign({}, manifest.signals)
})
}
1 change: 1 addition & 0 deletions test/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const hasOwnProperty = Object.prototype.hasOwnProperty
module.exports = function shape (t, manifest) {
t.ok(isObject(manifest), 'manifest is object')
t.ok(isObject(manifest.additionalMethods), 'additionalMethods is object')
t.ok(isObject(manifest.signals), 'signals is object')

for (const k in manifest) {
if (!hasOwnProperty.call(manifest, k)) continue
Expand Down

0 comments on commit 74b1706

Please sign in to comment.