-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Worker Pool): Creates new
fromWorkerPool
method to automatically
manage pools of workers for parallel processing
- Loading branch information
1 parent
b090625
commit f0a274a
Showing
8 changed files
with
200 additions
and
42 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { EMPTY, Observable, of, Subject } from 'rxjs'; | ||
import { finalize, mergeMap } from 'rxjs/operators'; | ||
import { fromWorker } from './from-worker'; | ||
|
||
export function fromWorkerPool<I, O>( | ||
workerConstructor: (index: number) => Worker, | ||
workUnitIterator: IterableIterator<I> | Array<I>, | ||
selectTransferables?: (input: I) => Transferable[], | ||
workerCount: number = navigator.hardwareConcurrency - 1, | ||
): Observable<O> { | ||
|
||
const iterator = Array.isArray(workUnitIterator) ? workUnitIterator[Symbol.iterator]() : workUnitIterator; | ||
|
||
return new Observable<O>(resultObserver => { | ||
|
||
const idleWorker$$: Subject<Worker> = new Subject(); | ||
|
||
let completed = 0; | ||
let sent = 0; | ||
let finished = false; | ||
|
||
const workers: Worker[] = Array.from({ length: workerCount }).map((_, i) => workerConstructor(i)); | ||
|
||
const processor$ = idleWorker$$.pipe( | ||
mergeMap( | ||
(worker): Observable<O> => { | ||
|
||
const next = iterator.next(); | ||
|
||
if (next.done) { | ||
idleWorker$$.complete(); | ||
finished = true; | ||
return EMPTY; | ||
} | ||
|
||
sent++; | ||
const unitWork: I = next.value; | ||
|
||
return fromWorker<I, O>(() => worker, of(unitWork), selectTransferables, { terminateOnComplete: false }).pipe( | ||
finalize(() => { | ||
completed ++; | ||
|
||
if (!idleWorker$$.closed) { | ||
idleWorker$$.next(worker); | ||
} | ||
|
||
if (finished && completed === sent){ | ||
resultObserver.complete(); | ||
} | ||
|
||
}) | ||
); | ||
}, | ||
), | ||
); | ||
|
||
const sub = processor$.subscribe({ | ||
next: (o) => resultObserver.next(o), | ||
error: (e) => resultObserver.error(e), | ||
}); | ||
|
||
workers.forEach(w => idleWorker$$.next(w)); | ||
|
||
return () => { | ||
workers.forEach(w => w.terminate()); | ||
sub.unsubscribe(); | ||
} | ||
|
||
}); | ||
} |
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
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
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
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
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
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
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