Created
June 2, 2020 20:38
-
-
Save Deepak13245/48e9d4cfd3af96d9db11921822ae7a16 to your computer and use it in GitHub Desktop.
Woker channel for parallel processing of async function
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
class Channel { | |
_results = []; | |
_index = 0; | |
constructor(list = []) { | |
this._list = list; | |
} | |
push(...items) { | |
this._list.push(...items); | |
} | |
clear() { | |
this._results = []; | |
this._index = 0; | |
this._list = []; | |
} | |
get results() { | |
return this._results; | |
} | |
next() { | |
if(this._list.length === 0 ) { | |
return [false]; | |
} | |
const item = this._list.shift(); | |
const cb = this._callback(this._index) | |
return [true, [ item, this._index++ ], cb] | |
} | |
_callback(index) { | |
return result => { | |
this._results[index] = result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment