From ba525c450f9ed0c3be7623e3284f52d5732b3055 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 10 Apr 2021 11:49:58 +0200 Subject: [PATCH] Use _nextTick() utility for browsers This abstract-leveldown utility is now backed by `queue-microtask`, which is a smaller shim than `immediate`. --- immediate-browser.js | 1 - immediate.js | 1 - memdown.js | 37 +++++++++++++++++++++++-------------- package.json | 8 +------- 4 files changed, 24 insertions(+), 23 deletions(-) delete mode 100644 immediate-browser.js delete mode 100644 immediate.js diff --git a/immediate-browser.js b/immediate-browser.js deleted file mode 100644 index 3e424be..0000000 --- a/immediate-browser.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('immediate') diff --git a/immediate.js b/immediate.js deleted file mode 100644 index f09f984..0000000 --- a/immediate.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = setImmediate diff --git a/memdown.js b/memdown.js index 7d9579f..9233a4f 100644 --- a/memdown.js +++ b/memdown.js @@ -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 @@ -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() @@ -109,7 +106,7 @@ MemIterator.prototype._next = function (callback) { this._tree[this._incr]() - setImmediate(function callNext () { + this._nextTick(function callNext () { callback(null, key, value) }) } @@ -170,7 +167,7 @@ function MemDOWN () { inherits(MemDOWN, AbstractLevelDOWN) MemDOWN.prototype._open = function (options, callback) { - setImmediate(() => { + this._nextTick(() => { callback(null, this) }) } @@ -192,7 +189,7 @@ 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) { @@ -200,7 +197,7 @@ MemDOWN.prototype._get = function (key, options, callback) { if (typeof value === 'undefined') { // 'NotFound' error, consistent with LevelDOWN API - return setImmediate(function callNext () { + return this._nextTick(function callNext () { callback(new Error('NotFound')) }) } @@ -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) { @@ -240,8 +237,7 @@ MemDOWN.prototype._batch = function (array, options, callback) { } this._store = tree - - setImmediate(callback) + this._nextTick(callback) } MemDOWN.prototype._iterator = function (options) { @@ -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)) + } + } +} diff --git a/package.json b/package.json index e9ac9f2..e5bbc31 100644 --- a/package.json +++ b/package.json @@ -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" },