*/
export class Pool {
#approach: 'converge' | 'divide' = 'divide'
- // #cores: number = Math.max(1, navigator.hardwareConcurrency - 1)
- #cores: number = 1
+ #cores: number = Math.max(1, navigator.hardwareConcurrency - 1)
+ // #cores: number = 1
#queue: object[] = []
#resolve: Function = (value: unknown): void => { }
#results: object[] = []
}
constructor (fn: string) {
- if (typeof window === 'undefined' && typeof self === 'undefined') {
- this.#url = fn
- } else {
- this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' }))
- }
+ this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' }))
for (let i = this.#cores; i > 0; i--) {
const thread = {
isBusy: false,
- //@ts-expect-error
- worker: new Worker(this.#url, { type: 'module', eval: true })
+ worker: new Worker(this.#url, { type: 'module' })
}
thread.worker.addEventListener('message', message => {
const data = new TextDecoder().decode(message.data ?? message)
}
#assign (thread: Thread, next: any[]): void {
- if (next.length > 0) {
+ console.dir(thread.worker)
+ if (next?.length > 0) {
thread.isBusy = true
const buf = new TextEncoder().encode(JSON.stringify(next)).buffer
thread.worker.postMessage(buf, [buf])
}
dismiss (): void {
- if (typeof Window === 'undefined') {
- this.#threads.forEach(thread => thread.worker.terminate())
- } else {
- URL.revokeObjectURL(this.#url)
- }
+ URL.revokeObjectURL(this.#url)
}
}