Skip to content

feat(h5): 支持 createWorker - #19676

Merged
Single-Dancer merged 3 commits into
NervJS:mainfrom
Jarvis636431:codex/h5-create-worker
Nov 30, 2026
Merged

Single-Dancer merged 3 commits into
NervJS:mainfrom
Jarvis636431:codex/h5-create-worker

Conversation

@Jarvis636431

@Jarvis636431 Jarvis636431 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #19675

这个 PR 做了什么?

封装浏览器 dedicated Worker,支持已部署的 classic Worker 脚本 URL、消息收发和终止。遵循公开接口的单 Worker 约定。浏览器没有小程序 onProcessKilled 事件。

验证

3 个 Jest 用例覆盖消息结构、终止后重建和同步报错。此前已在 Chrome 157 验证浏览器原生 Worker 消息往返。

这个 PR 是什么类型?

  • 新功能 (Feature)

这个 PR 涉及以下平台:

  • Web 端(H5)

Chrome 157 联调

Chrome 157 中用实际 Taro API 加载同源 classic Worker、发送和接收消息、终止后重建。

Summary by CodeRabbit

  • 新功能
    • H5 现已支持创建浏览器 Worker,可发送和接收消息,并在需要时终止 Worker;同一时间仅支持一个活动 Worker。
    • Worker 脚本路径为空、浏览器不支持 Worker,或已有活动 Worker 时,创建操作会报错。
  • 文档
    • 补充 H5 对 classic Worker 脚本 URL 的要求,并说明进程被回收回调仅在小程序端可用。

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6664959b-0a7f-4d73-b41a-c8132fc47a4e
Commits

Reviewing files that changed from the base of the PR and between b75d182 and 1d9f15d.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

H5 的 createWorker 现使用浏览器原生 Worker,支持消息收发和终止。实现会校验浏览器支持、脚本路径及活动 Worker 状态。Worker API 的平台注释和文档已补充 H5 说明,并新增相关测试。

Changes

H5 Worker 支持

Layer / File(s) Summary
Worker API 与生命周期
packages/taro/types/api/worker/index.d.ts, packages/taro-h5/src/api/worker/index.ts, packages/taro-h5/__tests__/worker/createWorker.test.ts
Worker API 的平台注释已扩展到 H5,文档补充 classic Worker 脚本 URL 要求及 onProcessKilled 的平台说明。createWorker 创建浏览器 Worker,校验脚本路径和活动实例,并提供消息适配、消息发送及终止操作。测试覆盖消息转发、活动实例互斥、终止后的释放和错误路径。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Taro调用方
  participant createWorker
  participant 浏览器Worker
  Taro调用方->>createWorker: createWorker(scriptPath)
  createWorker->>浏览器Worker: 创建 Worker
  Taro调用方->>createWorker: postMessage(message)
  createWorker->>浏览器Worker: postMessage(message)
  浏览器Worker-->>createWorker: message 事件及 data
  createWorker-->>Taro调用方: 回调 { message: data }
  Taro调用方->>createWorker: terminate()
  createWorker->>浏览器Worker: terminate()
Loading

Suggested reviewers: zakarycode

Merge Risk: Minimal · previous review b75d1

H5 createWorker now wraps the browser's dedicated Worker, replacing the previous unsupported placeholder. No actionable merge-blocking risk was found, and the new tests cover the main behaviors.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“feat(h5): 支持 createWorker”准确描述了主要变更:H5 端新增 createWorker 支持。变更内容包括将占位实现替换为基于浏览器 Worker 的封装、补充文档和新增测试,标题覆盖了核心目标,属于明确且相关的标题。
Linked Issues check Passed [#19675] 要求补齐 H5 createWorker 占位实现。在 head 版本的 packages/taro-h5/src/api/worker/index.ts 中:(1) createWorker(scriptPath) 使用 new Worker(scriptPath) 加载已部署的脚本 URL,未设置 type: 'module',即 classic Work…
Out of Scope Changes check Passed 变更包括 createWorker 实现、对应 Jest 测试和 packages/taro/types/api/worker/index.d.ts 的平台与文档注释更新。新增的 onError 封装与类型声明中 Worker 公开接口的 onError 相关,并由 commit fix(h5): expose asynchronous Worker errors 说明,属…
Full details: Linked Issues check

Explanation

[#19675] 要求补齐 H5 createWorker 占位实现。在 head 版本的 packages/taro-h5/src/api/worker/index.ts 中:(1) createWorker(scriptPath) 使用 new Worker(scriptPath) 加载已部署的脚本 URL,未设置 type: 'module',即 classic Worker;(2) onMessage 将原生 message 事件转换为 { message: event.data };(3) postMessage 调用原生 postMessage;(4) terminate 调用原生 terminate 并清除 activeWorker;(5) 存在活动 Worker 时抛出错误,符合单 Worker 约定;(6) onProcessKilled 为空实现,并注明浏览器不提供该事件。temporarilyNotSupport 占位已移除。packages/taro-h5/__tests__/worker/createWorker.test.ts 已在 head 中检查,覆盖消息转发与事件适配、终止后释放活动槽位、异步脚本错误与终止后重建、空路径与缺少 Worker 时的同步错误。Chrome 157 联调仅为 PR 描述中的说明,未作为代码证据使用。

Full details: Out of Scope Changes check

Explanation

变更包括 createWorker 实现、对应 Jest 测试和 packages/taro/types/api/worker/index.d.ts 的平台与文档注释更新。新增的 onError 封装与类型声明中 Worker 公开接口的 onError 相关,并由 commit fix(h5): expose asynchronous Worker errors 说明,属于遵循公开接口的实现,不属于无关变更。测试文件与实现文件直接对应本 Issue 的功能范围。未发现与 [#19675] 无关的变更。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子挥挥小耳朵,
Worker 启动跑得快。
消息来回传得清,
终止之后槽位开。
月光照着代码行,
萝卜庆祝新功能。<fixed_issue_severity>Medium</fixed_issue_severity>

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bcf12d48-1911-4783-b633-888c7c2da9fa
Commits

Reviewing files that changed from the base of the PR and between 25c3d4d and b75d182.

Files selected for processing: 3

  • packages/taro-h5/__tests__/worker/createWorker.test.ts
  • packages/taro-h5/src/api/worker/index.ts
  • packages/taro/types/api/worker/index.d.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.64%. Comparing base (b598745) to head (1d9f15d).

Files with missing lines Patch % Lines
packages/taro-h5/src/api/worker/index.ts 94.73% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19676      +/-   ##
==========================================
+ Coverage   59.62%   59.64%   +0.02%     
==========================================
  Files         475      475              
  Lines       26699    26716      +17     
  Branches     6727     6705      -22     
==========================================
+ Hits        15918    15934      +16     
+ Misses       8990     8916      -74     
- Partials     1791     1866      +75     
Flag Coverage Δ
taro-cli 59.69% <ø> (ø)
taro-runtime 66.45% <ø> (ø)
taro-web 75.94% <94.73%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-h5/src/api/worker/index.ts 94.73% <94.73%> (-5.27%) ⬇️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai
coderabbitai Bot requested a review from ZakaryCode November 13, 2026 20:42
}
if (activeWorker) throw new Error('createWorker: terminate the active worker before creating another')

const worker = new Worker(scriptPath)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Worker 脚本加载失败时,调用方收不到错误

在 Chrome 158 用本 PR 的实现复现。调用 createWorker('/missing.js'),让同源服务对该脚本返回 HTTP 404。createWorker() 同步返回实例,随后原生 Worker 触发 error。Taro 返回的对象没有暴露这个事件,onMessage 也没有回调。此时改用有效的 /echo.js 重试,仍会抛出 createWorker: terminate the active worker before creating another。对失败实例调用 terminate() 后,重新创建并发送 { input: 21 },正常收到 { message: { output: 42 } }。

建议在 H5 暴露并声明 Worker 的错误回调(例如 onError),转发原生 error,让调用方可以在失败时终止实例并重试。补一个脚本异步加载失败、收到错误回调、终止后用有效 URL 重建成功的回归测试。

@Jarvis636431

Copy link
Copy Markdown
Contributor Author

@Single-Dancer 已在 b75d182 补齐 H5 Worker 的 onError 回调和类型声明,转发原生 Worker 的异步加载及执行错误。

4 项 Worker 回归测试通过。Chrome 实测同源脚本返回 404 时可收到错误回调,调用 terminate() 后用有效脚本重建,发送 { input: 21 } 能收到 { message: { output: 42 } }。

修复已推送,新一轮 CI 正在运行,麻烦复审。

@Single-Dancer

Copy link
Copy Markdown
Collaborator

LGTM

@Single-Dancer Single-Dancer added this to the 4.4.0 milestone Nov 29, 2026
@Single-Dancer
Single-Dancer merged commit 056ffb9 into NervJS:main Nov 30, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[H5] 补齐 createWorker

2 participants