This repository was archived by the owner on Dec 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathiterator.js
More file actions
155 lines (125 loc) · 3.72 KB
/
iterator.js
File metadata and controls
155 lines (125 loc) · 3.72 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict'
var inherits = require('inherits')
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var createKeyRange = require('./util/key-range')
var deserialize = require('./util/deserialize')
var setImmediate = require('./util/immediate')
var noop = function () {}
module.exports = Iterator
function Iterator (db, location, options) {
AbstractIterator.call(this, db)
this._limit = options.limit
this._count = 0
this._callback = null
this._cache = []
this._completed = false
this._aborted = false
this._error = null
this._transaction = null
this._keys = options.keys
this._values = options.values
this._keyAsBuffer = options.keyAsBuffer
this._valueAsBuffer = options.valueAsBuffer
if (this._limit === 0) {
this._completed = true
return
}
try {
var keyRange = createKeyRange(options)
} catch (e) {
// The lower key is greater than the upper key.
// IndexedDB throws an error, but we'll just return 0 results.
this._completed = true
return
}
this.createIterator(location, keyRange, options.reverse)
}
inherits(Iterator, AbstractIterator)
Iterator.prototype.createIterator = function (location, keyRange, reverse) {
var self = this
var transaction = this.db.db.transaction([location], 'readonly')
var store = transaction.objectStore(location)
var req = store.openCursor(keyRange, reverse ? 'prev' : 'next')
req.onsuccess = function (ev) {
var cursor = ev.target.result
if (cursor) self.onItem(cursor)
}
this._transaction = transaction
// If an error occurs (on the request), the transaction will abort.
transaction.onabort = function () {
self.onAbort(self._transaction.error || new Error('aborted by user'))
}
transaction.oncomplete = function () {
self.onComplete()
}
}
Iterator.prototype.onItem = function (cursor) {
this._cache.push(cursor.key, cursor.value)
if (this._limit <= 0 || ++this._count < this._limit) {
cursor.continue()
}
this.maybeNext()
}
Iterator.prototype.onAbort = function (err) {
this._aborted = true
this._error = err
this.maybeNext()
}
Iterator.prototype.onComplete = function () {
this._completed = true
this.maybeNext()
}
Iterator.prototype.maybeNext = function () {
if (this._callback) {
this._next(this._callback)
this._callback = null
}
}
Iterator.prototype._next = function (callback) {
if (this._aborted) {
// The error should be picked up by either next() or end().
var err = this._error
this._error = null
setImmediate(function () {
callback(err)
})
} else if (this._cache.length > 0) {
var key = this._cache.shift()
var value = this._cache.shift()
if (this._keys && key !== undefined) {
key = this._deserializeKey(key, this._keyAsBuffer)
} else {
key = undefined
}
if (this._values && value !== undefined) {
value = this._deserializeValue(value, this._valueAsBuffer)
} else {
value = undefined
}
setImmediate(function () {
callback(null, key, value)
})
} else if (this._completed) {
setImmediate(callback)
} else {
this._callback = callback
}
}
// Exposed for the v4 to v5 upgrade utility
Iterator.prototype._deserializeKey = deserialize
Iterator.prototype._deserializeValue = deserialize
Iterator.prototype._end = function (callback) {
if (this._aborted || this._completed) {
var err = this._error
setImmediate(function () {
callback(err)
})
return
}
// Don't advance the cursor anymore, and the transaction will complete
// on its own in the next tick. This approach is much cleaner than calling
// transaction.abort() with its unpredictable event order.
this.onItem = noop
this.onAbort = callback
this.onComplete = callback
}