* Processes an array of tasks using Web Workers.
*/
export class Pool {
- static #threads: Thread[] = []
static #cores: number = Math.max(1, navigator.hardwareConcurrency - 1)
static #queue: Job[] = []
+ static #threads: Thread[] = []
static #url: string = URL.createObjectURL(new Blob([Workers], { type: 'text/javascript' }))
+ static get threadsBusy (): number {
+ let n = 0
+ for (const thread of this.#threads) {
+ n += +(thread.job != null)
+ }
+ return n
+ }
+ static get threadsIdle (): number {
+ let n = 0
+ for (const thread of this.#threads) {
+ n += +(thread.job == null)
+ }
+ return n
+ }
+
static {
for (let i = this.#cores; i > 0; i--) {
const thread = {
thread.worker.postMessage({ name: job.name, buffer: job.data }, [job.data])
}
} else {
- const chunk: number = 1 + (job.data.length / this.#cores)
+ const chunk: number = 1 + (job.data.length / this.threadsIdle)
const next = job.data.slice(0, chunk)
job.data = job.data.slice(chunk)
if (job.data.length === 0) this.#queue.shift()