Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 2226bba

Browse files
committed
Bump deferred-leveldown from 6.x to 7.x
As well as the encoding-down and memdown devDependencies. Adds `db.status` and `db.isOperational()` for API parity. Ref Level/deferred-leveldown#90
1 parent 02cf2d3 commit 2226bba

File tree

6 files changed

+75
-28
lines changed

6 files changed

+75
-28
lines changed

lib/levelup.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function LevelUP (db, options, callback) {
5959

6060
this.options = getOptions(options)
6161
this._db = db
62-
this.db = new DeferredLevelDOWN(db)
62+
this.db = null
6363
this.open(callback || ((err) => {
6464
if (err) this.emit('error', err)
6565
}))
@@ -88,6 +88,19 @@ LevelUP.prototype.emit = EventEmitter.prototype.emit
8888
LevelUP.prototype.once = EventEmitter.prototype.once
8989
inherits(LevelUP, EventEmitter)
9090

91+
// TODO: docs and tests
92+
Object.defineProperty(LevelUP.prototype, 'status', {
93+
enumerable: true,
94+
get () {
95+
return this.db.status
96+
}
97+
})
98+
99+
// TODO: docs and tests
100+
LevelUP.prototype.isOperational = function () {
101+
return this.db.status === 'open' || this.db.status === 'opening'
102+
}
103+
91104
LevelUP.prototype.open = function (opts, callback) {
92105
if (typeof opts === 'function') {
93106
callback = opts
@@ -100,16 +113,21 @@ LevelUP.prototype.open = function (opts, callback) {
100113
opts = this.options
101114
}
102115

103-
if (this.isOpen()) {
116+
// 1) Don't check db.status until levelup has opened,
117+
// in order for levelup events to be consistent
118+
if (this.db && this.isOpen()) {
104119
nextTick(callback, null, this)
105120
return callback.promise
106121
}
107122

108-
if (this._isOpening()) {
123+
if (this.db && this._isOpening()) {
109124
this.once('open', () => { callback(null, this) })
110125
return callback.promise
111126
}
112127

128+
// 2) Instead let deferred-leveldown handle already-open cases.
129+
// TODO: ideally though, levelup would have its own status
130+
this.db = new DeferredLevelDOWN(this._db)
113131
this.emit('opening')
114132

115133
this.db.open(opts, (err) => {
@@ -134,7 +152,6 @@ LevelUP.prototype.close = function (callback) {
134152
callback(err, ...rest)
135153
})
136154
this.emit('closing')
137-
this.db = new DeferredLevelDOWN(this._db)
138155
} else if (this.isClosed()) {
139156
nextTick(callback)
140157
} else if (this.db.status === 'closing') {
@@ -148,14 +165,17 @@ LevelUP.prototype.close = function (callback) {
148165
return callback.promise
149166
}
150167

168+
// TODO: remove in future major
151169
LevelUP.prototype.isOpen = function () {
152170
return this.db.status === 'open'
153171
}
154172

173+
// TODO: remove in future major
155174
LevelUP.prototype._isOpening = function () {
156175
return this.db.status === 'opening'
157176
}
158177

178+
// TODO: remove in future major
159179
LevelUP.prototype.isClosed = function () {
160180
return (/^clos|new/).test(this.db.status)
161181
}
@@ -306,10 +326,12 @@ LevelUP.prototype.toString = function () {
306326
LevelUP.prototype.type = 'levelup'
307327

308328
function maybeError (db, callback) {
309-
if (!db._isOpening() && !db.isOpen()) {
329+
if (!db.isOperational()) {
310330
nextTick(callback, new ReadError('Database is not open'))
311331
return true
312332
}
333+
334+
return false
313335
}
314336

315337
LevelUP.errors = errors

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
],
2626
"dependencies": {
2727
"catering": "^2.0.0",
28-
"deferred-leveldown": "^6.0.0",
28+
"deferred-leveldown": "^7.0.0",
2929
"level-errors": "^3.0.0",
3030
"level-iterator-stream": "^5.0.0",
31-
"level-supports": "^2.0.0",
31+
"level-supports": "^2.0.1",
3232
"queue-microtask": "^1.2.3"
3333
},
3434
"devDependencies": {
@@ -41,12 +41,12 @@
4141
"concat-stream": "^2.0.0",
4242
"delayed": "^2.0.0",
4343
"dependency-check": "^4.1.0",
44-
"encoding-down": "^7.0.0",
44+
"encoding-down": "^7.1.0",
4545
"faucet": "^0.0.1",
4646
"hallmark": "^3.1.0",
4747
"level-community": "^3.0.0",
4848
"level-concat-iterator": "^3.0.0",
49-
"memdown": "^6.0.0",
49+
"memdown": "^6.1.0",
5050
"nyc": "^15.1.0",
5151
"run-parallel": "^1.2.0",
5252
"run-series": "^1.1.8",

test/deferred-open-test.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = function (test, testCommon) {
1010
test('deferred open(): put() and get() on new database', function (t) {
1111
// 1) open database without callback, opens in next tick
1212
const db = testCommon.factory()
13-
t.ok(typeof db === 'object' && db !== null)
1413

1514
parallel([
1615
// 2) insert 3 values with put(), these should be deferred until the database is actually open
@@ -34,15 +33,29 @@ module.exports = function (test, testCommon) {
3433
})
3534
})
3635

37-
// we should still be in a state of limbo down here, not opened or closed, but 'new'
38-
t.is(db.isOpen(), false)
39-
t.is(db.isClosed(), false)
36+
t.is(db.status, 'opening')
37+
})
38+
39+
test('deferred open(): put() and get() on reopened database', async function (t) {
40+
const db = testCommon.factory()
41+
42+
await db.close()
43+
t.is(db.status, 'closed')
44+
45+
db.open(() => {})
46+
t.is(db.status, 'opening')
47+
48+
await db.put('beep', 'boop')
49+
50+
t.is(db.status, 'open')
51+
t.is(await db.get('beep', { asBuffer: false }), 'boop')
52+
53+
await db.close()
4054
})
4155

4256
test('deferred open(): batch() on new database', function (t) {
4357
// 1) open database without callback, opens in next tick
4458
const db = testCommon.factory()
45-
t.ok(typeof db === 'object' && db !== null)
4659

4760
// 2) insert 3 values with batch(), these should be deferred until the database is actually open
4861
db.batch([
@@ -66,15 +79,12 @@ module.exports = function (test, testCommon) {
6679
})
6780
})
6881

69-
// we should still be in a state of limbo down here, not opened or closed, but 'new'
70-
t.is(db.isOpen(), false)
71-
t.is(db.isClosed(), false)
82+
t.is(db.status, 'opening')
7283
})
7384

7485
test('deferred open(): chained batch() on new database', function (t) {
7586
// 1) open database without callback, opens in next tick
7687
const db = testCommon.factory()
77-
t.ok(typeof db === 'object' && db !== null)
7888

7989
// 2) insert 3 values with batch(), these should be deferred until the database is actually open
8090
db.batch()
@@ -98,9 +108,7 @@ module.exports = function (test, testCommon) {
98108
})
99109
})
100110

101-
// we should still be in a state of limbo down here, not opened or closed, but 'new'
102-
t.is(db.isOpen(), false)
103-
t.is(db.isClosed(), false)
111+
t.is(db.status, 'opening')
104112
})
105113

106114
testCommon.streams && test('deferred open(): test deferred ReadStream', function (t) {

test/maybe-error-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function (test, testCommon) {
1414
t.is(err.message, 'Database is not open')
1515
})
1616
t.is(sync, false, '.put cb called asynchronously')
17-
done()
17+
done() // TODO: called too soon, we still have 2 pending assertions
1818
})
1919
})
2020
})
@@ -72,4 +72,19 @@ module.exports = function (test, testCommon) {
7272
})
7373
})
7474
})
75+
76+
test('maybeError() should be called async: getMany()', function (t) {
77+
discardable(t, testCommon, function (db, done) {
78+
db.close(function () {
79+
t.is(db.status, 'closed', 'db is closed')
80+
let sync = false
81+
db.getMany(['key'], function (err) {
82+
sync = true
83+
t.is(err && err.message, 'Database is not open')
84+
done()
85+
})
86+
t.is(sync, false, '.getMany cb called asynchronously')
87+
})
88+
})
89+
})
7590
}

test/open-patchsafe-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ module.exports = function (test, testCommon) {
4949

5050
function makeTest (fn) {
5151
return function (t) {
52-
// 1) open database without callback, opens in next tick
52+
// Open database without callback, opens in next tick
5353
const db = testCommon.factory()
5454

5555
fn(t, db, function (err) {
5656
t.ifError(err, 'no test error')
5757
db.close(t.end.bind(t))
5858
})
5959

60-
// we should still be in a state of limbo down here, not opened or closed, but 'new'
60+
// Expected state is 'opening'
61+
t.is(db._isOpening(), true)
6162
t.is(db.isOpen(), false)
6263
t.is(db.isClosed(), false)
6364
}

test/self/manifest-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ test('manifest: additionalMethod is proxied', function (t) {
1212
mem.supports = { additionalMethods: { beep: true } }
1313

1414
const db = levelup(mem)
15+
const noop = () => {}
1516

1617
t.is(typeof db.beep, 'function')
1718
t.is(typeof levelup.prototype.beep, 'undefined')
1819

19-
db.beep()
20+
db.beep(noop)
2021
t.is(mem.beep.callCount, 0, 'deferred')
2122

2223
db.on('open', function () {
2324
t.is(mem.beep.callCount, 1)
24-
t.same(mem.beep.getCall(0).args, [])
25+
t.same(mem.beep.getCall(0).args, [noop])
2526

26-
db.beep('boop')
27-
t.same(mem.beep.getCall(1).args, ['boop'])
27+
db.beep('boop', noop)
28+
t.same(mem.beep.getCall(1).args, ['boop', noop])
2829

2930
db.close(t.end.bind(t))
3031
})

0 commit comments

Comments
 (0)