Skip to content

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 97eb4b7 commit 3393c9c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 114 deletions.
1 change: 0 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ updates:
ignore:
- dependency-name: dependency-check
- dependency-name: nyc
- dependency-name: standard
31 changes: 17 additions & 14 deletions deferred-iterator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var inherits = require('inherits')
'use strict'

const AbstractIterator = require('abstract-leveldown').AbstractIterator
const inherits = require('inherits')

function DeferredIterator (db, options) {
AbstractIterator.call(this, db)
Expand All @@ -12,26 +14,27 @@ function DeferredIterator (db, options) {
inherits(DeferredIterator, AbstractIterator)

DeferredIterator.prototype.setDb = function (db) {
var it = this._iterator = db.iterator(this._options)
this._operations.forEach(function (op) {
it[op.method].apply(it, op.args)
})
const it = this._iterator = db.iterator(this._options)

for (const op of this._operations) {
it[op.method](...op.args)
}
}

DeferredIterator.prototype._operation = function (method, args) {
if (this._iterator) return this._iterator[method].apply(this._iterator, args)
this._operations.push({ method: method, args: args })
if (this._iterator) return this._iterator[method](...args)
this._operations.push({ method, args })
}

'next end'.split(' ').forEach(function (m) {
DeferredIterator.prototype['_' + m] = function () {
this._operation(m, arguments)
for (const m of ['next', 'end']) {
DeferredIterator.prototype['_' + m] = function (...args) {
this._operation(m, args)
}
})
}

// Must defer seek() rather than _seek() because it requires db._serializeKey to be available
DeferredIterator.prototype.seek = function () {
this._operation('seek', arguments)
DeferredIterator.prototype.seek = function (...args) {
this._operation('seek', args)
}

module.exports = DeferredIterator
77 changes: 40 additions & 37 deletions deferred-leveldown.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var inherits = require('inherits')
var DeferredIterator = require('./deferred-iterator')
var deferrables = 'put get del batch clear'.split(' ')
var optionalDeferrables = 'approximateSize compactRange'.split(' ')
'use strict'

const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const inherits = require('inherits')
const DeferredIterator = require('./deferred-iterator')
const deferrables = 'put get del batch clear'.split(' ')
const optionalDeferrables = 'approximateSize compactRange'.split(' ')

function DeferredLevelDOWN (db) {
AbstractLevelDOWN.call(this, db.supports || {})

// TODO (future major): remove this fallback; db must have manifest that
// declares approximateSize and compactRange in additionalMethods.
optionalDeferrables.forEach(function (m) {
for (const m of optionalDeferrables) {
if (typeof db[m] === 'function' && !this.supports.additionalMethods[m]) {
this.supports.additionalMethods[m] = true
}
}, this)
}

this._db = db
this._operations = []

closed(this)
}

Expand All @@ -25,61 +28,61 @@ inherits(DeferredLevelDOWN, AbstractLevelDOWN)
DeferredLevelDOWN.prototype.type = 'deferred-leveldown'

DeferredLevelDOWN.prototype._open = function (options, callback) {
var self = this

this._db.open(options, function (err) {
this._db.open(options, (err) => {
if (err) return callback(err)

self._operations.forEach(function (op) {
for (const op of this._operations) {
if (op.iterator) {
op.iterator.setDb(self._db)
op.iterator.setDb(this._db)
} else {
self._db[op.method].apply(self._db, op.args)
this._db[op.method](...op.args)
}
})
self._operations = []
}

open(self)
this._operations = []

open(this)
callback()
})
}

DeferredLevelDOWN.prototype._close = function (callback) {
var self = this

this._db.close(function (err) {
this._db.close((err) => {
if (err) return callback(err)
closed(self)
closed(this)
callback()
})
}

function open (self) {
deferrables.concat('iterator').forEach(function (m) {
self['_' + m] = function () {
return this._db[m].apply(this._db, arguments)
for (const m of deferrables.concat('iterator')) {
self['_' + m] = function (...args) {
return this._db[m](...args)
}
})
Object.keys(self.supports.additionalMethods).forEach(function (m) {
self[m] = function () {
return this._db[m].apply(this._db, arguments)
}

for (const m of Object.keys(self.supports.additionalMethods)) {
self[m] = function (...args) {
return this._db[m](...args)
}
})
}
}

function closed (self) {
deferrables.forEach(function (m) {
self['_' + m] = function () {
this._operations.push({ method: m, args: arguments })
for (const m of deferrables) {
self['_' + m] = function (...args) {
this._operations.push({ method: m, args })
}
})
Object.keys(self.supports.additionalMethods).forEach(function (m) {
self[m] = function () {
this._operations.push({ method: m, args: arguments })
}

for (const m of Object.keys(self.supports.additionalMethods)) {
self[m] = function (...args) {
this._operations.push({ method: m, args })
}
})
}

self._iterator = function (options) {
var it = new DeferredIterator(self, options)
const it = new DeferredIterator(self, options)
this._operations.push({ iterator: it })
return it
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"memdown": "^5.0.0",
"nyc": "^14.0.0",
"reachdown": "^1.0.0",
"standard": "^14.0.0",
"standard": "^16.0.3",
"tape": "^5.0.1"
},
"hallmark": {
Expand Down
Loading

0 comments on commit 3393c9c

Please sign in to comment.