web-worker-helper
Version:
Utilities for running tasks on worker threads
48 lines (47 loc) • 1.34 kB
TypeScript
/**
* Worker 配置项
*/
export type WorkerOptions = {
CDN?: string;
worker?: boolean;
maxConcurrency?: number;
maxMobileConcurrency?: number;
reuseWorkers?: boolean;
_workerType?: string;
[key: string]: any;
};
export type WorkerContext = {
process?: Process;
processInBatches?: ProcessInBatches;
};
export type Process = (data: any, options?: Record<string, any>, context?: WorkerContext) => any;
export type ProcessInBatches = (iterator: AsyncIterable<any> | Iterable<any>, options?: Record<string, any>, context?: WorkerContext) => AsyncIterable<any>;
/**
* worker 类型定义, worker 描述对象
*/
export type WorkerObject = {
id: string;
name: string;
module: string;
worker?: string | boolean;
options: Record<string, any>;
process?: Process;
processInBatches?: ProcessInBatches;
};
export type WorkerMessageType = 'process' | 'done' | 'error' | 'process-in-batches' | 'input-batch' | 'input-done' | 'output-batch';
export type WorkerMessagePayload = {
id?: number;
options?: Record<string, any>;
input?: any;
result?: any;
error?: string;
};
export type WorkerMessageData = {
source: string;
type: WorkerMessageType;
payload: WorkerMessagePayload;
};
export type WorkerMessage = {
type: string;
data: WorkerMessageData;
};