Created
July 13, 2018 03:27
-
-
Save nedarb/86ad66eb53ff0e9159c6beaa4ab42c5c to your computer and use it in GitHub Desktop.
JS Bin ConcurrencyManager with takeANumber // source http://jsbin.com/zozoxac
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="ConcurrencyManager with takeANumber"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* jshint esnext: true */ | |
var stopwatch = new Stopwatch(); | |
function Stopwatch() { | |
this.start = performance.now(); | |
} | |
Stopwatch.prototype.duration = function getDuration() { | |
return performance.now() - this.start; | |
}; | |
class ConcurrencyManager { | |
constructor(maxConcurrent) { | |
this.maxConcurrent = maxConcurrent; | |
this.runningWork = []; | |
this.workQueue = []; | |
} | |
enqueue(generator) { | |
return new Promise(function (resolve, reject) { | |
this.workQueue.push({ resolve: resolve, reject: reject, generator: generator }); | |
if (this.runningWork.length < this.maxConcurrent) { | |
this.__startNewWork(); | |
} | |
}.bind(this)); | |
} | |
__onWorkItemFinished(workItem) { | |
var index = this.runningWork.indexOf(workItem); | |
// remove the workItem from the running work array | |
this.runningWork.splice(index, 1); | |
if (this.workQueue.length > 0) { | |
this.__startNewWork(); | |
} | |
} | |
__startNewWork() { | |
// take the first entry from the front of the work queue array | |
var entry = this.workQueue.splice(0, 1)[0]; | |
var generator = entry.generator; | |
var resolve = entry.resolve, reject = entry.reject; | |
// create the promise that resolves/rejects it's enqueue promise | |
var promise = generator().then(resolve, reject); | |
this.runningWork.push(promise); | |
// once the promise finishes, trigger this work being done | |
promise.then(this.__onWorkItemFinished.bind(this, promise)); | |
} | |
} | |
function delay(timeInMs, val) { | |
LOG('Starting ', val); | |
return new Promise(resolve=>setTimeout(resolve, timeInMs, val)); | |
} | |
function delayReject(timeInMs, val) { | |
LOG('Starting ', val); | |
return new Promise((resolve, reject)=> setTimeout(reject, timeInMs, val)); | |
} | |
function LOG() { | |
var time = stopwatch.duration(); | |
var args = Array.from(arguments); | |
args.splice(0,0, Math.round(time) + 'ms\t'); | |
console.log.apply(console, args); | |
} | |
LOG('START'); | |
var queue = new ConcurrencyManager(3); | |
queue.enqueue(()=>delay(1500, ['first', 123])).then(LOG); | |
queue.enqueue(()=>delay(600, ['second', 'abc'])).then(LOG); | |
queue.enqueue(()=>delay(700, ['third', 456])).then(LOG); | |
queue.enqueue(()=>delayReject(1200, ['fourth', 789])).then(LOG, LOG); | |
queue.enqueue(()=>delay(2000, ['fifth', 246])).then(LOG); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* jshint esnext: true */ | |
var stopwatch = new Stopwatch(); | |
function Stopwatch() { | |
this.start = performance.now(); | |
} | |
Stopwatch.prototype.duration = function getDuration() { | |
return performance.now() - this.start; | |
}; | |
class ConcurrencyManager { | |
constructor(maxConcurrent) { | |
this.maxConcurrent = maxConcurrent; | |
this.runningWork = []; | |
this.workQueue = []; | |
} | |
enqueue(generator) { | |
return new Promise(function (resolve, reject) { | |
this.workQueue.push({ resolve: resolve, reject: reject, generator: generator }); | |
if (this.runningWork.length < this.maxConcurrent) { | |
this.__startNewWork(); | |
} | |
}.bind(this)); | |
} | |
__onWorkItemFinished(workItem) { | |
var index = this.runningWork.indexOf(workItem); | |
// remove the workItem from the running work array | |
this.runningWork.splice(index, 1); | |
if (this.workQueue.length > 0) { | |
this.__startNewWork(); | |
} | |
} | |
__startNewWork() { | |
// take the first entry from the front of the work queue array | |
var entry = this.workQueue.splice(0, 1)[0]; | |
var generator = entry.generator; | |
var resolve = entry.resolve, reject = entry.reject; | |
// create the promise that resolves/rejects it's enqueue promise | |
var promise = generator().then(resolve, reject); | |
this.runningWork.push(promise); | |
// once the promise finishes, trigger this work being done | |
promise.then(this.__onWorkItemFinished.bind(this, promise)); | |
} | |
} | |
function delay(timeInMs, val) { | |
LOG('Starting ', val); | |
return new Promise(resolve=>setTimeout(resolve, timeInMs, val)); | |
} | |
function delayReject(timeInMs, val) { | |
LOG('Starting ', val); | |
return new Promise((resolve, reject)=> setTimeout(reject, timeInMs, val)); | |
} | |
function LOG() { | |
var time = stopwatch.duration(); | |
var args = Array.from(arguments); | |
args.splice(0,0, Math.round(time) + 'ms\t'); | |
console.log.apply(console, args); | |
} | |
LOG('START'); | |
var queue = new ConcurrencyManager(3); | |
queue.enqueue(()=>delay(1500, ['first', 123])).then(LOG); | |
queue.enqueue(()=>delay(600, ['second', 'abc'])).then(LOG); | |
queue.enqueue(()=>delay(700, ['third', 456])).then(LOG); | |
queue.enqueue(()=>delayReject(1200, ['fourth', 789])).then(LOG, LOG); | |
queue.enqueue(()=>delay(2000, ['fifth', 246])).then(LOG); | |
</script></body> | |
</html> |
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
/* jshint esnext: true */ | |
var stopwatch = new Stopwatch(); | |
function Stopwatch() { | |
this.start = performance.now(); | |
} | |
Stopwatch.prototype.duration = function getDuration() { | |
return performance.now() - this.start; | |
}; | |
class ConcurrencyManager { | |
constructor(maxConcurrent) { | |
this.maxConcurrent = maxConcurrent; | |
this.runningWork = []; | |
this.workQueue = []; | |
} | |
enqueue(generator) { | |
return new Promise(function (resolve, reject) { | |
this.workQueue.push({ resolve: resolve, reject: reject, generator: generator }); | |
if (this.runningWork.length < this.maxConcurrent) { | |
this.__startNewWork(); | |
} | |
}.bind(this)); | |
} | |
__onWorkItemFinished(workItem) { | |
var index = this.runningWork.indexOf(workItem); | |
// remove the workItem from the running work array | |
this.runningWork.splice(index, 1); | |
if (this.workQueue.length > 0) { | |
this.__startNewWork(); | |
} | |
} | |
__startNewWork() { | |
// take the first entry from the front of the work queue array | |
var entry = this.workQueue.splice(0, 1)[0]; | |
var generator = entry.generator; | |
var resolve = entry.resolve, reject = entry.reject; | |
// create the promise that resolves/rejects it's enqueue promise | |
var promise = generator().then(resolve, reject); | |
this.runningWork.push(promise); | |
// once the promise finishes, trigger this work being done | |
promise.then(this.__onWorkItemFinished.bind(this, promise)); | |
} | |
} | |
function delay(timeInMs, val) { | |
LOG('Starting ', val); | |
return new Promise(resolve=>setTimeout(resolve, timeInMs, val)); | |
} | |
function delayReject(timeInMs, val) { | |
LOG('Starting ', val); | |
return new Promise((resolve, reject)=> setTimeout(reject, timeInMs, val)); | |
} | |
function LOG() { | |
var time = stopwatch.duration(); | |
var args = Array.from(arguments); | |
args.splice(0,0, Math.round(time) + 'ms\t'); | |
console.log.apply(console, args); | |
} | |
LOG('START'); | |
var queue = new ConcurrencyManager(3); | |
queue.enqueue(()=>delay(1500, ['first', 123])).then(LOG); | |
queue.enqueue(()=>delay(600, ['second', 'abc'])).then(LOG); | |
queue.enqueue(()=>delay(700, ['third', 456])).then(LOG); | |
queue.enqueue(()=>delayReject(1200, ['fourth', 789])).then(LOG, LOG); | |
queue.enqueue(()=>delay(2000, ['fifth', 246])).then(LOG); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment