Skip to content

Commit

Permalink
optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 25, 2015
1 parent 499701c commit 6225480
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
50 changes: 22 additions & 28 deletions directly.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,64 @@
'use strict';

require('es6-promise').polyfill();

var Directly = function (concurrence, funcs) {

if (!Promise) {
throw 'Directly requires es6 Promises';
}

if (!(this instanceof Directly)) {
return new Directly(concurrence, funcs).run();
}
this.results = [];
this.concurrence = concurrence;
this.funcs = funcs;
this.counter = 0;
this.competitors = [];
};

Directly.prototype.run = function () {

if (this.funcs.length <= this.concurrence) {
return Promise.all(this.funcs.map(function (func) {
return func();
}));
}
this.competitors = [];
this.promise = new Promise(function (resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}.bind(this));

var i = this.concurrence;

while (i--) {
while (this.concurrence--) {
this.executeOne();
}
this.startRace();

return this.promise;
return new Promise(function (resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}.bind(this));
};

Directly.prototype.executeOne = function () {
var promise = this.funcs.shift()();
var competitors = this.competitors;

this.results.push(promise);
var index = this.counter++;
promise = promise.then(function () {
return index;
competitors.push(promise);

promise.then(function () {
competitors.splice(competitors.indexOf(promise), 1);
});
promise.__index = index;
this.competitors.push(promise);
};

Directly.prototype.startRace = function () {
Promise.race(this.competitors)
.then(function (index) {
if (!this.funcs.length) {
Promise.all(this.results)
.then(function (results) {
this.resolve(results);
}.bind(this));
} else {
this.competitors = this.competitors.filter(function (promise) {
return promise.__index !== index;
});
this.executeOne();
this.startRace();
return this.resolve(Promise.all(this.results));
}

this.executeOne();
this.startRace();

}.bind(this), function (err) {
this.reject(err);
}.bind(this));
};


module.exports = Directly;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "directly",
"version": "1.0.2",
"version": "1.0.3",
"description": "Like Promise.all, only less so",
"main": "directly.js",
"scripts": {
Expand All @@ -24,10 +24,11 @@
},
"homepage": "https://github.com/wheresrhys/directly",
"devDependencies": {
"es6-promise": "^2.3.0",
"chai": "^3.2.0",
"mocha": "^2.2.5"
},
"dependencies": {
"es6-promise": "^2.3.0"

}
}
2 changes: 2 additions & 0 deletions test/directly.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('es6-promise').polyfill();

var expect = require('chai').expect;
var Directly = require('../directly');

Expand Down

0 comments on commit 6225480

Please sign in to comment.