Skip to content

Commit 987088d

Browse files
authored
Required import.meta.url to be passed (#276)
* feature, bugfix: Required import.meta.url to be passed * chore: Added changeset
1 parent b0d2268 commit 987088d

8 files changed

Lines changed: 35 additions & 26 deletions

File tree

.changeset/silver-planes-sink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codemod-utils/threads": minor
3+
---
4+
5+
Required import.meta.url to be passed

packages/threads/src/parallelize.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { runTask, type Task } from './run-task.js';
55

66
const MIN_NUM_TASKS_PER_WORKER = 100;
77

8+
type WorkerOptions = {
9+
importMetaUrl: string;
10+
workerFilePath: string;
11+
};
12+
813
function batchDatasets<T>(
914
datasets: T[],
1015
numTasksPerWorker: number,
@@ -22,11 +27,13 @@ function batchDatasets<T>(
2227
}
2328

2429
function createRunWorker<U>(
25-
workerFilePath: string,
30+
workerOptions: WorkerOptions,
2631
): <T>(datasets: T[]) => Promise<U[]> {
32+
const { importMetaUrl, workerFilePath } = workerOptions;
33+
2734
function runWorker<T>(datasets: T[]): Promise<U[]> {
2835
return new Promise((resolve, reject) => {
29-
const workerUrl = new URL(workerFilePath, import.meta.url);
36+
const workerUrl = new URL(workerFilePath, importMetaUrl);
3037

3138
const worker = new Worker(workerUrl, {
3239
env: SHARE_ENV,
@@ -50,12 +57,9 @@ function createRunWorker<U>(
5057

5158
export async function parallelize<T extends unknown[], U>(
5259
task: Task<T, U>,
53-
options: {
54-
datasets: T[];
55-
workerFilePath: string;
56-
},
60+
datasets: T[],
61+
workerOptions: WorkerOptions,
5762
): Promise<U[]> {
58-
const { datasets, workerFilePath } = options;
5963
const numTasks = datasets.length;
6064

6165
if (numTasks < MIN_NUM_TASKS_PER_WORKER) {
@@ -74,7 +78,7 @@ export async function parallelize<T extends unknown[], U>(
7478
numTasksPerWorker,
7579
);
7680

77-
const runWorker = createRunWorker<U>(workerFilePath);
81+
const runWorker = createRunWorker<U>(workerOptions);
7882

7983
const [mainThreadResults, ...workerResults] = await Promise.all([
8084
runTask(task, datasetsForMainThread),

packages/threads/tests/parallelize/vector-1.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ test('parallelize > vector (1)', async function () {
77
const numTasks = 0;
88
const datasets = getDatasets(numTasks);
99

10-
const output = await parallelize(task, {
11-
datasets,
12-
workerFilePath: '../tests/helpers/vector/worker.js',
10+
const output = await parallelize(task, datasets, {
11+
importMetaUrl: import.meta.url,
12+
workerFilePath: '../helpers/vector/worker.js',
1313
});
1414

1515
assertOutput(output);

packages/threads/tests/parallelize/vector-2.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ test('parallelize > vector (2)', async function () {
99
const numTasks = MIN_NUM_TASKS_PER_WORKER - 1;
1010
const datasets = getDatasets(numTasks);
1111

12-
const output = await parallelize(task, {
13-
datasets,
14-
workerFilePath: '../tests/helpers/vector/worker.js',
12+
const output = await parallelize(task, datasets, {
13+
importMetaUrl: import.meta.url,
14+
workerFilePath: '../helpers/vector/worker.js',
1515
});
1616

1717
assertOutput(output);

packages/threads/tests/parallelize/vector-3.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ test('parallelize > vector (3)', async function () {
99
const numTasks = MIN_NUM_TASKS_PER_WORKER;
1010
const datasets = getDatasets(numTasks);
1111

12-
const output = await parallelize(task, {
13-
datasets,
14-
workerFilePath: '../tests/helpers/vector/worker.js',
12+
const output = await parallelize(task, datasets, {
13+
importMetaUrl: import.meta.url,
14+
workerFilePath: '../helpers/vector/worker.js',
1515
});
1616

1717
assertOutput(output);

packages/threads/tests/parallelize/vector-4.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ test('parallelize > vector (4)', async function () {
1111
const numTasks = availableParallelism() * MIN_NUM_TASKS_PER_WORKER;
1212
const datasets = getDatasets(numTasks);
1313

14-
const output = await parallelize(task, {
15-
datasets,
16-
workerFilePath: '../tests/helpers/vector/worker.js',
14+
const output = await parallelize(task, datasets, {
15+
importMetaUrl: import.meta.url,
16+
workerFilePath: '../helpers/vector/worker.js',
1717
});
1818

1919
assertOutput(output);

packages/threads/tests/parallelize/vector-5.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ test('parallelize > vector (5)', async function () {
1111
const numTasks = availableParallelism() * (3 * MIN_NUM_TASKS_PER_WORKER);
1212
const datasets = getDatasets(numTasks);
1313

14-
const output = await parallelize(task, {
15-
datasets,
16-
workerFilePath: '../tests/helpers/vector/worker.js',
14+
const output = await parallelize(task, datasets, {
15+
importMetaUrl: import.meta.url,
16+
workerFilePath: '../helpers/vector/worker.js',
1717
});
1818

1919
assertOutput(output);

packages/threads/tests/parallelize/vector-error.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ test('parallelize > vector (error)', async function () {
1111

1212
await assert.rejects(
1313
async () => {
14-
await parallelize(taskThatErrors, {
15-
datasets,
16-
workerFilePath: '../tests/helpers/vector/worker.js',
14+
await parallelize(taskThatErrors, datasets, {
15+
importMetaUrl: import.meta.url,
16+
workerFilePath: '../helpers/vector/worker.js',
1717
});
1818
},
1919
(error: Error) => {

0 commit comments

Comments
 (0)