-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (46 loc) · 1.22 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
'use strict'
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var AbstractChainedBatch = require('abstract-leveldown').AbstractChainedBatch
var inherits = require('util').inherits
module.exports = function shared (batch) {
var forks = []
var todo = 0
var abstract = new AbstractLevelDOWN('')
return function fork () {
var forkedBatch = new ForwardingBatch(abstract, batch, function () {
process.nextTick(commit)
})
todo++
forks.push(forkedBatch)
return forkedBatch
}
function commit () {
if (--todo) return
batch.write(function (err) {
forks.forEach(function (forkedBatch) {
forkedBatch.cb(err)
})
})
}
}
function ForwardingBatch (db, batch, onwrite) {
AbstractChainedBatch.call(this)
this._db = db
this._batch = batch
this._onwrite = onwrite
this.cb = null
}
inherits(ForwardingBatch, AbstractChainedBatch)
ForwardingBatch.prototype._put = function (key, value) {
this._batch.put(key, value)
}
ForwardingBatch.prototype._del = function (key) {
this._batch.del(key)
}
ForwardingBatch.prototype._clear = function () {
this._batch.clear()
}
ForwardingBatch.prototype._write = function (cb) {
this.cb = cb
this._onwrite()
}