UNPKG

2.67 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.8.0
2(function() {
3 var AbstractChainedBatch, Errors, InvalidArgumentError, setImmediate;
4
5 setImmediate = global.setImmediate || process.nextTick;
6
7 Errors = require("./abstract-error");
8
9 InvalidArgumentError = Errors.InvalidArgumentError;
10
11 module.exports = AbstractChainedBatch = (function() {
12 function AbstractChainedBatch(db) {
13 this._db = db;
14 this._operations = [];
15 this._written = false;
16 }
17
18 AbstractChainedBatch.prototype._checkWritten = function() {
19 if (this._written) {
20 throw new Error("write() already called on this batch");
21 }
22 };
23
24 AbstractChainedBatch.prototype.put = function(key, value) {
25 var err;
26 this._checkWritten();
27 err = this._db._checkKey(key, "key", this._db._isBuffer);
28 if (err) {
29 throw err;
30 }
31 if (!this._db._isBuffer(key)) {
32 key = String(key);
33 }
34 if (!this._db._isBuffer(value)) {
35 value = String(value);
36 }
37 if (typeof this._put === "function") {
38 this._put(key, value);
39 } else {
40 this._operations.push({
41 type: "put",
42 key: key,
43 value: value
44 });
45 }
46 return this;
47 };
48
49 AbstractChainedBatch.prototype.del = function(key) {
50 var err;
51 this._checkWritten();
52 err = this._db._checkKey(key, "key", this._db._isBuffer);
53 if (err) {
54 throw err;
55 }
56 if (!this._db._isBuffer(key)) {
57 key = String(key);
58 }
59 if (typeof this._del === "function") {
60 this._del(key);
61 } else {
62 this._operations.push({
63 type: "del",
64 key: key
65 });
66 }
67 return this;
68 };
69
70 AbstractChainedBatch.prototype.clear = function() {
71 this._checkWritten();
72 this._operations = [];
73 if (typeof this._clear === "function") {
74 this._clear();
75 }
76 return this;
77 };
78
79 AbstractChainedBatch.prototype.write = function(options, callback) {
80 this._checkWritten();
81 if (typeof options === "function") {
82 callback = options;
83 }
84 if (typeof callback !== "function") {
85 throw new InvalidArgumentError("write() requires a callback argument");
86 }
87 if (typeof options !== "object") {
88 options = {};
89 }
90 this._written = true;
91 if (typeof this._write === "function") {
92 return this._write(callback);
93 }
94 if (typeof this._db._batch === "function") {
95 return this._db._batch(this._operations, options, callback);
96 }
97 return setImmediate(callback);
98 };
99
100 return AbstractChainedBatch;
101
102 })();
103
104}).call(this);