From 6f1a7bb444a3a71b87c2b2a919f1f5cdd5253d19 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 11 May 2016 21:11:09 +0200 Subject: [PATCH] refactor: replace async with light weight alternatives --- package.json | 9 +++++---- src/index.js | 14 +++++++------- test/browser.js | 6 +++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 99f4700..a2af968 100644 --- a/package.json +++ b/package.json @@ -43,19 +43,20 @@ "chai": "^3.5.0", "fs-blob-store": "^5.2.1", "idb-plus-blob-store": "^1.1.2", + "ipfs-block": "^0.3.0", "ipfs-repo": "^0.7.1", "lodash": "^4.8.2", "ncp": "^2.0.0", "pre-commit": "^1.1.2", - "ipfs-block": "^0.3.0", - "rimraf": "^2.5.1" + "rimraf": "^2.5.1", + "run-series": "^1.1.4" }, "dependencies": { - "async": "^1.5.2" + "run-parallel-limit": "^1.0.3" }, "contributors": [ "David Dias ", "Friedel Ziegelmayer ", "Stephen Whitmore " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index f2d6ca5..0fcdcad 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ 'use strict' -const async = require('async') +const parallelLimit = require('run-parallel-limit') // BlockService is a hybrid block datastore. It stores data in a local // datastore and may retrieve data from a remote Exchange. @@ -41,9 +41,9 @@ module.exports = class BlockService { return callback(new Error('expects an array of Blocks')) } - async.eachLimit(blocks, 100, (block, next) => { + parallelLimit(blocks.map((block) => (next) => { this.addBlock(block, next) - }, callback) + }), 100, callback) } getBlock (key, extension, callback) { @@ -71,7 +71,7 @@ module.exports = class BlockService { var results = {} - async.eachLimit(multihashes, 100, (multihash, next) => { + parallelLimit(multihashes.map((multihash) => (next) => { this.getBlock(multihash, extension, (err, block) => { results[multihash] = { err: err, @@ -79,7 +79,7 @@ module.exports = class BlockService { } next() }) - }, (err) => { + }), 100, (err) => { callback(err, results) }) } @@ -98,9 +98,9 @@ module.exports = class BlockService { return callback(new Error('Invalid batch of multihashes')) } - async.eachLimit(multihashes, 100, (multihash, next) => { + parallelLimit(multihashes.map((multihash) => (next) => { this.deleteBlock(multihash, extension, next) - }, (err) => { + }), 100, (err) => { callback(err) }) } diff --git a/test/browser.js b/test/browser.js index d3b22d9..5343878 100644 --- a/test/browser.js +++ b/test/browser.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ 'use strict' -const async = require('async') +const series = require('run-series') const store = require('idb-plus-blob-store') const _ = require('lodash') const IPFSRepo = require('ipfs-repo') @@ -31,7 +31,7 @@ describe('IPFS Repo Tests on the Browser', function () { const mainBlob = store('ipfs') const blocksBlob = store('ipfs/blocks') - async.eachSeries(repoData, (file, cb) => { + series(repoData.map((file) => (cb) => { if (_.startsWith(file.key, 'datastore/')) { return cb() } @@ -44,7 +44,7 @@ describe('IPFS Repo Tests on the Browser', function () { blob.createWriteStream({ key: key }).end(file.value, cb) - }, done) + }), done) }) const repo = new IPFSRepo('ipfs', {stores: store})