Skip to content

Commit 74b1706

Browse files
authored
Add signals (#22)
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) { // .. }) } ```
1 parent d8893b0 commit 74b1706

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ if (db.supports.additionalMethods.foo) {
272272

273273
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://github.com/Level/supports/issues/1)).
274274

275+
### `signals` (object)
276+
277+
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:
278+
279+
```js
280+
if (db.supports.signals.iterators) {
281+
const ac = new AbortController()
282+
const iterator = db.keys({ signal: ac.signal })
283+
284+
ac.abort()
285+
}
286+
```
287+
275288
## Install
276289

277290
With [npm](https://npmjs.org) do:

index.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,20 @@ export interface IManifest {
103103
* ```
104104
*/
105105
additionalMethods: Record<string, boolean>
106+
107+
/**
108+
* Which methods or method groups take a `signal` option? At the time of writing there
109+
* is only one method group: `iterators`. This includes `db.iterator()`, `db.keys()` and
110+
* `db.values()`. For example:
111+
*
112+
* ```js
113+
* if (db.supports.signals.iterators) {
114+
* const ac = new AbortController()
115+
* const iterator = db.keys({ signal: ac.signal })
116+
*
117+
* ac.abort()
118+
* }
119+
* ```
120+
*/
121+
signals: Record<string, boolean>
106122
}

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ exports.supports = function supports (...manifests) {
1313
streams: manifest.streams || false,
1414
encodings: Object.assign({}, manifest.encodings),
1515
events: Object.assign({}, manifest.events),
16-
additionalMethods: Object.assign({}, manifest.additionalMethods)
16+
additionalMethods: Object.assign({}, manifest.additionalMethods),
17+
signals: Object.assign({}, manifest.signals)
1718
})
1819
}

test/shape.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const hasOwnProperty = Object.prototype.hasOwnProperty
55
module.exports = function shape (t, manifest) {
66
t.ok(isObject(manifest), 'manifest is object')
77
t.ok(isObject(manifest.additionalMethods), 'additionalMethods is object')
8+
t.ok(isObject(manifest.signals), 'signals is object')
89

910
for (const k in manifest) {
1011
if (!hasOwnProperty.call(manifest, k)) continue

0 commit comments

Comments
 (0)