Skip to content

Commit

Permalink
Merge pull request #33 from cloudnc/fix/use-default-hardware-concurrency
Browse files Browse the repository at this point in the history
fix(Safari): Add default hardware concurrency value when one is not set
  • Loading branch information
zakhenry authored Aug 18, 2019
2 parents a1426c7 + faf4474 commit 665662b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
30 changes: 30 additions & 0 deletions projects/observable-webworker/src/lib/from-worker-pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, number>(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<number, number>(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;
Expand Down
8 changes: 6 additions & 2 deletions projects/observable-webworker/src/lib/from-worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface LazyWorker {

export interface WorkerPoolOptions<I, O> {
workerCount?: number;
fallbackWorkerCount?: number;
flattenOperator?: (input$: Observable<Observable<O>>) => Observable<O>;
selectTransferables?: (input: I) => Transferable[];
}
Expand All @@ -24,7 +25,8 @@ export function fromWorkerPool<I, O>(
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<O>(),
} = options || {};

Expand All @@ -35,7 +37,9 @@ export function fromWorkerPool<I, O>(
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() {
Expand Down

0 comments on commit 665662b

Please sign in to comment.