Skip to content

Commit 1d9f15d

Browse files
Jarvis636431Single-Dancer
authored andcommitted
fix(h5): expose asynchronous Worker errors
1 parent 058d2b0 commit 1d9f15d

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎packages/taro-h5/__tests__/worker/createWorker.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ describe('createWorker', () => {
4949
expect(() => createWorker('/third.js')).toThrow('terminate the active worker')
5050
})
5151

52+
it('reports async script errors and permits explicit termination and retry', () => {
53+
worker = createWorker('/missing.js')
54+
const onError = jest.fn()
55+
worker.onError(onError)
56+
NativeWorker.current.dispatchEvent(new ErrorEvent('error', { message: 'script load failed' }))
57+
expect(onError).toHaveBeenCalledWith({ errMsg: 'createWorker:fail script load failed' })
58+
worker.terminate()
59+
worker = createWorker('/echo.js')
60+
const onMessage = jest.fn()
61+
worker.onMessage(onMessage)
62+
worker.postMessage({ input: 21 })
63+
NativeWorker.current.dispatchEvent(new MessageEvent('message', { data: { output: 42 } }))
64+
expect(onMessage).toHaveBeenCalledWith({ message: { output: 42 } })
65+
})
66+
5267
it('fails synchronously on an invalid path or missing browser support', () => {
5368
expect(() => createWorker('')).toThrow('non-empty string')
5469
// Delete the mock so the VM sees an absent global binding.

‎packages/taro-h5/src/api/worker/index.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import Taro from '@tarojs/api'
22

3+
export interface H5Worker extends Taro.Worker {
4+
onError(callback: (result: TaroGeneral.CallbackResult) => void): void
5+
}
6+
37
let activeWorker: Worker | undefined
48

5-
export const createWorker: typeof Taro.createWorker = scriptPath => {
9+
export const createWorker = (scriptPath: string): H5Worker => {
610
if (typeof Worker !== 'function') throw new Error('createWorker: Web Worker is unavailable')
711
if (typeof scriptPath !== 'string' || !scriptPath.trim()) {
812
throw new TypeError('createWorker: scriptPath must be a non-empty string')
@@ -15,6 +19,11 @@ export const createWorker: typeof Taro.createWorker = scriptPath => {
1519
onMessage (callback) {
1620
worker.addEventListener('message', event => callback({ message: event.data }))
1721
},
22+
onError (callback) {
23+
worker.addEventListener('error', event => callback({
24+
errMsg: `createWorker:fail ${event.message || 'worker script failed'}`,
25+
}))
26+
},
1827
onProcessKilled () {
1928
// Browsers do not expose system reclamation events for dedicated workers.
2029
},

‎packages/taro/types/api/worker/index.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ declare module '../../index' {
1111
}
1212
}
1313
interface Worker {
14+
/** 监听浏览器 Worker 的脚本加载或执行错误。收到错误后可 terminate 再重建。
15+
* @supported h5
16+
*/
17+
onError?(callback: (result: TaroGeneral.CallbackResult) => void): void
18+
1419
/** 监听主线程/Worker 线程向当前线程发送的消息的事件。
1520
* @supported weapp, h5
1621
* @see https://developers.weixin.qq.com/miniprogram/dev/api/worker/Worker.onMessage.html

0 commit comments

Comments
 (0)