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

Commit

Permalink
Breaking: 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 11, 2021
1 parent 14969c8 commit d8ad014
Show file tree
Hide file tree
Showing 44 changed files with 373 additions and 381 deletions.
2 changes: 0 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ updates:
ignore:
- dependency-name: dependency-check
- dependency-name: sinon
- dependency-name: airtap
- dependency-name: nyc
- dependency-name: standard

# Pinned to 1.1.10 for browser compatibility (later versions depend
# on `queue-microtask` which uses arrow functions and promises).
Expand Down
18 changes: 10 additions & 8 deletions lib/batch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var WriteError = require('level-errors').WriteError
var catering = require('catering')
var getCallback = require('./common').getCallback
var getOptions = require('./common').getOptions
'use strict'

const WriteError = require('level-errors').WriteError
const catering = require('catering')
const getCallback = require('./common').getCallback
const getOptions = require('./common').getOptions

function Batch (levelup) {
// TODO (next major): remove this._levelup alias
Expand All @@ -18,7 +20,7 @@ Batch.prototype.put = function (key, value) {
throw new WriteError(e)
}

this.ops.push({ type: 'put', key: key, value: value })
this.ops.push({ type: 'put', key, value })
this.length++

return this
Expand All @@ -31,7 +33,7 @@ Batch.prototype.del = function (key) {
throw new WriteError(err)
}

this.ops.push({ type: 'del', key: key })
this.ops.push({ type: 'del', key })
this.length++

return this
Expand All @@ -51,8 +53,8 @@ Batch.prototype.clear = function () {
}

Batch.prototype.write = function (options, callback) {
var levelup = this._levelup
var ops = this.ops
const levelup = this._levelup
const ops = this.ops

callback = getCallback(options, callback)
callback = catering.fromCallback(callback)
Expand Down
2 changes: 2 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

exports.getCallback = function (options, callback) {
return typeof options === 'function' ? options : callback
}
Expand Down
109 changes: 49 additions & 60 deletions lib/levelup.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var extend = require('xtend')
var DeferredLevelDOWN = require('deferred-leveldown')
var IteratorStream = require('level-iterator-stream')
var Batch = require('./batch')
var errors = require('level-errors')
var supports = require('level-supports')
var assert = require('assert')
var catering = require('catering')
var getCallback = require('./common').getCallback
var getOptions = require('./common').getOptions

var WriteError = errors.WriteError
var ReadError = errors.ReadError
var NotFoundError = errors.NotFoundError
var OpenError = errors.OpenError
var InitializationError = errors.InitializationError
'use strict'

const EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const extend = require('xtend')
const DeferredLevelDOWN = require('deferred-leveldown')
const IteratorStream = require('level-iterator-stream')
const Batch = require('./batch')
const errors = require('level-errors')
const supports = require('level-supports')
const assert = require('assert')
const catering = require('catering')
const getCallback = require('./common').getCallback
const getOptions = require('./common').getOptions

const WriteError = errors.WriteError
const ReadError = errors.ReadError
const NotFoundError = errors.NotFoundError
const OpenError = errors.OpenError
const InitializationError = errors.InitializationError

// Possible AbstractLevelDOWN#status values:
// - 'new' - newly created, not opened or closed
Expand All @@ -30,8 +32,7 @@ function LevelUP (db, options, callback) {
return new LevelUP(db, options, callback)
}

var error
var self = this
let error

EventEmitter.call(this)
this.setMaxListeners(Infinity)
Expand All @@ -56,9 +57,9 @@ function LevelUP (db, options, callback) {
this.options = getOptions(options)
this._db = db
this.db = new DeferredLevelDOWN(db)
this.open(callback || function (err) {
if (err) self.emit('error', err)
})
this.open(callback || ((err) => {
if (err) this.emit('error', err)
}))

// Create manifest based on deferred-leveldown's
this.supports = supports(this.db.supports, {
Expand All @@ -70,23 +71,21 @@ function LevelUP (db, options, callback) {
})

// Experimental: enrich levelup interface
Object.keys(this.supports.additionalMethods).forEach(function (method) {
if (this[method] != null) return
for (const method of Object.keys(this.supports.additionalMethods)) {
if (this[method] != null) continue

// Don't do this.db[method].bind() because this.db is dynamic.
this[method] = function () {
return this.db[method].apply(this.db, arguments)
this[method] = function (...args) {
return this.db[method](...args)
}
}, this)
}
}

LevelUP.prototype.emit = EventEmitter.prototype.emit
LevelUP.prototype.once = EventEmitter.prototype.once
inherits(LevelUP, EventEmitter)

LevelUP.prototype.open = function (opts, callback) {
var self = this

if (typeof opts === 'function') {
callback = opts
opts = null
Expand All @@ -99,39 +98,37 @@ LevelUP.prototype.open = function (opts, callback) {
}

if (this.isOpen()) {
process.nextTick(callback, null, self)
process.nextTick(callback, null, this)
return callback.promise
}

if (this._isOpening()) {
this.once('open', function () { callback(null, self) })
this.once('open', () => { callback(null, this) })
return callback.promise
}

this.emit('opening')

this.db.open(opts, function (err) {
this.db.open(opts, (err) => {
if (err) {
return callback(new OpenError(err))
}
self.db = self._db
callback(null, self)
self.emit('open')
self.emit('ready')
this.db = this._db
callback(null, this)
this.emit('open')
this.emit('ready')
})

return callback.promise
}

LevelUP.prototype.close = function (callback) {
var self = this

callback = catering.fromCallback(callback)

if (this.isOpen()) {
this.db.close(function () {
self.emit('closed')
callback.apply(null, arguments)
this.db.close((err, ...rest) => {
this.emit('closed')
callback(err, ...rest)
})
this.emit('closing')
this.db = new DeferredLevelDOWN(this._db)
Expand All @@ -140,8 +137,8 @@ LevelUP.prototype.close = function (callback) {
} else if (this.db.status === 'closing') {
this.once('closed', callback)
} else if (this._isOpening()) {
this.once('open', function () {
self.close(callback)
this.once('open', () => {
this.close(callback)
})
}

Expand Down Expand Up @@ -186,8 +183,6 @@ LevelUP.prototype.get = function (key, options, callback) {
}

LevelUP.prototype.put = function (key, value, options, callback) {
var self = this

callback = getCallback(options, callback)
callback = catering.fromCallback(callback)

Expand All @@ -197,20 +192,18 @@ LevelUP.prototype.put = function (key, value, options, callback) {

options = getOptions(options)

this.db.put(key, value, options, function (err) {
this.db.put(key, value, options, (err) => {
if (err) {
return callback(new WriteError(err))
}
self.emit('put', key, value)
this.emit('put', key, value)
callback()
})

return callback.promise
}

LevelUP.prototype.del = function (key, options, callback) {
var self = this

callback = getCallback(options, callback)
callback = catering.fromCallback(callback)

Expand All @@ -220,11 +213,11 @@ LevelUP.prototype.del = function (key, options, callback) {

options = getOptions(options)

this.db.del(key, options, function (err) {
this.db.del(key, options, (err) => {
if (err) {
return callback(new WriteError(err))
}
self.emit('del', key)
this.emit('del', key)
callback()
})

Expand All @@ -236,8 +229,6 @@ LevelUP.prototype.batch = function (arr, options, callback) {
return new Batch(this)
}

var self = this

if (typeof arr === 'function') callback = arr
else callback = getCallback(options, callback)

Expand All @@ -249,11 +240,11 @@ LevelUP.prototype.batch = function (arr, options, callback) {

options = getOptions(options)

this.db.batch(arr, options, function (err) {
this.db.batch(arr, options, (err) => {
if (err) {
return callback(new WriteError(err))
}
self.emit('batch', arr)
this.emit('batch', arr)
callback()
})

Expand All @@ -265,8 +256,6 @@ LevelUP.prototype.iterator = function (options) {
}

LevelUP.prototype.clear = function (options, callback) {
var self = this

callback = getCallback(options, callback)
options = getOptions(options)
callback = catering.fromCallback(callback)
Expand All @@ -275,11 +264,11 @@ LevelUP.prototype.clear = function (options, callback) {
return callback.promise
}

this.db.clear(options, function (err) {
this.db.clear(options, (err) => {
if (err) {
return callback(new WriteError(err))
}
self.emit('clear', options)
this.emit('clear', options)
callback()
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"safe-buffer": "^5.1.0",
"simple-concat": "^1.0.0",
"sinon": "^7.4.2",
"standard": "^15.0.0",
"standard": "^16.0.3",
"tape": "^5.2.2",
"trickle": "0.0.2"
},
Expand Down
26 changes: 13 additions & 13 deletions test/batch-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var levelup = require('../lib/levelup')
var errors = levelup.errors
var each = require('async-each')
var series = require('run-series')
var discardable = require('./util/discardable')
const levelup = require('../lib/levelup')
const errors = levelup.errors
const each = require('async-each')
const series = require('run-series')
const discardable = require('./util/discardable')

module.exports = function (test, testCommon) {
test('array-form batch(): multiple puts', function (t) {
Expand Down Expand Up @@ -124,11 +124,11 @@ module.exports = function (test, testCommon) {

test('chained batch(): options', function (t) {
discardable(t, testCommon, function (db, done) {
var batch = db.batch()
var underlying = batch
const batch = db.batch()
let underlying = batch
while (underlying.batch) underlying = underlying.batch

var write = underlying.write.bind(underlying)
const write = underlying.write.bind(underlying)
underlying.write = function (options, cb) {
t.same(options, { foo: 'bar' })
write(options, cb)
Expand All @@ -144,9 +144,9 @@ module.exports = function (test, testCommon) {

testCommon.promises && test('chained batch(): promise interface - options', function (t) {
discardable(t, testCommon, function (db, done) {
var batch = db.batch()
const batch = db.batch()

var write = batch.batch.write.bind(batch.batch)
const write = batch.batch.write.bind(batch.batch)
batch.batch.write = function (options, cb) {
t.same(options, { foo: 'bar' })
write(options, cb)
Expand Down Expand Up @@ -194,7 +194,7 @@ module.exports = function (test, testCommon) {

test('chained batch(): exposes ops queue length', function (t) {
discardable(t, testCommon, function (db, done) {
var batch = db.batch()
const batch = db.batch()
.put('one', '1')
.del('two')
.put('three', '3')
Expand Down Expand Up @@ -288,7 +288,7 @@ module.exports = function (test, testCommon) {

test('chained batch() arguments', function (t) {
discardable(t, testCommon, function (db, done) {
var batch = db.batch()
const batch = db.batch()

t.test('chained batch() arguments: batch#put() with missing `value`', function (t) {
throws(t, batch.put.bind(batch, 'foo1'), function (err) {
Expand Down Expand Up @@ -360,7 +360,7 @@ module.exports = function (test, testCommon) {
t.is(err.message, 'write() already called on this batch')
}

var batch = db.batch()
const batch = db.batch()
batch.put('foo', 'bar').put('boom', 'bang').del('foo').write(function (err) {
t.ifError(err, 'no batch error')

Expand Down
Loading

0 comments on commit d8ad014

Please sign in to comment.