-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from wheresrhys/infinite
Infinite
- Loading branch information
Showing
5 changed files
with
296 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,118 @@ | ||
'use strict'; | ||
|
||
var Directly = function (concurrence, funcs) { | ||
function getRemover (arr, target) { | ||
return () => { | ||
arr.splice(arr.indexOf(target), 1); | ||
}; | ||
} | ||
|
||
if (!Promise) { | ||
throw 'Directly requires es6 Promises'; | ||
class Directly { | ||
constructor (concurrence, funcs) { | ||
if (!(this instanceof Directly)) { | ||
return new Directly(concurrence, funcs).run(); | ||
} | ||
this.results = []; | ||
this.concurrence = concurrence; | ||
this.funcs = funcs; | ||
this.terminates = Array.isArray(this.funcs); | ||
this.cancelled = false; | ||
if (!Array.isArray(this.funcs)) { | ||
this.funcs.attachDirectlyInstance(this); | ||
} | ||
this.competitors = []; | ||
} | ||
|
||
if (!(this instanceof Directly)) { | ||
return new Directly(concurrence, funcs).run(); | ||
} | ||
this.results = []; | ||
this.concurrence = concurrence; | ||
this.funcs = funcs; | ||
this.competitors = []; | ||
}; | ||
run () { | ||
if (this.terminates) { | ||
if (this.funcs.length <= this.concurrence) { | ||
return Promise.all(this.funcs.map(func => func())); | ||
} | ||
|
||
Directly.prototype.run = function () { | ||
while (this.concurrence - this.competitors.length) { | ||
this.executeOne(); | ||
} | ||
this.startRace(); | ||
|
||
if (this.funcs.length <= this.concurrence) { | ||
return Promise.all(this.funcs.map(function (func) { | ||
return func(); | ||
})); | ||
|
||
} else if (!this.running) { | ||
// never take the Promise.all shortcut as even if the initial list is short, it | ||
// could easily grow to exceed the concurrence limit. | ||
while (this.funcs.length && this.concurrence - this.competitors.length) { | ||
this.executeOne(); | ||
} | ||
this.startRace(); | ||
} | ||
this.running === true; | ||
if (!this.resolve) { | ||
return this.instancePromise(); | ||
} | ||
} | ||
|
||
while (this.concurrence--) { | ||
this.executeOne(); | ||
instancePromise () { | ||
return new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}); | ||
} | ||
this.startRace(); | ||
|
||
return new Promise(function (resolve, reject) { | ||
this.resolve = resolve; | ||
this.reject = reject; | ||
}.bind(this)); | ||
}; | ||
executeOne () { | ||
const promise = this.funcs.shift()(); | ||
|
||
Directly.prototype.executeOne = function () { | ||
var promise = this.funcs.shift()(); | ||
var competitors = this.competitors; | ||
this.results.push(promise); | ||
this.competitors.push(promise); | ||
const remove = getRemover(this.competitors, promise) | ||
promise.then(remove, remove); | ||
} | ||
|
||
this.results.push(promise); | ||
competitors.push(promise); | ||
startRace () { | ||
const race = this.race = Promise.race(this.competitors); | ||
|
||
promise.then(function () { | ||
competitors.splice(competitors.indexOf(promise), 1); | ||
}); | ||
}; | ||
race | ||
.then(() => { | ||
this.rejoinRace(race); | ||
}, err => { | ||
|
||
if (this.terminates) { | ||
this.reject(err); | ||
} else { | ||
// give the ability to handle future errors; | ||
const reject = this.reject; | ||
const nextPromise = this.instancePromise(); | ||
reject({ | ||
error: err, | ||
nextError: nextPromise, | ||
terminate: this.terminate.bind(this) | ||
}); | ||
this.rejoinRace(race); | ||
} | ||
}); | ||
} | ||
|
||
Directly.prototype.startRace = function () { | ||
Promise.race(this.competitors) | ||
.then(function (index) { | ||
rejoinRace (race) { | ||
if (this.race === race) { | ||
if (!this.funcs.length) { | ||
return this.resolve(Promise.all(this.results)); | ||
if (this.terminates) { | ||
return this.resolve(Promise.all(this.results)); | ||
} else { | ||
this.running = false; | ||
} | ||
} else if (!this.cancelled) { | ||
this.executeOne(); | ||
this.startRace(); | ||
} | ||
} | ||
} | ||
|
||
this.executeOne(); | ||
this.startRace(); | ||
terminate () { | ||
this.resolve(); | ||
this.cancelled = true; | ||
} | ||
|
||
} | ||
|
||
}.bind(this), function (err) { | ||
this.reject(err); | ||
}.bind(this)); | ||
module.exports = function SmartConstructor (concurrence, funcs) { | ||
const directly = new Directly(concurrence, funcs) | ||
return (this instanceof SmartConstructor) ? directly : directly.run(); | ||
}; | ||
|
||
module.exports = Directly; | ||
module.exports.Queue = require('./lib/queue'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
class Queue { | ||
constructor (items) { | ||
this.items = items || []; | ||
} | ||
|
||
attachDirectlyInstance (directly) { | ||
this.directly = directly; | ||
} | ||
|
||
push (func) { | ||
this.items.push.apply(this.items, [].slice.call(arguments)); | ||
this.directly.run(); | ||
} | ||
|
||
shift () { | ||
return this.items.shift(); | ||
} | ||
|
||
get length () { | ||
return this.items.length; | ||
} | ||
|
||
} | ||
|
||
module.exports = Queue; |
Oops, something went wrong.