Skip to content

Commit

Permalink
fix: 🐛 fix worker error in nextjs
Browse files Browse the repository at this point in the history
  • Loading branch information
tangzx committed Oct 17, 2023
1 parent 0e4fb17 commit 8bf17cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "let-hooks",
"version": "0.0.8",
"version": "0.0.9",
"description": "A react hooks library",
"keywords": [
"react",
Expand Down
60 changes: 32 additions & 28 deletions src/utils/work-timer/timer.worker.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import isBrowser from '../isBrowser';
import { MsgType } from './types';

addEventListener('message', (message: MessageEvent) => {
const data = message.data;
switch (data.type) {
case MsgType.Create: {
const $timer = setTimeout(() => {
postMessage({
taskId: data.taskId,
type: MsgType.Execute,
});
if (isBrowser()) {
const webWorker = self;

clearTimeout($timer);
}, data.wait);
webWorker.addEventListener('message', (message: MessageEvent) => {
const data = message.data;
switch (data.type) {
case MsgType.Create: {
const $timer = setTimeout(() => {
webWorker.postMessage({
taskId: data.taskId,
type: MsgType.Execute,
});

postMessage({
taskId: data.taskId,
timer: $timer,
type: MsgType.Create,
});
break;
}
case MsgType.Clear: {
clearTimeout(data.timerId);
postMessage({
taskId: data.taskId,
type: MsgType.Clear,
});
break;
}
}
});
clearTimeout($timer);
}, data.wait);

webWorker.postMessage({
taskId: data.taskId,
timer: $timer,
type: MsgType.Create,
});
break;
}
case MsgType.Clear: {
clearTimeout(data.timerId);
webWorker.postMessage({
taskId: data.taskId,
type: MsgType.Clear,
});
break;
}
}
});
}
export default null as any;

0 comments on commit 8bf17cb

Please sign in to comment.