Skip to content

Commit f57d27c

Browse files
committed
fix(Worker pool): prevent sending completion notification to workers in the pool context to keep the workers alive
1 parent 69df17d commit f57d27c

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

projects/observable-webworker/src/lib/from-worker-pool.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ describe('fromWorkerPool', () => {
6060
sub.unsubscribe();
6161
});
6262

63+
it('does not send input close notification to ensure the workers are kept alive', () => {
64+
const subscriptionSpy = jasmine.createSpy('subscriptionSpy');
65+
const sub = stubbedWorkerStream.subscribe(subscriptionSpy);
66+
67+
input$.next(1);
68+
69+
expect(stubbedWorkers[0].postMessage).not.toHaveBeenCalledWith(jasmine.objectContaining({ kind: 'C' }));
70+
71+
sub.unsubscribe();
72+
});
73+
6374
it('shuts down workers when subscriber unsubscribes', () => {
6475
const subscriptionSpy = jasmine.createSpy('subscriptionSpy');
6576
const sub = stubbedWorkerStream.subscribe(subscriptionSpy);

projects/observable-webworker/src/lib/from-worker-pool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Observable, ObservableInput, of, Subject, zip } from 'rxjs';
1+
import { concat, NEVER, Observable, ObservableInput, of, Subject, zip } from 'rxjs';
22
import { finalize, map, mergeAll, tap } from 'rxjs/operators';
33
import { fromWorker } from './from-worker';
44

@@ -69,7 +69,7 @@ export function fromWorkerPool<I, O>(
6969
}),
7070
map(
7171
([worker, unitWork]): Observable<O> => {
72-
return fromWorker<I, O>(() => worker.factory(), of(unitWork), selectTransferables, {
72+
return fromWorker<I, O>(() => worker.factory(), concat(of(unitWork), NEVER), selectTransferables, {
7373
terminateOnComplete: false,
7474
}).pipe(
7575
finalize(() => {

projects/observable-webworker/src/lib/run-worker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export function getWorkerResult<I, O>(
3030
dematerialize(),
3131
);
3232

33-
return workerIsUnitType(worker)
34-
? input$.pipe(concatMap(input => from(worker.workUnit(input)).pipe(materialize())))
35-
: worker.work(input$).pipe(materialize());
33+
return (workerIsUnitType(worker)
34+
? input$.pipe(concatMap(input => from(worker.workUnit(input))))
35+
: worker.work(input$)
36+
).pipe(materialize());
3637
}
3738

3839
export function runWorker<I, O>(workerConstructor: ObservableWorkerConstructor<I, O>): Subscription {

0 commit comments

Comments
 (0)