Skip to content

Commit

Permalink
worker timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiram committed May 9, 2024
1 parent 73ea92f commit fbdb96b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/renderer/src/utils/drpy/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Worker from '@/utils/drpy/worker?worker';
import Worker from '@/utils/drpy/worker?worker';

let worker = new Worker();
let worker: Worker = new Worker();
let timer: any = null;

const doWork = (data) => {
return new Promise((resolve, reject) => {
worker.onmessage = (event) => {
const response = event.data;
console.log(response)
if (response) clearInterval(timer);
resolve(response);
};

Expand All @@ -15,14 +16,21 @@ const doWork = (data) => {
};

worker.postMessage(data);

timer = setTimeout(async () => {
worker.terminate();
worker = new Worker();
reject(new Error('Worker job run 15s, timed out'));
}, 15000);
});
}

const terminateWork = () => {
return new Promise((resolve, reject) => {
if (timer) clearInterval(timer);
if (typeof worker !== 'undefined' && worker !== null) {
worker.terminate();
worker = new Worker(); // 重新创建
worker = new Worker();
resolve({
msg: 'Worker terminated successfully.',
code: 200
Expand All @@ -36,4 +44,4 @@ const terminateWork = () => {
});
};

export { doWork, terminateWork };
export { doWork, terminateWork };

0 comments on commit fbdb96b

Please sign in to comment.