This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (41 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var used = false
// our plugin object, only cares about 'construction' extension
module.exports = {
constructor: function (options, next) {
// add a rangeDel() method to the current db instance
this.rangeDel = function () {
// the tricky bit is that the db may not be open so we may not
// even have a `this._db` object, so we may have to wait till it's
// ready
var args = Array.prototype.slice.call(arguments)
, go = function () {
this._db.rangeDel.apply(this._db, args)
}.bind(this)
// illegal behaviour
if (!arguments.length
|| (arguments.length == 1 && typeof arguments[0] != 'function')
|| (arguments.length > 1 && typeof arguments[1] != 'function'))
// this is done in the C++ too, but the deferred operation will also
// defer a throw which gets messy
throw new Error('rangeDel() requires a callback argument')
if (this.isClosed())
throw new Error('Can\'t call rangeDel() on a closed database')
// if open, then execute now, otherwise defer till 'ready'
if (this.isOpen())
go()
else
this.on('ready', go)
}
// must call next plugin, or actual constructure base
next(options)
}
}
// register
module.exports.use = function () {
if (used) return
used = true
// register Downer RangeDel with LevelDOWN
require('downer-rangedel').use()
// register us with LevelUP
require('levelup').use(module.exports)
}