From faf44745f2023158275f3a2139e73f8e225b579d Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Sun, 18 Aug 2019 17:32:20 +0100 Subject: [PATCH] fix(Safari): Add default hardware concurrency value when one is not set --- package.json | 2 +- .../src/lib/from-worker-pool.spec.ts | 30 +++++++++++++++++++ .../src/lib/from-worker-pool.ts | 8 +++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index b984802..819130d 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "report-coverage": "codecov", "------------------ QUICK COMMANDS ------------------": "", "lint:fix": "yarn demo:lint:fix && yarn prettier:write", - "test": "yarn lib:test:watch", + "test": "yarn lib:test:watch --code-coverage", "start": "yarn demo:start", "commit": "git add . && git-cz" }, diff --git a/projects/observable-webworker/src/lib/from-worker-pool.spec.ts b/projects/observable-webworker/src/lib/from-worker-pool.spec.ts index 9e6f169..5d6945c 100644 --- a/projects/observable-webworker/src/lib/from-worker-pool.spec.ts +++ b/projects/observable-webworker/src/lib/from-worker-pool.spec.ts @@ -205,6 +205,36 @@ describe('fromWorkerPool', () => { }); }); + describe('with undefined navigator.hardwareConcurrency', () => { + it('runs a default fallback number of workers', () => { + spyOnProperty(window.navigator, 'hardwareConcurrency').and.returnValue(undefined); + + const input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + + const testWorkerStream$ = fromWorkerPool(workerFactorySpy, input); + const subscriptionSpy = jasmine.createSpy('subscriptionSpy'); + const sub = testWorkerStream$.subscribe(subscriptionSpy); + + expect(workerFactorySpy).toHaveBeenCalledTimes(3); + + sub.unsubscribe(); + }); + + it('runs a configured fallback number of workers', () => { + spyOnProperty(window.navigator, 'hardwareConcurrency').and.returnValue(undefined); + + const input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + + const testWorkerStream$ = fromWorkerPool(workerFactorySpy, input, { fallbackWorkerCount: 2 }); + const subscriptionSpy = jasmine.createSpy('subscriptionSpy'); + const sub = testWorkerStream$.subscribe(subscriptionSpy); + + expect(workerFactorySpy).toHaveBeenCalledTimes(2); + + sub.unsubscribe(); + }); + }); + describe('output strategy', () => { it('[default] outputs results as they are available', fakeAsync(() => { const workerCount = 7; diff --git a/projects/observable-webworker/src/lib/from-worker-pool.ts b/projects/observable-webworker/src/lib/from-worker-pool.ts index dc06cc6..2a58a72 100644 --- a/projects/observable-webworker/src/lib/from-worker-pool.ts +++ b/projects/observable-webworker/src/lib/from-worker-pool.ts @@ -12,6 +12,7 @@ interface LazyWorker { export interface WorkerPoolOptions { workerCount?: number; + fallbackWorkerCount?: number; flattenOperator?: (input$: Observable>) => Observable; selectTransferables?: (input: I) => Transferable[]; } @@ -24,7 +25,8 @@ export function fromWorkerPool( const { // tslint:disable-next-line:no-unnecessary-initializer selectTransferables = undefined, - workerCount = navigator.hardwareConcurrency - 1, + workerCount = navigator.hardwareConcurrency ? navigator.hardwareConcurrency - 1 : null, + fallbackWorkerCount = 3, flattenOperator = mergeAll(), } = options || {}; @@ -35,7 +37,9 @@ export function fromWorkerPool( let sent = 0; let finished = false; - const lazyWorkers: LazyWorker[] = Array.from({ length: workerCount }).map((_, index) => { + const lazyWorkers: LazyWorker[] = Array.from({ + length: workerCount !== null ? workerCount : fallbackWorkerCount, + }).map((_, index) => { return { _cachedWorker: null, factory() {