-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tangzx
committed
Oct 17, 2023
1 parent
0e4fb17
commit 8bf17cb
Showing
2 changed files
with
33 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |