This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
101 lines (87 loc) · 2.24 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var DeferredIterator = require('deferred-leveldown').DeferredIterator
var EventEmitter = require('events').EventEmitter
var assert = require('assert')
var inherits = require('inherits')
var debug = require('debug')('level-lazy-open')
var slice = [].slice
module.exports = Lazy
inherits(Lazy, AbstractLevelDOWN)
function Lazy (factory) {
assert(factory, 'factory required')
if (!(this instanceof Lazy)) return new Lazy(factory)
AbstractLevelDOWN.call(this, '')
this._factory = factory
this._db = null
this._opening = false
this._closing = false
this._events = new EventEmitter()
}
Lazy.prototype._open = function (opts, cb) {
throw new Error('can\'t manually open level-lazy-open')
}
Lazy.prototype._ensureOpen = function (cb) {
var self = this
if (self._db) return setImmediate(cb)
if (self._opening) {
self._events.once('open', function () {
cb()
})
return
}
self._opening = true
self._factory(function (err, db) {
self._opening = false
if (err) return cb(err)
self._db = db
self._events.emit('open')
cb()
})
}
Lazy.prototype._close = function (cb) {
var self = this
if (!self._db && !self._opening) {
debug('close(), but not open')
throw new Error('not open')
}
if (self._opening) {
debug('close(), but opening')
self._events.once('open', function () {
self.close(cb)
})
return
}
if (self._db && !self._opening) {
debug('close()')
self._closing = true
self._db.close(function (err) {
if (err) return cb(err)
self.closing = false
self._db = false
cb()
self._events.emit('closed')
})
return
}
if (self._closing) {
debug('close(), but already closing')
return self._events.once('closed', cb)
}
}
'put get del batch approximateSize'.split(' ').forEach(function (m) {
Lazy.prototype['_' + m] = function () {
var self = this
var args = slice.call(arguments)
self._ensureOpen(function () {
self._db[m].apply(self._db, args)
})
}
})
Lazy.prototype._iterator = function (opts) {
var self = this
var it = new DeferredIterator(opts)
self._ensureOpen(function () {
it.setDb(self._db)
})
return it
}