Created
September 25, 2022 12:23
-
-
Save webprogramozo/cf86b247c5cd39a667123f1f1090d865 to your computer and use it in GitHub Desktop.
Some async functionality with a class-based approach
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
const Asyncer = (function(){ | |
function asyncWorkerRunner(worker){ | |
if(worker.result.isWaiting()){ | |
worker.result.setPending(); | |
try{ | |
worker.workerFunction(function(resultData){ | |
worker.result.setSuccessResult(resultData); | |
}, ); | |
}catch(exceptionData){ | |
worker.result.setErrorResult(exceptionData); | |
} | |
} | |
} | |
function asyncResultDispenser(result){ | |
if(result.isFinished()){ | |
const resultReceiverFunctions = result.resultReceiverFunctions; | |
result.resultReceiverFunctions = []; | |
resultReceiverFunctions.forEach(function(resultReceiverFunction){ | |
try{resultReceiverFunction(result.result, result.worker);}catch(d){} | |
}); | |
} | |
} | |
class AsyncResult{ | |
resultReceiverFunctions = []; | |
constructor(worker){ | |
this.worker = worker; | |
this.result = null; | |
this.status = 'waiting'; | |
} | |
getStatus(){ | |
return this.status; | |
} | |
setPending(){ | |
this.status = 'pending'; | |
} | |
setSuccessResult(result){ | |
this.status = 'success'; | |
this.result = result; | |
this.dispenseResult(); | |
} | |
setErrorResult(result){ | |
this.status = 'error'; | |
this.result = result; | |
this.dispenseResult(); | |
} | |
isWaiting(){ | |
return this.status === 'waiting'; | |
} | |
isPending(){ | |
return this.status === 'pending'; | |
} | |
isSuccess(){ | |
return this.status === 'success'; | |
} | |
isError(){ | |
return this.status === 'error'; | |
} | |
isFinished(){ | |
return this.isSuccess() || this.isError(); | |
} | |
getResult(resultReceiverFunction){ | |
this.resultReceiverFunctions.push(resultReceiverFunction); | |
this.dispenseResult(); | |
return this; | |
} | |
dispenseResult(){ | |
asyncResultDispenser(this); | |
} | |
} | |
class AsyncWorker{ | |
workerFunction = null; | |
constructor(workerFunction){ | |
if(typeof workerFunction === 'function'){this.workerFunction = workerFunction;} | |
this.result = new AsyncResult(this); | |
} | |
run(){asyncWorkerRunner(this);return this;} | |
get(resultReceiverFunction){this.run();this.result.getResult(resultReceiverFunction);return this;} | |
} | |
class AsyncResultProcessor{ | |
constructor(workerArray){ | |
this.workerArray = workerArray; | |
} | |
all(multipleResultsReceiverFunction){ | |
const workerCount = this.workerArray.length; | |
let workerFinished = 0; | |
let workerResults = []; | |
for(let i = 0; i < workerCount; i++){ | |
workerResults[i] = null; | |
(function(worker, i){ | |
worker.get(function(result){ | |
workerResults[i] = result; | |
workerFinished++; | |
if(workerFinished === workerCount){ | |
multipleResultsReceiverFunction(...workerResults); | |
} | |
}); | |
})(this.workerArray[i], i); | |
} | |
} | |
} | |
AsyncResultProcessor.create = function(){ | |
return new AsyncResultProcessor([...arguments]); | |
}; | |
return { | |
Result: AsyncResult, | |
Worker: AsyncWorker, | |
Processor: AsyncResultProcessor, | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment