Skip to content

Commit

Permalink
define default methods on prototype to please TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Feb 8, 2018
1 parent 38b3706 commit b1ed564
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 68 deletions.
28 changes: 14 additions & 14 deletions abstract-chained-batch.js
Expand Up @@ -29,42 +29,41 @@ AbstractChainedBatch.prototype.put = function (key, value) {
key = this._serializeKey(key)
value = this._serializeValue(value)

if (typeof this._put === 'function') {
this._put(key, value)
} else {
this._operations.push({ type: 'put', key: key, value: value })
}
this._put(key, value)

return this
}

AbstractChainedBatch.prototype._put = function (key, value) {
this._operations.push({ type: 'put', key: key, value: value })
}

AbstractChainedBatch.prototype.del = function (key) {
this._checkWritten()

var err = this._db._checkKey(key, 'key')
if (err) { throw err }

key = this._serializeKey(key)

if (typeof this._del === 'function') {
this._del(key)
} else {
this._operations.push({ type: 'del', key: key })
}
this._del(key)

return this
}

AbstractChainedBatch.prototype._del = function (key) {
this._operations.push({ type: 'del', key: key })
}

AbstractChainedBatch.prototype.clear = function () {
this._checkWritten()

this._operations = []

if (typeof this._clear === 'function') { this._clear() }
this._clear()

return this
}

AbstractChainedBatch.prototype._clear = function noop () {}

AbstractChainedBatch.prototype.write = function (options, callback) {
this._checkWritten()

Expand All @@ -76,6 +75,7 @@ AbstractChainedBatch.prototype.write = function (options, callback) {

this._written = true

// @ts-ignore
if (typeof this._write === 'function') { return this._write(callback) }

if (typeof this._db._batch === 'function') {
Expand Down
20 changes: 9 additions & 11 deletions abstract-iterator.js
Expand Up @@ -22,19 +22,16 @@ AbstractIterator.prototype.next = function (callback) {
}

self._nexting = true
if (typeof self._next === 'function') {
return self._next(function () {
self._nexting = false
callback.apply(null, arguments)
})
}

process.nextTick(function () {
self._next(function () {
self._nexting = false
callback()
callback.apply(null, arguments)
})
}

AbstractIterator.prototype._next = function (callback) {
process.nextTick(callback)
}

AbstractIterator.prototype.end = function (callback) {
if (typeof callback !== 'function') {
throw new Error('end() requires a callback argument')
Expand All @@ -45,9 +42,10 @@ AbstractIterator.prototype.end = function (callback) {
}

this._ended = true
this._end(callback)
}

if (typeof this._end === 'function') { return this._end(callback) }

AbstractIterator.prototype._end = function (callback) {
process.nextTick(callback)
}

Expand Down
84 changes: 41 additions & 43 deletions abstract-leveldown.js
Expand Up @@ -34,20 +34,19 @@ AbstractLevelDOWN.prototype.open = function (options, callback) {
options.createIfMissing = options.createIfMissing !== false
options.errorIfExists = !!options.errorIfExists

if (typeof this._open === 'function') {
this.status = 'opening'
this._open(options, function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'open'
callback()
})
} else {
this.status = 'open'
process.nextTick(callback)
}
this.status = 'opening'
this._open(options, function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'open'
callback()
})
}

AbstractLevelDOWN.prototype._open = function (options, callback) {
process.nextTick(callback)
}

AbstractLevelDOWN.prototype.close = function (callback) {
Expand All @@ -58,20 +57,19 @@ AbstractLevelDOWN.prototype.close = function (callback) {
throw new Error('close() requires a callback argument')
}

if (typeof this._close === 'function') {
this.status = 'closing'
this._close(function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'closed'
callback()
})
} else {
this.status = 'closed'
process.nextTick(callback)
}
this.status = 'closing'
this._close(function (err) {
if (err) {
self.status = oldStatus
return callback(err)
}
self.status = 'closed'
callback()
})
}

AbstractLevelDOWN.prototype._close = function (callback) {
process.nextTick(callback)
}

AbstractLevelDOWN.prototype.get = function (key, options, callback) {
Expand All @@ -90,10 +88,10 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) {

options.asBuffer = options.asBuffer !== false

if (typeof this._get === 'function') {
return this._get(key, options, callback)
}
this._get(key, options, callback)
}

AbstractLevelDOWN.prototype._get = function (key, options, callback) {
process.nextTick(function () { callback(new Error('NotFound')) })
}

Expand All @@ -112,10 +110,10 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {

if (typeof options !== 'object') { options = {} }

if (typeof this._put === 'function') {
return this._put(key, value, options, callback)
}
this._put(key, value, options, callback)
}

AbstractLevelDOWN.prototype._put = function (key, value, options, callback) {
process.nextTick(callback)
}

Expand All @@ -133,10 +131,10 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) {

if (typeof options !== 'object') { options = {} }

if (typeof this._del === 'function') {
return this._del(key, options, callback)
}
this._del(key, options, callback)
}

AbstractLevelDOWN.prototype._del = function (key, options, callback) {
process.nextTick(callback)
}

Expand Down Expand Up @@ -180,10 +178,10 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
serialized[i] = e
}

if (typeof this._batch === 'function') {
return this._batch(serialized, options, callback)
}
this._batch(serialized, options, callback)

This comment has been minimized.

Copy link
@ralphtheninja

ralphtheninja Feb 9, 2018

Member

@vweevers So it was these return-statements that broke it?

This comment has been minimized.

Copy link
@vweevers

vweevers Feb 9, 2018

Author Member

Yes. Could be that we broke more things, but they are not covered by tests.

}

AbstractLevelDOWN.prototype._batch = function (array, options, callback) {
process.nextTick(callback)
}

Expand Down Expand Up @@ -227,11 +225,11 @@ function isEmptyBuffer (v) {

AbstractLevelDOWN.prototype.iterator = function (options) {
if (typeof options !== 'object') { options = {} }

options = this._setupIteratorOptions(options)
return this._iterator(options)
}

if (typeof this._iterator === 'function') { return this._iterator(options) }

AbstractLevelDOWN.prototype._iterator = function (options) {
return new AbstractIterator(this)
}

Expand Down

0 comments on commit b1ed564

Please sign in to comment.