Skip to content

Commit

Permalink
fix: Should not destroy the timer multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Mar 16, 2024
1 parent f5bcf19 commit a6935c4
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions xmcl-runtime/worker/index.ts
Expand Up @@ -11,6 +11,9 @@ export const createLazyWorker = <T>(factory: () => Worker, methods: Array<keyof
logger.log(`Awake the worker ${factory}`)
worker.on('message', (message: 'idle' | WorkerResponse) => {
if (message === 'idle') {
if (destroyTimer) {
clearTimeout(destroyTimer)
}
destroyTimer = setTimeout(() => {
if (threadWorker) {
logger.log(`Dispose the worker ${factory}`)
Expand Down Expand Up @@ -45,6 +48,7 @@ export const createLazyWorker = <T>(factory: () => Worker, methods: Array<keyof
threadWorker = threadWorker || createWorker()
if (destroyTimer) {
clearTimeout(destroyTimer)
destroyTimer = undefined
}
threadWorker.postMessage({ type: method, id: _id, args })
})
Expand Down

0 comments on commit a6935c4

Please sign in to comment.