Optionalconfig: Partial<WorkerPoolConfig>Initializes the shared worker pool for batch sync operations.
A promise that resolves when initialization is complete.
async initialize(): Promise<void> {
if (this.isInitialized) {
return;
}
const pool = getGenericWorkerPool({
maxWorkers: this.config.maxWorkers,
enableWorkers: this.config.enableWorkers,
fallbackToMainThread: this.config.fallbackToMainThread,
});
await pool.initialize();
this.isInitialized = true;
}
Executes batch sync pre-processing using a worker or the main thread. Uses the standard pool task registration and message dispatch flow.
Entries to prepare for synchronization.
OptionalonProgress: (Optional callback for progress updates.
OptionaltaskId: stringOptional external task identifier.
Promise resolving to prepared sync operations.
async executeBatchSyncPreprocessing(
entries: AniListMediaEntry[],
onProgress?: (
phase: "organizing" | "building" | "ready",
processed: number,
total: number,
currentMediaId?: number,
) => void,
taskId?: string,
): Promise<PreparedSyncOperation[]> {
const pool = getGenericWorkerPool();
// Ensure pool is initialized
await pool.ensureInitialized();
const mainTaskId = taskId || generateUUID();
const promise = new Promise<PreparedSyncOperation[]>((resolve, reject) => {
if (!pool.isAvailable()) {
// Fallback to main thread
this.executeBatchSyncMainThread(entries).then(resolve).catch(reject);
return;
}
const workerIndex = pool.selectWorker();
if (workerIndex === -1) {
// Fallback to main thread
this.executeBatchSyncMainThread(entries).then(resolve).catch(reject);
return;
}
const worker = pool.getWorker(workerIndex);
if (!worker) {
// Fallback to main thread
this.executeBatchSyncMainThread(entries).then(resolve).catch(reject);
return;
}
// Register task with pool using standard flow
const task = {
taskId: mainTaskId,
resolve: (result: unknown) => {
// Adapt raw payload to expected shape: extract operations array
const adaptedResult =
(result as { operations?: PreparedSyncOperation[] }).operations ||
(result as PreparedSyncOperation[]);
resolve(adaptedResult);
},
reject,
isCancelled: false,
onProgress: (message: unknown) => {
// Adapt generic message to typed callback
const msgWithType = message as {
type?: string;
payload?: {
phase?: string;
processed?: number;
total?: number;
currentMediaId?: number;
};
};
if (
msgWithType.type === "BATCH_SYNC_PROGRESS" &&
onProgress &&
msgWithType.payload &&
typeof msgWithType.payload.processed === "number" &&
typeof msgWithType.payload.total === "number"
) {
const { phase, processed, total, currentMediaId } =
msgWithType.payload;
onProgress(
phase as "organizing" | "building" | "ready",
processed,
total,
currentMediaId,
);
}
},
workerIndex,
};
pool.registerTask(mainTaskId, task);
// Send message to worker
const batchSyncMessage: BatchSyncMessage = {
type: "BATCH_SYNC",
payload: {
taskId: mainTaskId,
entries,
rateLimitConfig: {
maxRequestsPerMinute: 60,
requestInterval: 1000,
},
},
};
worker.postMessage(batchSyncMessage);
console.info(
`[BatchSyncWorkerPool] 📦 Dispatched batch sync pre-processing to worker ${workerIndex}: ${entries.length} entries`,
);
});
return promise;
}
Manages batch sync pre-processing using the shared worker pool.
Source