Skip to content

Commit fc3f1e0

Browse files
committed
Add idempotentOpen and passiveOpen features
1 parent 22a3837 commit fc3f1e0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,39 @@ db.open(function (err) {
219219
})
220220
```
221221

222-
_TBD: whether this also includes methods like `isOpen()` and `isClosed()`._
222+
### `idempotentOpen` (boolean)
223+
224+
Are `db.open()` and `db.close()` idempotent and safe, such that:
225+
226+
1. Calling `open()` or `close()` twice has the same effect as calling it once
227+
2. If both `open()` and `close()` are called in succession while a previous call has not yet completed, the last call dictates the final `status` and all earlier calls yield an error if they oppose the final `status`.
228+
3. Callbacks are called in the order that the `open()` or `close()` calls were made.
229+
4. Callbacks are not called until any pending state changes are done, meaning that `status` is not (or no longer) 'opening' or 'closing'.
230+
5. If events are supported, they are emitted before callbacks are called, because event listeners may result in additional pending state changes.
231+
6. If events are supported, the 'open' and 'closed' events are not emitted if there are pending state changes or if the initial `status` matches the final `status`. They communicate a changed final `status` even though the (underlying) db may open and close multiple times before that. This avoids emitting (for example) an 'open' event while `status` is 'closing'.
232+
233+
For example:
234+
235+
1. In a sequence of calls like `open(); close(); open()` the final `status` will be 'open', the second call will receive an error, an 'open' event is emitted once, no 'closed' event is emitted, and all callbacks will see that `status` is 'open'. That is unless `open()` failed.
236+
2. If `open()` failed twice in the same sequence of calls, the final status will be 'closed', the first and last call receive an error, no events are emitted, and all callbacks will see that `status` is 'closed'.
237+
238+
_At the time of writing this is a new feature, subject to change, zero modules support it (including `levelup`)._
239+
240+
### `passiveOpen` (boolean)
241+
242+
Does db support `db.open({ passive: true })` which waits for but does not initiate opening? To the same but more comprehensive effect as :
243+
244+
```js
245+
if (db.status === 'opening') {
246+
db.once('open', callback)
247+
} else if (db.status === 'open') {
248+
queueMicrotask(callback)
249+
} else {
250+
// Yield error
251+
}
252+
```
253+
254+
_At the time of writing this is a new feature, subject to change, zero modules support it._
223255

224256
### `openCallback` (boolean)
225257

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module.exports = function supports (...manifests) {
1818

1919
// Features of abstract-leveldown that levelup doesn't have
2020
status: manifest.status || false,
21+
idempotentOpen: manifest.idempotentOpen || false,
22+
passiveOpen: manifest.passiveOpen || false,
23+
serialize: manifest.serialize || false,
2124

2225
// Features of disk-based implementations
2326
createIfMissing: manifest.createIfMissing || false,

0 commit comments

Comments
 (0)