refactor(shared): Consolidate initTaskRunner implementations

Add generic initTaskRunner function to processConcurrency.ts to eliminate
duplicate initialization logic across multiple modules. This reduces code
duplication and provides consistent worker pool management with proper
type safety through generic parameters.

- Add TaskRunner<T, R> interface and initTaskRunner function
- Remove duplicate createTaskRunner wrappers from 5 modules
- Update all deps parameters to use shared initTaskRunner directly
- Maintain type safety with explicit generic type parameters
- Update corresponding test mocks to match new signature

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Kazuki Yamada
2025-07-24 23:47:07 +09:00
parent 97d4171176
commit 748ce7cead
13 changed files with 109 additions and 83 deletions
+30 -2
View File
@@ -1,7 +1,12 @@
import os from 'node:os';
import { Tinypool } from 'tinypool';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getProcessConcurrency, getWorkerThreadCount, initWorker } from '../../src/shared/processConcurrency.js';
import {
createWorkerPool,
getProcessConcurrency,
getWorkerThreadCount,
initTaskRunner,
} from '../../src/shared/processConcurrency.js';
vi.mock('node:os');
vi.mock('tinypool');
@@ -68,7 +73,7 @@ describe('processConcurrency', () => {
it('should initialize Tinypool with correct configuration', () => {
const workerPath = '/path/to/worker.js';
const tinypool = initWorker(500, workerPath);
const tinypool = createWorkerPool(500, workerPath);
expect(Tinypool).toHaveBeenCalledWith({
filename: workerPath,
@@ -82,4 +87,27 @@ describe('processConcurrency', () => {
expect(tinypool).toBeDefined();
});
});
describe('initTaskRunner', () => {
beforeEach(() => {
vi.mocked(os).availableParallelism = vi.fn().mockReturnValue(4);
vi.mocked(Tinypool).mockImplementation(
() =>
({
run: vi.fn(),
destroy: vi.fn(),
}) as unknown as Tinypool,
);
});
it('should return a TaskRunner with run and cleanup methods', () => {
const workerPath = '/path/to/worker.js';
const taskRunner = initTaskRunner(100, workerPath);
expect(taskRunner).toHaveProperty('run');
expect(taskRunner).toHaveProperty('cleanup');
expect(typeof taskRunner.run).toBe('function');
expect(typeof taskRunner.cleanup).toBe('function');
});
});
});