You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1007 B
35 lines
1007 B
4 years ago
|
'use strict'
|
||
|
|
||
|
module.exports = function clear (db, location, keyRange, options, callback) {
|
||
|
if (options.limit === 0) return db._nextTick(callback)
|
||
|
|
||
|
var transaction = db.db.transaction([location], 'readwrite')
|
||
|
var store = transaction.objectStore(location)
|
||
|
var count = 0
|
||
|
|
||
|
transaction.oncomplete = function () {
|
||
|
callback()
|
||
|
}
|
||
|
|
||
|
transaction.onabort = function () {
|
||
|
callback(transaction.error || new Error('aborted by user'))
|
||
|
}
|
||
|
|
||
|
// A key cursor is faster (skips reading values) but not supported by IE
|
||
|
var method = store.openKeyCursor ? 'openKeyCursor' : 'openCursor'
|
||
|
var direction = options.reverse ? 'prev' : 'next'
|
||
|
|
||
|
store[method](keyRange, direction).onsuccess = function (ev) {
|
||
|
var cursor = ev.target.result
|
||
|
|
||
|
if (cursor) {
|
||
|
// Wait for a request to complete before continuing, saving CPU.
|
||
|
store.delete(cursor.key).onsuccess = function () {
|
||
|
if (options.limit <= 0 || ++count < options.limit) {
|
||
|
cursor.continue()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|