Skip to content

Commit 499ef99

Browse files
authored
fix: Allow Workers to be instantiated directly via callback (#206)
1 parent 0552540 commit 499ef99

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alleninstitute/vis-core",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"contributors": [
55
{
66
"name": "Lane Sawyer",

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ export {
3131
HEARTBEAT_RATE_MS,
3232
} from './workers/messages';
3333

34-
export { WorkerPool } from './workers/worker-pool';
34+
export { WorkerPool, type WorkerInit } from './workers/worker-pool';

packages/core/src/workers/worker-pool.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ type MessageValidator<T> = TypeGuardFunction<unknown, T>;
1212

1313
type TypeGuardFunction<T, S extends T> = (value: T) => value is S;
1414

15+
export type WorkerInstantiationCallback = () => Worker;
16+
17+
export type WorkerInit = URL | WorkerInstantiationCallback;
18+
1519
type MessagePromise = {
1620
validator: MessageValidator<WorkerMessageWithId>;
1721
resolve: PromiseResolve<WorkerMessageWithId>;
@@ -30,11 +34,15 @@ export class WorkerPool {
3034
#timeOfPreviousHeartbeat: Map<number, number>;
3135
#which: number;
3236

33-
constructor(size: number, workerModule: URL) {
37+
constructor(size: number, workerInit: WorkerInit) {
3438
this.#workers = new Array(size);
3539
this.#timeOfPreviousHeartbeat = new Map();
3640
for (let i = 0; i < size; i++) {
37-
this.#workers[i] = new Worker(workerModule, { type: 'module' });
41+
if (workerInit instanceof URL) {
42+
this.#workers[i] = new Worker(workerInit, { type: 'module' });
43+
} else {
44+
this.#workers[i] = workerInit();
45+
}
3846
this.#workers[i].onmessage = (msg) => this.#handleMessage(i, msg);
3947
this.#timeOfPreviousHeartbeat.set(i, 0);
4048
}

packages/omezarr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alleninstitute/vis-omezarr",
3-
"version": "0.0.15",
3+
"version": "0.0.16",
44
"contributors": [
55
{
66
"name": "Lane Sawyer",

packages/omezarr/src/zarr/cache-lower.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import type { OmeZarrShapedDataset, OmeZarrMetadata } from './types';
22
import { type ZarrRequest, buildQuery, loadZarrArrayFileFromStore } from './loading';
33
import { VisZarrDataError } from '../errors';
44
import * as zarr from 'zarrita';
5-
import { logger } from '@alleninstitute/vis-core';
5+
import { logger, type WorkerInit } from '@alleninstitute/vis-core';
66
import { ZarrFetchStore, type CachingMultithreadedFetchStoreOptions } from './cached-loading/store';
77

8-
export function decoderFactory(url: string, workerModule: URL, options?: CachingMultithreadedFetchStoreOptions) {
8+
export function decoderFactory(url: string, workerModule: WorkerInit, options?: CachingMultithreadedFetchStoreOptions) {
99
const store = new ZarrFetchStore(url, workerModule, options);
1010
const getSlice = async (
1111
metadata: OmeZarrMetadata,

packages/omezarr/src/zarr/cached-loading/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Cacheable, logger, PriorityCache, WorkerPool } from '@alleninstitute/vis-core';
1+
import { type Cacheable, logger, PriorityCache, type WorkerInit, WorkerPool } from '@alleninstitute/vis-core';
22
import * as zarr from 'zarrita';
33
import { FETCH_MESSAGE_TYPE, type FetchResponseMessage, isFetchResponseMessage } from './fetch-data.interface';
44

@@ -310,8 +310,9 @@ export class CachingMultithreadedFetchStore extends zarr.FetchStore {
310310
return this.#doFetch(key, range, workerOptions, abort);
311311
}
312312
}
313+
313314
export class ZarrFetchStore extends CachingMultithreadedFetchStore {
314-
constructor(url: string | URL, workerModule: URL, options?: CachingMultithreadedFetchStoreOptions) {
315+
constructor(url: string | URL, workerModule: WorkerInit, options?: CachingMultithreadedFetchStoreOptions) {
315316
super(url, new WorkerPool(options?.numWorkers ?? DEFAULT_NUM_WORKERS, workerModule), options);
316317
}
317318
}

0 commit comments

Comments
 (0)