Skip to content

Commit

Permalink
Add buffer and immediate for browsers (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored Apr 3, 2020
1 parent f340d98 commit 60ec6cd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
11 changes: 6 additions & 5 deletions abstract-iterator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var nextTick = require('./next-tick')
function AbstractIterator (db) {
if (typeof db !== 'object' || db === null) {
throw new TypeError('First argument must be an abstract-leveldown compliant store')
Expand All @@ -16,12 +17,12 @@ AbstractIterator.prototype.next = function (callback) {
}

if (self._ended) {
process.nextTick(callback, new Error('cannot call next() after end()'))
nextTick(callback, new Error('cannot call next() after end()'))
return self
}

if (self._nexting) {
process.nextTick(callback, new Error('cannot call next() before previous next() has completed'))
nextTick(callback, new Error('cannot call next() before previous next() has completed'))
return self
}

Expand All @@ -35,7 +36,7 @@ AbstractIterator.prototype.next = function (callback) {
}

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

AbstractIterator.prototype.seek = function (target) {
Expand All @@ -58,15 +59,15 @@ AbstractIterator.prototype.end = function (callback) {
}

if (this._ended) {
return process.nextTick(callback, new Error('end() already called on iterator'))
return nextTick(callback, new Error('end() already called on iterator'))
}

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

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

module.exports = AbstractIterator
31 changes: 16 additions & 15 deletions abstract-leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var supports = require('level-supports')
var Buffer = require('buffer').Buffer
var AbstractIterator = require('./abstract-iterator')
var AbstractChainedBatch = require('./abstract-chained-batch')
var nextTick = require('./next-tick')
var hasOwnProperty = Object.prototype.hasOwnProperty
var rangeOptions = 'start end gt gte lt lte'.split(' ')

Expand Down Expand Up @@ -42,7 +43,7 @@ AbstractLevelDOWN.prototype.open = function (options, callback) {
}

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

AbstractLevelDOWN.prototype.close = function (callback) {
Expand All @@ -65,7 +66,7 @@ AbstractLevelDOWN.prototype.close = function (callback) {
}

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

AbstractLevelDOWN.prototype.get = function (key, options, callback) {
Expand All @@ -76,7 +77,7 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) {
}

var err = this._checkKey(key)
if (err) return process.nextTick(callback, err)
if (err) return nextTick(callback, err)

key = this._serializeKey(key)

Expand All @@ -88,7 +89,7 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) {
}

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

AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {
Expand All @@ -99,7 +100,7 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {
}

var err = this._checkKey(key) || this._checkValue(value)
if (err) return process.nextTick(callback, err)
if (err) return nextTick(callback, err)

key = this._serializeKey(key)
value = this._serializeValue(value)
Expand All @@ -110,7 +111,7 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {
}

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

AbstractLevelDOWN.prototype.del = function (key, options, callback) {
Expand All @@ -121,7 +122,7 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) {
}

var err = this._checkKey(key)
if (err) return process.nextTick(callback, err)
if (err) return nextTick(callback, err)

key = this._serializeKey(key)

Expand All @@ -131,7 +132,7 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) {
}

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

AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
Expand All @@ -146,11 +147,11 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
}

if (!Array.isArray(array)) {
return process.nextTick(callback, new Error('batch(array) requires an array argument'))
return nextTick(callback, new Error('batch(array) requires an array argument'))
}

if (array.length === 0) {
return process.nextTick(callback)
return nextTick(callback)
}

if (typeof options !== 'object' || options === null) options = {}
Expand All @@ -159,23 +160,23 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {

for (var i = 0; i < array.length; i++) {
if (typeof array[i] !== 'object' || array[i] === null) {
return process.nextTick(callback, new Error('batch(array) element must be an object and not `null`'))
return nextTick(callback, new Error('batch(array) element must be an object and not `null`'))
}

var e = xtend(array[i])

if (e.type !== 'put' && e.type !== 'del') {
return process.nextTick(callback, new Error("`type` must be 'put' or 'del'"))
return nextTick(callback, new Error("`type` must be 'put' or 'del'"))
}

var err = this._checkKey(e.key)
if (err) return process.nextTick(callback, err)
if (err) return nextTick(callback, err)

e.key = this._serializeKey(e.key)

if (e.type === 'put') {
var valueErr = this._checkValue(e.value)
if (valueErr) return process.nextTick(callback, valueErr)
if (valueErr) return nextTick(callback, valueErr)

e.value = this._serializeValue(e.value)
}
Expand All @@ -187,7 +188,7 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) {
}

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

AbstractLevelDOWN.prototype.clear = function (options, callback) {
Expand Down
1 change: 1 addition & 0 deletions next-tick.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('immediate')
1 change: 1 addition & 0 deletions next-tick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = process.nextTick
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "An abstract prototype matching the LevelDOWN API",
"license": "MIT",
"main": "index.js",
"browser": {
"./next-tick.js": "./next-tick.browser.js"
},
"scripts": {
"test": "standard && hallmark && nyc node test/self.js",
"test-browsers": "airtap --coverage --loopback airtap.local test/self.js",
Expand All @@ -14,6 +17,8 @@
"prepublishOnly": "npm run dependency-check"
},
"dependencies": {
"buffer": "^5.5.0",
"immediate": "^3.2.3",
"level-concat-iterator": "~2.0.0",
"level-supports": "~1.0.0",
"xtend": "~4.0.0"
Expand Down

0 comments on commit 60ec6cd

Please sign in to comment.