-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiterator.js
More file actions
56 lines (45 loc) · 1.24 KB
/
Copy pathiterator.js
File metadata and controls
56 lines (45 loc) · 1.24 KB
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
const Util = require('util');
const AbstractIterator = require('abstract-leveldown').AbstractIterator;
const fastFuture = require('fast-future');
function Iterator(db, options) {
AbstractIterator.call(this, db);
this.binding = db.binding.iterator(options);
this.cache = null;
this.finished = false;
this.fastFuture = fastFuture();
}
Util.inherits(Iterator, AbstractIterator);
Iterator.prototype.seek = function(key) {
if (typeof key !== 'string')
throw new Error('seek requires a string key');
this.cache = null;
this.binding.seek(key);
};
Iterator.prototype._next = function(callback) {
var that = this;
var key, value;
if (this.cache && this.cache.length) {
key = this.cache.pop();
value = this.cache.pop();
this.fastFuture(function() {
callback(null, key, value);
});
} else if (this.finished) {
this.fastFuture(function() {
callback();
});
} else {
this.binding.next(function(err, array, finished) {
if (err) return callback(err);
that.cache = array;
that.finished = finished;
that._next(callback);
});
}
return this;
};
Iterator.prototype._end = function(callback) {
delete this.cache;
this.binding.end(callback);
};
module.exports = Iterator;