Skip to content

Commit

Permalink
Merge pull request #700 from Level/catering
Browse files Browse the repository at this point in the history
Refactor promisify() code by using catering module
  • Loading branch information
ralphtheninja authored Sep 27, 2020
2 parents 79f323e + ea568e7 commit 33b1318
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 74 deletions.
12 changes: 3 additions & 9 deletions lib/batch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var WriteError = require('level-errors').WriteError
var promisify = require('./promisify')
var catering = require('catering')
var getCallback = require('./common').getCallback
var getOptions = require('./common').getOptions

Expand Down Expand Up @@ -53,15 +53,9 @@ Batch.prototype.clear = function () {
Batch.prototype.write = function (options, callback) {
var levelup = this._levelup
var ops = this.ops
var promise

callback = getCallback(options, callback)

if (!callback) {
callback = promisify()
promise = callback.promise
}

callback = catering.fromCallback(callback)
options = getOptions(options)

try {
Expand All @@ -74,7 +68,7 @@ Batch.prototype.write = function (options, callback) {
throw new WriteError(err)
}

return promise
return callback.promise
}

module.exports = Batch
79 changes: 27 additions & 52 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var Batch = require('./batch')
var errors = require('level-errors')
var supports = require('level-supports')
var assert = require('assert')
var promisify = require('./promisify')
var catering = require('catering')
var getCallback = require('./common').getCallback
var getOptions = require('./common').getOptions

Expand Down Expand Up @@ -86,30 +86,26 @@ inherits(LevelUP, EventEmitter)

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

if (typeof opts === 'function') {
callback = opts
opts = null
}

if (!callback) {
callback = promisify()
promise = callback.promise
}
callback = catering.fromCallback(callback)

if (!opts) {
opts = this.options
}

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

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

this.emit('opening')
Expand All @@ -124,17 +120,13 @@ LevelUP.prototype.open = function (opts, callback) {
self.emit('ready')
})

return promise
return callback.promise
}

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

if (!callback) {
callback = promisify()
promise = callback.promise
}
callback = catering.fromCallback(callback)

if (this.isOpen()) {
this.db.close(function () {
Expand All @@ -153,7 +145,7 @@ LevelUP.prototype.close = function (callback) {
})
}

return promise
return callback.promise
}

LevelUP.prototype.isOpen = function () {
Expand All @@ -169,17 +161,13 @@ LevelUP.prototype.isClosed = function () {
}

LevelUP.prototype.get = function (key, options, callback) {
var promise

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

if (!callback) {
callback = promisify()
promise = callback.promise
if (maybeError(this, callback)) {
return callback.promise
}

if (maybeError(this, callback)) { return promise }

options = getOptions(options)

this.db.get(key, options, function (err, value) {
Expand All @@ -194,22 +182,19 @@ LevelUP.prototype.get = function (key, options, callback) {
callback(null, value)
})

return promise
return callback.promise
}

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

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

if (!callback) {
callback = promisify()
promise = callback.promise
if (maybeError(this, callback)) {
return callback.promise
}

if (maybeError(this, callback)) { return promise }

options = getOptions(options)

this.db.put(key, value, options, function (err) {
Expand All @@ -220,22 +205,19 @@ LevelUP.prototype.put = function (key, value, options, callback) {
callback()
})

return promise
return callback.promise
}

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

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

if (!callback) {
callback = promisify()
promise = callback.promise
if (maybeError(this, callback)) {
return callback.promise
}

if (maybeError(this, callback)) { return promise }

options = getOptions(options)

this.db.del(key, options, function (err) {
Expand All @@ -246,7 +228,7 @@ LevelUP.prototype.del = function (key, options, callback) {
callback()
})

return promise
return callback.promise
}

LevelUP.prototype.batch = function (arr, options, callback) {
Expand All @@ -255,17 +237,15 @@ LevelUP.prototype.batch = function (arr, options, callback) {
}

var self = this
var promise

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

if (!callback) {
callback = promisify()
promise = callback.promise
}
callback = catering.fromCallback(callback)

if (maybeError(this, callback)) { return promise }
if (maybeError(this, callback)) {
return callback.promise
}

options = getOptions(options)

Expand All @@ -277,7 +257,7 @@ LevelUP.prototype.batch = function (arr, options, callback) {
callback()
})

return promise
return callback.promise
}

LevelUP.prototype.iterator = function (options) {
Expand All @@ -286,18 +266,13 @@ LevelUP.prototype.iterator = function (options) {

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

callback = getCallback(options, callback)
options = getOptions(options)

if (!callback) {
callback = promisify()
promise = callback.promise
}
callback = catering.fromCallback(callback)

if (maybeError(this, callback)) {
return promise
return callback.promise
}

this.db.clear(options, function (err) {
Expand All @@ -308,7 +283,7 @@ LevelUP.prototype.clear = function (options, callback) {
callback()
})

return promise
return callback.promise
}

LevelUP.prototype.readStream =
Expand Down
13 changes: 0 additions & 13 deletions lib/promisify.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"prepublishOnly": "npm run dependency-check"
},
"dependencies": {
"catering": "~2.0.0",
"deferred-leveldown": "~5.3.0",
"level-errors": "~2.0.0",
"level-iterator-stream": "~4.0.0",
Expand Down

0 comments on commit 33b1318

Please sign in to comment.