Skip to content

Commit

Permalink
Use _nextTick() utility for browsers
Browse files Browse the repository at this point in the history
This abstract-leveldown utility is now backed by `queue-microtask`,
which is a smaller shim than `immediate`.
  • Loading branch information
vweevers committed Apr 10, 2021
1 parent 0942b07 commit ba525c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
1 change: 0 additions & 1 deletion immediate-browser.js

This file was deleted.

1 change: 0 additions & 1 deletion immediate.js

This file was deleted.

37 changes: 23 additions & 14 deletions memdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ const ltgt = require('ltgt')
const createRBT = require('functional-red-black-tree')
const { Buffer } = require('buffer')

// In Node, use global.setImmediate. In the browser, use a consistent
// microtask library to give consistent microtask experience to all browsers
const setImmediate = require('./immediate')
const NONE = Symbol('none')

// TODO (perf): replace ltgt.compare with a simpler, buffer-only comparator
Expand Down Expand Up @@ -91,13 +88,13 @@ MemIterator.prototype._next = function (callback) {
let key
let value

if (this._done++ >= this._limit) return setImmediate(callback)
if (!this._tree.valid) return setImmediate(callback)
if (this._done++ >= this._limit) return this._nextTick(callback)
if (!this._tree.valid) return this._nextTick(callback)

key = this._tree.key
value = this._tree.value

if (!this._test(key)) return setImmediate(callback)
if (!this._test(key)) return this._nextTick(callback)

if (!this.keyAsBuffer) {
key = key.toString()
Expand All @@ -109,7 +106,7 @@ MemIterator.prototype._next = function (callback) {

this._tree[this._incr]()

setImmediate(function callNext () {
this._nextTick(function callNext () {
callback(null, key, value)
})
}
Expand Down Expand Up @@ -170,7 +167,7 @@ function MemDOWN () {
inherits(MemDOWN, AbstractLevelDOWN)

MemDOWN.prototype._open = function (options, callback) {
setImmediate(() => {
this._nextTick(() => {
callback(null, this)
})
}
Expand All @@ -192,15 +189,15 @@ MemDOWN.prototype._put = function (key, value, options, callback) {
this._store = this._store.insert(key, value)
}

setImmediate(callback)
this._nextTick(callback)
}

MemDOWN.prototype._get = function (key, options, callback) {
let value = this._store.get(key)

if (typeof value === 'undefined') {
// 'NotFound' error, consistent with LevelDOWN API
return setImmediate(function callNext () {
return this._nextTick(function callNext () {
callback(new Error('NotFound'))
})
}
Expand All @@ -209,14 +206,14 @@ MemDOWN.prototype._get = function (key, options, callback) {
value = value.toString()
}

setImmediate(function callNext () {
this._nextTick(function callNext () {
callback(null, value)
})
}

MemDOWN.prototype._del = function (key, options, callback) {
this._store = this._store.remove(key)
setImmediate(callback)
this._nextTick(callback)
}

MemDOWN.prototype._batch = function (array, options, callback) {
Expand All @@ -240,8 +237,7 @@ MemDOWN.prototype._batch = function (array, options, callback) {
}

this._store = tree

setImmediate(callback)
this._nextTick(callback)
}

MemDOWN.prototype._iterator = function (options) {
Expand All @@ -252,3 +248,16 @@ module.exports = MemDOWN

// Exposed for unit tests only
module.exports.MemIterator = MemIterator

// Use setImmediate() in Node.js to allow IO in between our callbacks
if (typeof process !== 'undefined' && !process.browser && typeof global !== 'undefined' && typeof global.setImmediate === 'function') {
const setImmediate = global.setImmediate

MemDOWN.prototype._nextTick = MemIterator.prototype._nextTick = function (fn, ...args) {
if (args.length === 0) {
setImmediate(fn)
} else {
setImmediate(() => fn(...args))
}
}
}
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@
"test-browsers-local": "airtap --coverage -p local test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"hallmark": "hallmark --fix",
"dependency-check": "dependency-check . immediate*.js test.js",
"dependency-check": "dependency-check . test.js",
"prepublishOnly": "npm run dependency-check"
},
"files": [
"memdown.js",
"immediate.js",
"immediate-browser.js",
"UPGRADING.md",
"CHANGELOG.md",
"CONTRIBUTORS.md"
],
"browser": {
"./immediate.js": "./immediate-browser.js"
},
"dependencies": {
"abstract-leveldown": "^7.0.0",
"buffer": "^6.0.3",
"functional-red-black-tree": "^1.0.1",
"immediate": "^3.2.3",
"inherits": "^2.0.1",
"ltgt": "^2.2.0"
},
Expand Down

0 comments on commit ba525c4

Please sign in to comment.