Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Modernize syntax and bump standard (Level/community#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 9, 2021
1 parent 5eb4d56 commit 1035f3c
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 133 deletions.
2 changes: 0 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ updates:
schedule:
interval: monthly
ignore:
- dependency-name: buffer
- dependency-name: dependency-check
- dependency-name: uuid
- dependency-name: standard
89 changes: 48 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

module.exports = Level

var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var inherits = require('inherits')
var Iterator = require('./iterator')
var serialize = require('./util/serialize')
var deserialize = require('./util/deserialize')
var support = require('./util/support')
var clear = require('./util/clear')
var createKeyRange = require('./util/key-range')
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const inherits = require('inherits')
const Iterator = require('./iterator')
const serialize = require('./util/serialize')
const deserialize = require('./util/deserialize')
const support = require('./util/support')
const clear = require('./util/clear')
const createKeyRange = require('./util/key-range')

var DEFAULT_PREFIX = 'level-js-'
const DEFAULT_PREFIX = 'level-js-'

function Level (location, opts) {
if (!(this instanceof Level)) return new Level(location, opts)
Expand Down Expand Up @@ -41,34 +41,33 @@ inherits(Level, AbstractLevelDOWN)
Level.prototype.type = 'level-js'

Level.prototype._open = function (options, callback) {
var req = indexedDB.open(this.prefix + this.location, this.version)
var self = this
const req = indexedDB.open(this.prefix + this.location, this.version)

req.onerror = function () {
callback(req.error || new Error('unknown error'))
}

req.onsuccess = function () {
self.db = req.result
req.onsuccess = () => {
this.db = req.result
callback()
}

req.onupgradeneeded = function (ev) {
var db = ev.target.result
req.onupgradeneeded = (ev) => {
const db = ev.target.result

if (!db.objectStoreNames.contains(self.location)) {
db.createObjectStore(self.location)
if (!db.objectStoreNames.contains(this.location)) {
db.createObjectStore(this.location)
}
}
}

Level.prototype.store = function (mode) {
var transaction = this.db.transaction([this.location], mode)
const transaction = this.db.transaction([this.location], mode)
return transaction.objectStore(this.location)
}

Level.prototype.await = function (request, callback) {
var transaction = request.transaction
const transaction = request.transaction

// Take advantage of the fact that a non-canceled request error aborts
// the transaction. I.e. no need to listen for "request.onerror".
Expand All @@ -82,10 +81,11 @@ Level.prototype.await = function (request, callback) {
}

Level.prototype._get = function (key, options, callback) {
var store = this.store('readonly')
const store = this.store('readonly')
let req

try {
var req = store.get(key)
req = store.get(key)
} catch (err) {
return this._nextTick(callback, err)
}
Expand All @@ -103,10 +103,11 @@ Level.prototype._get = function (key, options, callback) {
}

Level.prototype._del = function (key, options, callback) {
var store = this.store('readwrite')
const store = this.store('readwrite')
let req

try {
var req = store.delete(key)
req = store.delete(key)
} catch (err) {
return this._nextTick(callback, err)
}
Expand All @@ -115,12 +116,13 @@ Level.prototype._del = function (key, options, callback) {
}

Level.prototype._put = function (key, value, options, callback) {
var store = this.store('readwrite')
const store = this.store('readwrite')
let req

try {
// Will throw a DataError or DataCloneError if the environment
// does not support serializing the key or value respectively.
var req = store.put(value, key)
req = store.put(value, key)
} catch (err) {
return this._nextTick(callback, err)
}
Expand All @@ -143,10 +145,10 @@ Level.prototype._iterator = function (options) {
Level.prototype._batch = function (operations, options, callback) {
if (operations.length === 0) return this._nextTick(callback)

var store = this.store('readwrite')
var transaction = store.transaction
var index = 0
var error
const store = this.store('readwrite')
const transaction = store.transaction
let index = 0
let error

transaction.onabort = function () {
callback(error || transaction.error || new Error('aborted by user'))
Expand All @@ -158,11 +160,13 @@ Level.prototype._batch = function (operations, options, callback) {

// Wait for a request to complete before making the next, saving CPU.
function loop () {
var op = operations[index++]
var key = op.key
const op = operations[index++]
const key = op.key

let req

try {
var req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
} catch (err) {
error = err
transaction.abort()
Expand All @@ -178,8 +182,11 @@ Level.prototype._batch = function (operations, options, callback) {
}

Level.prototype._clear = function (options, callback) {
let keyRange
let req

try {
var keyRange = createKeyRange(options)
keyRange = createKeyRange(options)
} catch (e) {
// The lower key is greater than the upper key.
// IndexedDB throws an error, but we'll just do nothing.
Expand All @@ -193,8 +200,8 @@ Level.prototype._clear = function (options, callback) {
}

try {
var store = this.store('readwrite')
var req = keyRange ? store.delete(keyRange) : store.clear()
const store = this.store('readwrite')
req = keyRange ? store.delete(keyRange) : store.clear()
} catch (err) {
return this._nextTick(callback, err)
}
Expand All @@ -213,9 +220,9 @@ Level.prototype.upgrade = function (callback) {
return this._nextTick(callback, new Error('cannot upgrade() before open()'))
}

var it = this.iterator()
var batchOptions = {}
var self = this
const it = this.iterator()
const batchOptions = {}
const self = this

it._deserializeKey = it._deserializeValue = identity
next()
Expand All @@ -230,8 +237,8 @@ Level.prototype.upgrade = function (callback) {
return finish(err)
}

var newKey = self._serializeKey(deserialize(key, true))
var newValue = self._serializeValue(deserialize(value, true))
const newKey = self._serializeKey(deserialize(key, true))
const newValue = self._serializeValue(deserialize(value, true))

// To bypass serialization on the old key, use _batch() instead of batch().
// NOTE: if we disable snapshotting (#86) this could lead to a loop of
Expand Down Expand Up @@ -259,7 +266,7 @@ Level.destroy = function (location, prefix, callback) {
callback = prefix
prefix = DEFAULT_PREFIX
}
var request = indexedDB.deleteDatabase(prefix + location)
const request = indexedDB.deleteDatabase(prefix + location)
request.onsuccess = function () {
callback()
}
Expand Down
43 changes: 22 additions & 21 deletions iterator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

var inherits = require('inherits')
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var createKeyRange = require('./util/key-range')
var deserialize = require('./util/deserialize')
var noop = function () {}
const inherits = require('inherits')
const AbstractIterator = require('abstract-leveldown').AbstractIterator
const createKeyRange = require('./util/key-range')
const deserialize = require('./util/deserialize')
const noop = function () {}

module.exports = Iterator

Expand All @@ -30,8 +30,10 @@ function Iterator (db, location, options) {
return
}

let keyRange

try {
var keyRange = createKeyRange(options)
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.
Expand All @@ -45,25 +47,24 @@ function Iterator (db, location, options) {
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)
const transaction = this.db.db.transaction([location], 'readonly')
const store = transaction.objectStore(location)
const req = store.openCursor(keyRange, reverse ? 'prev' : 'next')

req.onsuccess = (ev) => {
const cursor = ev.target.result
if (cursor) this.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.onabort = () => {
this.onAbort(this._transaction.error || new Error('aborted by user'))
}

transaction.oncomplete = function () {
self.onComplete()
transaction.oncomplete = () => {
this.onComplete()
}
}

Expand Down Expand Up @@ -98,12 +99,12 @@ Iterator.prototype.maybeNext = function () {
Iterator.prototype._next = function (callback) {
if (this._aborted) {
// The error should be picked up by either next() or end().
var err = this._error
const err = this._error
this._error = null
this._nextTick(callback, err)
} else if (this._cache.length > 0) {
var key = this._cache.shift()
var value = this._cache.shift()
let key = this._cache.shift()
let value = this._cache.shift()

if (this._keys && key !== undefined) {
key = this._deserializeKey(key, this._keyAsBuffer)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"level-community": "^3.0.0",
"level-concat-iterator": "^2.0.0",
"nyc": "^15.0.0",
"standard": "^15.0.1",
"standard": "^16.0.3",
"tape": "^5.0.0",
"uuid": "^3.3.2"
},
Expand Down
Loading

0 comments on commit 1035f3c

Please sign in to comment.