* 发送消息到 Worker 并等待响应(带进度回调) * 用于流式导入等长时间操作
( type: string, payload: any, onProgress?: (progress: ParseProgress) => void, timeoutMs: number = 600000 // 默认 10 分钟超时 )
| 231 | * 用于流式导入等长时间操作 |
| 232 | */ |
| 233 | function sendToWorkerWithProgress<T>( |
| 234 | type: string, |
| 235 | payload: any, |
| 236 | onProgress?: (progress: ParseProgress) => void, |
| 237 | timeoutMs: number = 600000 // 默认 10 分钟超时 |
| 238 | ): Promise<T> { |
| 239 | return new Promise((resolve, reject) => { |
| 240 | if (!worker) { |
| 241 | try { |
| 242 | initWorker() |
| 243 | } catch (error) { |
| 244 | reject(new Error('Worker not initialized')) |
| 245 | return |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | const requestWorker = worker! |
| 250 | const id = `req_${++requestIdCounter}` |
| 251 | |
| 252 | const timeout = setTimeout(() => { |
| 253 | if (pendingRequests.has(id)) { |
| 254 | pendingRequests.delete(id) |
| 255 | reject(new Error(`Worker request timeout: ${type}`)) |
| 256 | } |
| 257 | }, timeoutMs) |
| 258 | |
| 259 | pendingRequests.set(id, { resolve, reject, timeout, restartOnTimeout: false, onProgress }) |
| 260 | |
| 261 | requestWorker.postMessage({ id, type, payload }) |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * 关闭 Worker(同步版本,用于一般场景) |
no test coverage detected