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 52
/
abstract-iterator.js
110 lines (88 loc) · 2.54 KB
/
abstract-iterator.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
102
103
104
105
106
107
108
109
110
'use strict'
function AbstractIterator (db) {
if (typeof db !== 'object' || db === null) {
throw new TypeError('First argument must be an abstract-leveldown compliant store')
}
this.db = db
this._ended = false
this._nexting = false
}
AbstractIterator.prototype.next = function (callback) {
// In callback mode, we return `this`
let ret = this
if (callback === undefined) {
ret = new Promise(function (resolve, reject) {
callback = function (err, key, value) {
if (err) reject(err)
else if (key === undefined && value === undefined) resolve()
else resolve([key, value])
}
})
} else if (typeof callback !== 'function') {
throw new Error('next() requires a callback argument')
}
if (this._ended) {
this._nextTick(callback, new Error('cannot call next() after end()'))
return ret
}
if (this._nexting) {
this._nextTick(callback, new Error('cannot call next() before previous next() has completed'))
return ret
}
this._nexting = true
this._next((err, ...rest) => {
this._nexting = false
callback(err, ...rest)
})
return ret
}
AbstractIterator.prototype._next = function (callback) {
this._nextTick(callback)
}
AbstractIterator.prototype.seek = function (target) {
if (this._ended) {
throw new Error('cannot call seek() after end()')
}
if (this._nexting) {
throw new Error('cannot call seek() before next() has completed')
}
target = this.db._serializeKey(target)
this._seek(target)
}
AbstractIterator.prototype._seek = function (target) {}
AbstractIterator.prototype.end = function (callback) {
let promise
if (callback === undefined) {
promise = new Promise(function (resolve, reject) {
callback = function (err) {
if (err) reject(err)
else resolve()
}
})
} else if (typeof callback !== 'function') {
throw new Error('end() requires a callback argument')
}
if (this._ended) {
this._nextTick(callback, new Error('end() already called on iterator'))
return promise
}
this._ended = true
this._end(callback)
return promise
}
AbstractIterator.prototype._end = function (callback) {
this._nextTick(callback)
}
AbstractIterator.prototype[Symbol.asyncIterator] = async function * () {
try {
let kv
while ((kv = (await this.next())) !== undefined) {
yield kv
}
} finally {
if (!this._ended) await this.end()
}
}
// Expose browser-compatible nextTick for dependents
AbstractIterator.prototype._nextTick = require('./next-tick')
module.exports = AbstractIterator