MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / initWorker

Function initWorker

apps/desktop/main/worker/workerManager.ts:119–189  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

117 * 初始化 Worker
118 */
119export function initWorker(): void {
120 if (worker) {
121 console.log('[WorkerManager] Worker already initialized')
122 return
123 }
124
125 const workerPath = getWorkerPath()
126 console.log('[WorkerManager] Initializing worker at:', workerPath)
127
128 try {
129 const initializedWorker = new Worker(workerPath, {
130 workerData: {
131 dbDir: getDbDir(),
132 cacheDir: getCacheDir(),
133 tempDir: getTempDir(),
134 nlpDir: getNlpDir(),
135 appVersion: getDesktopAppVersion(app.getVersion()),
136 },
137 })
138 worker = initializedWorker
139
140 // 监听 Worker 消息
141 initializedWorker.on('message', (message) => {
142 const { id, type, success, result, error, payload } = message
143
144 const pending = pendingRequests.get(id)
145 if (!pending) return
146
147 // 处理进度消息(不删除 pending,因为还没完成)
148 if (type === 'progress') {
149 if (pending.onProgress) {
150 pending.onProgress(payload)
151 }
152 return
153 }
154
155 // 处理完成或错误消息
156 pendingRequests.delete(id)
157 clearTimeout(pending.timeout)
158
159 if (success) {
160 pending.resolve(result)
161 } else {
162 pending.reject(new Error(error))
163 }
164 })
165
166 // 监听 Worker 错误
167 initializedWorker.on('error', (error) => {
168 console.error('[WorkerManager] Worker error:', error)
169 })
170
171 // 监听 Worker 退出
172 initializedWorker.on('exit', (code) => {
173 console.log('[WorkerManager] Worker exited with code:', code)
174 if (worker !== initializedWorker) {
175 console.log('[WorkerManager] Ignoring exit from replaced worker')
176 return

Callers 2

sendToWorkerFunction · 0.85
sendToWorkerWithProgressFunction · 0.85

Calls 13

getCacheDirFunction · 0.90
getTempDirFunction · 0.90
getNlpDirFunction · 0.90
getDesktopAppVersionFunction · 0.90
getWorkerPathFunction · 0.85
clearTimeoutFunction · 0.85
rejectAllPendingFunction · 0.85
onMethod · 0.80
getDbDirFunction · 0.70
getVersionMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected