This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
abstract-chained-batch.js
90 lines (66 loc) · 2.14 KB
/
abstract-chained-batch.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
'use strict'
const emptyOptions = Object.freeze({})
function AbstractChainedBatch (db) {
if (typeof db !== 'object' || db === null) {
throw new TypeError('First argument must be an abstract-leveldown compliant store')
}
this.db = db
this._operations = []
this._written = false
}
AbstractChainedBatch.prototype._checkWritten = function () {
if (this._written) {
throw new Error('write() already called on this batch')
}
}
AbstractChainedBatch.prototype.put = function (key, value, options) {
this._checkWritten()
const err = this.db._checkKey(key) || this.db._checkValue(value)
if (err) throw err
key = this.db._serializeKey(key)
value = this.db._serializeValue(value)
this._put(key, value, options != null ? options : emptyOptions)
return this
}
AbstractChainedBatch.prototype._put = function (key, value, options) {
this._operations.push({ ...options, type: 'put', key, value })
}
AbstractChainedBatch.prototype.del = function (key, options) {
this._checkWritten()
const err = this.db._checkKey(key)
if (err) throw err
key = this.db._serializeKey(key)
this._del(key, options != null ? options : emptyOptions)
return this
}
AbstractChainedBatch.prototype._del = function (key, options) {
this._operations.push({ ...options, type: 'del', key })
}
AbstractChainedBatch.prototype.clear = function () {
this._checkWritten()
this._clear()
return this
}
AbstractChainedBatch.prototype._clear = function () {
this._operations = []
}
AbstractChainedBatch.prototype.write = function (options, callback) {
this._checkWritten()
if (typeof options === 'function') {
callback = options
}
if (typeof callback !== 'function') {
throw new Error('write() requires a callback argument')
}
if (typeof options !== 'object' || options === null) {
options = {}
}
this._written = true
this._write(options, callback)
}
AbstractChainedBatch.prototype._write = function (options, callback) {
this.db._batch(this._operations, options, callback)
}
// Expose browser-compatible nextTick for dependents
AbstractChainedBatch.prototype._nextTick = require('./next-tick')
module.exports = AbstractChainedBatch