diff --git a/README.md b/README.md index 43a00fc..d35879a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# directly +# dreckly ## Like Promise.all, only less so @@ -8,14 +8,14 @@ This module could more descriptively be named Promise.allButNotAllAtOnce. It tak Now supports throttling of potentially infinite queues of Promises (see notes on the `Queue` class below) ## About the name -In the West Country people will often promise to do things 'directly' `[drekt-lee]`, meaning they'll do it when they're good and ready, possibly never. Example usage: +In the West Country people will often promise to do things 'dreckly' `[drekt-lee]`, meaning they'll do it when they're good and ready, possibly never. Example usage: -> I'll wash the dishes directly, my lover +> I'll wash the dishes dreckly, my lover ## Usage ```js -const directly = require('directly'); +const dreckly = require('dreckly'); const urls = []; // a big array of urls const fetchers = urls.map(function (url) { return function () { @@ -24,7 +24,7 @@ const fetchers = urls.map(function (url) { }); -directly(10, fetchers) +dreckly(10, fetchers) .then(function (results) { // handle exactly as if it was a Promise.all() }); @@ -34,8 +34,8 @@ directly(10, fetchers) Can also be called as a constructor (in which case the `.run()` method should be used) ```js -const Directly = require('Directly'); -const throttledRequests = new Directly(10, fetchers) +const Dreckly = require('Dreckly'); +const throttledRequests = new Dreckly(10, fetchers) throttledRequests .run() @@ -43,21 +43,21 @@ throttledRequests // handle exactly as if it was a Promise.all() }) - // can be used to stop the directly instance prematurely + // can be used to stop the dreckly instance prematurely throttledRequests.terminate() ``` To handle an infinite queue of promises use the `Queue` class to wrap your array of functions ```js -fetchers = new directly.Queue(fetchers); -directly(10, fetchers) +fetchers = new dreckly.Queue(fetchers); +dreckly(10, fetchers) .catch(function (errorObject) { // You can handle any errors in here // The error object has 3 properties // error: The error thrown // nextError: A promise which will reject the next time an error is encountered - // terminate: A function to call which will terminate the directly instance + // terminate: A function to call which will terminate the dreckly instance }); // use push to add to the execution queue. Will work even if the queue has fallen idle diff --git a/bower.json b/bower.json index 541192e..bc6b8b2 100644 --- a/bower.json +++ b/bower.json @@ -1,8 +1,8 @@ { - "name": "directly", - "main": "directly.js", + "name": "dreckly", + "main": "dreckly.js", "version": "0.0.0", - "homepage": "https://github.com/wheresrhys/directly", + "homepage": "https://github.com/wheresrhys/dreckly", "authors": [ "Rhys Evans " ], diff --git a/directly.js b/dreckly.js similarity index 89% rename from directly.js rename to dreckly.js index 64b0eba..990d3c9 100644 --- a/directly.js +++ b/dreckly.js @@ -6,7 +6,7 @@ function getRemover (arr, target) { }; } -class Directly { +class Dreckly { constructor (concurrence, funcs) { this.results = []; this.concurrence = concurrence; @@ -14,14 +14,14 @@ class Directly { this.terminates = Array.isArray(this.funcs); this.cancelled = false; if (!Array.isArray(this.funcs)) { - this.funcs.attachDirectlyInstance(this); + this.funcs.attachDrecklyInstance(this); } this.competitors = []; } run () { if (typeof this.funcs[0] !== 'function') { - throw new TypeError('directly expects a list functions that return a Promise, not a list of Promises') + throw new TypeError('dreckly expects a list functions that return a Promise, not a list of Promises') } if (this.terminates) { if (this.funcs.length <= this.concurrence) { @@ -111,8 +111,8 @@ class Directly { } module.exports = function SmartConstructor (concurrence, funcs) { - const directly = new Directly(concurrence, funcs) - return (this instanceof SmartConstructor) ? directly : directly.run(); + const dreckly = new Dreckly(concurrence, funcs) + return (this instanceof SmartConstructor) ? dreckly : dreckly.run(); }; module.exports.Queue = require('./lib/queue'); diff --git a/lib/queue.js b/lib/queue.js index 9458b97..ac35938 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -5,13 +5,13 @@ class Queue { this.items = items || []; } - attachDirectlyInstance (directly) { - this.directly = directly; + attachDrecklyInstance (dreckly) { + this.dreckly = dreckly; } push (func) { this.items.push.apply(this.items, [].slice.call(arguments)); - this.directly.run(); + this.dreckly.run(); } shift () { diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..394c37e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,307 @@ +{ + "name": "dreckly", + "version": "2.0.5", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "assertion-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.2.tgz", + "integrity": "sha1-E8pRXYYgbaC6xm6DTdOX2HWBCUw=", + "dev": true + }, + "async": { + "version": "0.2.10", + "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", + "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=", + "dev": true + }, + "chai": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", + "integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=", + "dev": true, + "requires": { + "assertion-error": "1.0.2", + "deep-eql": "0.1.3", + "type-detect": "1.0.0" + } + }, + "colors": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/colors/-/colors-0.6.2.tgz", + "integrity": "sha1-JCP+ZnisDF2uiFLl0OW+CMmXq8w=", + "dev": true + }, + "commander": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz", + "integrity": "sha1-/UMOiJgy7DU7ms0d4hfBHLPu+HM=", + "dev": true + }, + "cycle": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz", + "integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI=", + "dev": true + }, + "debug": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz", + "integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=", + "dev": true, + "requires": { + "ms": "0.7.1" + } + }, + "deep-eql": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", + "integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=", + "dev": true, + "requires": { + "type-detect": "0.1.1" + }, + "dependencies": { + "type-detect": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz", + "integrity": "sha1-C6XsKohWQORw6k6FBZcZANrFiCI=", + "dev": true + } + } + }, + "denodeify": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/denodeify/-/denodeify-1.2.1.tgz", + "integrity": "sha1-OjYof1A05pnnV3kBBSwubJQlFjE=", + "dev": true + }, + "diff": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-1.4.0.tgz", + "integrity": "sha1-fyjS657nsVqX79ic5j3P2qPMur8=", + "dev": true + }, + "es6-promise": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-2.3.0.tgz", + "integrity": "sha1-lu258v2wGZWCKyY92KratnSBgbw=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz", + "integrity": "sha1-Tbwv5nTnGUnK8/smlc5/LcHZqNE=", + "dev": true + }, + "eyes": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz", + "integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A=", + "dev": true + }, + "glob": { + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz", + "integrity": "sha1-Spc/Y1uRkPcV0QmH1cAP0oFevj0=", + "dev": true, + "requires": { + "inherits": "2.0.3", + "minimatch": "0.3.0" + } + }, + "graceful-fs": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", + "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=", + "dev": true, + "optional": true + }, + "growl": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.9.2.tgz", + "integrity": "sha1-Dqd0NxXbjY3ixe3hd14bRayFwC8=", + "dev": true + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "isstream": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", + "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=", + "dev": true + }, + "jade": { + "version": "0.26.3", + "resolved": "https://registry.npmjs.org/jade/-/jade-0.26.3.tgz", + "integrity": "sha1-jxDXl32NefL2/4YqgbBRPMslaGw=", + "dev": true, + "requires": { + "commander": "0.6.1", + "mkdirp": "0.3.0" + }, + "dependencies": { + "commander": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz", + "integrity": "sha1-+mihT2qUXVTbvlDYzbMyDp47GgY=", + "dev": true + }, + "mkdirp": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz", + "integrity": "sha1-G79asbqCevI1dRQ0kEJkVfSB/h4=", + "dev": true + } + } + }, + "jsonfile": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz", + "integrity": "sha1-NzaitCi4e72gzIO1P6PWM6NcKug=", + "dev": true, + "requires": { + "graceful-fs": "4.1.11" + } + }, + "lru-cache": { + "version": "2.7.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz", + "integrity": "sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=", + "dev": true + }, + "minimatch": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz", + "integrity": "sha1-J12O2qxPG7MyZHIInnlJyDlGmd0=", + "dev": true, + "requires": { + "lru-cache": "2.7.3", + "sigmund": "1.0.1" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "2.5.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-2.5.3.tgz", + "integrity": "sha1-FhvlvetJZ3HrmzV0UFC2IrWu/Fg=", + "dev": true, + "requires": { + "commander": "2.3.0", + "debug": "2.2.0", + "diff": "1.4.0", + "escape-string-regexp": "1.0.2", + "glob": "3.2.11", + "growl": "1.9.2", + "jade": "0.26.3", + "mkdirp": "0.5.1", + "supports-color": "1.2.0", + "to-iso-string": "0.0.2" + } + }, + "ms": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz", + "integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=", + "dev": true + }, + "npm-prepublish": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/npm-prepublish/-/npm-prepublish-1.2.3.tgz", + "integrity": "sha1-fGwfVU9SGqfvPMdsotL5NWVn0Qw=", + "dev": true, + "requires": { + "denodeify": "1.2.1", + "es6-promise": "2.3.0", + "jsonfile": "2.4.0", + "minimist": "1.2.0", + "semver": "5.4.1", + "winston": "0.8.3" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", + "dev": true + } + } + }, + "pkginfo": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/pkginfo/-/pkginfo-0.3.1.tgz", + "integrity": "sha1-Wyn2qB9wcXFC4J52W76rl7T4HiE=", + "dev": true + }, + "semver": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.4.1.tgz", + "integrity": "sha512-WfG/X9+oATh81XtllIo/I8gOiY9EXRdv1cQdyykeXK17YcUW3EXUAi2To4pcH6nZtJPr7ZOpM5OMyWJZm+8Rsg==", + "dev": true + }, + "sigmund": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz", + "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=", + "dev": true + }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=", + "dev": true + }, + "supports-color": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz", + "integrity": "sha1-/x7R5hFp0Gs88tWI4YixjYhH4X4=", + "dev": true + }, + "to-iso-string": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/to-iso-string/-/to-iso-string-0.0.2.tgz", + "integrity": "sha1-TcGeZk38y+Jb2NtQiwDG2hWCVdE=", + "dev": true + }, + "type-detect": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz", + "integrity": "sha1-diIXzAbbJY7EiQihKY6LlRIejqI=", + "dev": true + }, + "winston": { + "version": "0.8.3", + "resolved": "https://registry.npmjs.org/winston/-/winston-0.8.3.tgz", + "integrity": "sha1-ZLar9M0Brcrv1QCTk7HY6L7BnbA=", + "dev": true, + "requires": { + "async": "0.2.10", + "colors": "0.6.2", + "cycle": "1.0.3", + "eyes": "0.1.8", + "isstream": "0.1.2", + "pkginfo": "0.3.1", + "stack-trace": "0.0.10" + } + } + } +} diff --git a/package.json b/package.json index a8543ad..8a85b5a 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { - "name": "directly", + "name": "dreckly", "version": "2.0.5", "description": "Like Promise.all, only less so", - "main": "directly.js", + "main": "dreckly.js", "scripts": { "test": "mocha" }, "repository": { "type": "git", - "url": "https://github.com/wheresrhys/directly.git" + "url": "https://github.com/wheresrhys/dreckly.git" }, "keywords": [ "promise", @@ -20,9 +20,9 @@ "author": "Rhys Evans", "license": "MIT", "bugs": { - "url": "https://github.com/wheresrhys/directly/issues" + "url": "https://github.com/wheresrhys/dreckly/issues" }, - "homepage": "https://github.com/wheresrhys/directly", + "homepage": "https://github.com/wheresrhys/dreckly", "devDependencies": { "chai": "^3.2.0", "es6-promise": "^2.3.0", diff --git a/test/directly.test.js b/test/dreckly.test.js similarity index 86% rename from test/directly.test.js rename to test/dreckly.test.js index a3f2e01..b001bb6 100644 --- a/test/directly.test.js +++ b/test/dreckly.test.js @@ -1,7 +1,7 @@ 'use strict'; var expect = require('chai').expect; -var Directly = require('../directly'); +var Dreckly = require('../dreckly'); var setupPromises = function (n) { @@ -49,7 +49,7 @@ describe('concordance with Promise.all', function () { it('should resolve if they all resolve', function (done) { var promises = setupPromises(2); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .then(function (res) { expect(res).to.eql([0, 1]); done(); @@ -64,7 +64,7 @@ describe('concordance with Promise.all', function () { it('should reject if any of them reject', function (done) { var promises = setupPromises(2); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .catch(function (res) { expect(res).to.equal('err1'); done(); @@ -79,7 +79,7 @@ describe('concordance with Promise.all', function () { it('should resolve if they all resolve', function (done) { var promises = setupPromises(3); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .then(function (res) { expect(res).to.eql([0, 1, 2]); done(); @@ -94,7 +94,7 @@ describe('concordance with Promise.all', function () { it('should reject if any of them reject', function (done) { var promises = setupPromises(3); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .catch(function (res) { expect(res).to.equal('err1'); done(); @@ -108,7 +108,7 @@ describe('concordance with Promise.all', function () { it('should resolve if they all resolve', function (done) { var promises = setupPromises(4); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .then(function (res) { expect(res).to.eql([0, 1, 2, 3]); done(); @@ -123,7 +123,7 @@ describe('concordance with Promise.all', function () { it('should reject if any of them reject', function (done) { var promises = setupPromises(4); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .catch(function (res) { expect(res).to.equal('err1'); done(); @@ -135,7 +135,7 @@ describe('concordance with Promise.all', function () { it('should preserve promise order in the results', function () { var promises = setupPromises(4); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() .then(function (res) { expect(res).to.eql([1,2,3,4]); done(); @@ -158,7 +158,7 @@ describe('throttling', function () { spyVal = true; }); - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() expect(spyVal).to.be.false; }); @@ -179,7 +179,7 @@ describe('throttling', function () { return func4(); } - new Directly(3, promises.functions).run() + new Dreckly(3, promises.functions).run() expect(spyVals).to.eql([]); @@ -208,7 +208,7 @@ describe('functional calling', function () { it('should be callable as a function', function (done) { var promises = setupPromises(3); - Directly(3, promises.functions) + Dreckly(3, promises.functions) .then(function (res) { expect(res).to.eql([0, 1, 2]); done(); @@ -225,14 +225,14 @@ describe('infinite queueing', function () { it('should call all on startup if throttle limit not reached', function (done) { const result = []; - const funcs = new Directly.Queue([1, 2].map(i => { + const funcs = new Dreckly.Queue([1, 2].map(i => { return () => { result.push(i); return Promise.resolve(i); } })) - new Directly(3, funcs).run() + new Dreckly(3, funcs).run() setTimeout(() => { expect(result).to.eql([1, 2]); done(); @@ -241,14 +241,14 @@ describe('infinite queueing', function () { it('should only call up to the throttle limit on startup', function (done) { const result = []; - const funcs = new Directly.Queue([1, 2, 3].map(i => { + const funcs = new Dreckly.Queue([1, 2, 3].map(i => { return () => { result.push(i); return new Promise(() => null); } })) - new Directly(2, funcs).run() + new Dreckly(2, funcs).run() setTimeout(() => { expect(result).to.eql([1, 2]); done(); @@ -257,9 +257,9 @@ describe('infinite queueing', function () { it('should seamlessly call any promise function added after startup', function (done) { const promises = setupPromises(3); - const funcs = new Directly.Queue(promises.functions); + const funcs = new Dreckly.Queue(promises.functions); - new Directly(3, funcs).run(); + new Dreckly(3, funcs).run(); let spyCalled = false; const spy = () => { spyCalled = true; @@ -280,13 +280,13 @@ describe('infinite queueing', function () { }); it('should restart if functions are pushed when it is idling', function (done) { - const funcs = new Directly.Queue([1, 2].map(i => { + const funcs = new Dreckly.Queue([1, 2].map(i => { return () => { return Promise.resolve(i); } })) - new Directly(3, funcs).run() + new Dreckly(3, funcs).run() let spyCalled = false; const spy = () => { spyCalled = true; @@ -301,13 +301,13 @@ describe('infinite queueing', function () { }); it('should still apply concurrence limits to functions added after startup', function (done) { - const funcs = new Directly.Queue([1, 2].map(i => { + const funcs = new Dreckly.Queue([1, 2].map(i => { return () => { return Promise.resolve(i); } })) - new Directly(3, funcs).run() + new Dreckly(3, funcs).run() let spyCalled = false; const spy = () => { spyCalled = true; @@ -325,11 +325,11 @@ describe('infinite queueing', function () { }); it('should provide a promisey interface to handle errors', function (done) { - const funcs = new Directly.Queue([() => { + const funcs = new Dreckly.Queue([() => { return Promise.reject('test error1'); }]); - var d = new Directly(3, funcs); + var d = new Dreckly(3, funcs); var p = d.run() .catch(err1 => {