代码 Vue 3.6.0-rc.15 的 reactivity 修复:computed 自读、抛错恢复与 stopped scope 的通知边界

2026-11-17 21:05:28

Vue 3.6.0-rc.15 的 reactivity 修复:computed 自读、抛错恢复与 stopped scope 的通知边界

Vue 3.6 把 @vue/reactivity 换成了基于 alien-signals 的实现,官方给出的目标是响应式系统的性能与内存占用。3.6.0-beta.1 发布于 2025-12-23,rc.1 是 2026-07-18,rc.15 是 2026-11-12;稳定版仍未发布,3.5.x 还在接收补丁(3.5.45 于 2026-11-05)。

rc.15 的 CHANGELOG 里有一批集中在 computed / effect 边界上的 reactivity 修复。它们不是新特性,而是重构后 dirty check、通知传播和生命周期这三条路径上的收口。

computed 自读或互相读:dirty check 要能结束

  • reactivity: end the dirty check on computeds that read themselves or each other (#16218)

一个 computed 在 getter 里读到自己,或者 A、B 两个 computed 互相读,dirty check 会沿着依赖图来回走。修复让这条检查在遇到自读/互读时结束,避免无限检查把渲染卡死。

对应的业务写法多半是事故而不是设计:

// Options API 里把 this 上的同名计算属性又读了一遍
computed: {
  total() {
    return this.total + this.base // 自读
  }
}

// 两个计算属性互相依赖
const a = computed(() => b.value + 1)
const b = computed(() => a.value + 1)

修复保证 dirty check 会终止,但循环依赖本身仍然不是能用的写法,取值时依旧会递归下去。升级后如果页面不再整页卡死而是报栈溢出,说明踩的就是这一类。

被自己的 run 判失效:通知对象要分开看

  • reactivity: keep notifying a subscriber invalidated by its own run (#16210), closes #16174
  • reactivity: do not notify readers of a computed invalidated by its own run (#16220)

这两条针对的是「effect 或 computed 在自己的 run 过程中把自己标成 dirty」的情形,但处理对象不同:effect 订阅者仍然要收到通知,否则 watchEffect 会丢更新;被自己 run 判失效的 computed 读者则不再通知,避免在同一轮里互相触发。

典型写法是在 watchEffect 里既读又写同一个源:

watchEffect(() => {
  count.value = count.value + 1
})

这种自触发写法本身应该改掉,但升级后它的表现会从「少触发一次」变成「按订阅语义稳定触发」,排查时不要把它当成新 bug。

getter 抛异常:通知不中断,恢复后重新求值

  • reactivity: keep notifying subscribers after a computed throws (ce28d57)
  • reactivity: re-evaluate readers of a computed that threw once it recovers (dc92688)

computed 的 getter 抛异常后,对订阅者的通知不再中断;等这个 computed 恢复正常求值,之前读到过它的读者会被重新求值。

容易踩到的是用 try/catch 把异常吞在 getter 内部或读取处:

const safe = computed(() => {
  try {
    return risky.value
  } catch {
    return fallback
  }
})

watchEffect(() => {
  try {
    render(safe.value)
  } catch {}
})

如果测试里出现过「异常恢复后视图停在旧值」,这两条就是对应的修复。

stopped scope 与调度器

  • reactivity: don't keep a computed read in a stopped effect scope subscribed (#16114), closes #16112
  • reactivity: don't call the scheduler of an effect stopped before the flush (#16214)

在已经 stop() 的 effectScope 里读过的 computed,不再保持订阅;在 flush 之前被 stop 的 effect,不再调用它的 scheduler。前者是内存和无效回调的问题,后者是队列里已经失效的任务仍被执行。

自查可以直接用 effectScope 断言:

const scope = effectScope()
const spy = vi.fn()
const source = ref(0)

scope.run(() => {
  const doubled = computed(() => source.value * 2)
  watchEffect(() => spy(doubled.value))
})

scope.stop()
source.value++

expect(spy).toHaveBeenCalledTimes(1) // 只有 stop 前那一次

SSR setup 期间创建的 computed 要释放

  • reactivity: release computeds created during SSR setup (#16013) (0727aa0)

服务端每次渲染都会跑 setup,期间创建的 computed 如果不释放,长时间运行的 SSR 进程会持续持有这些实例。这条对 SSR 服务的内存曲线影响比单次请求的耗时更明显。

同版本的其他条目

不在 computed/effect 边界这条线上,但同属 rc.15 的 reactivity 改动:

  • do not mutate the options object passed to effect() (#16105)
  • keep an effect stopped by a computed getter during its dirty check (#16212)
  • run onTrigger hooks after the trigger has propagated (#16213)
  • stop removed links from keeping younger effects alive across scavenges (#16211), closes #16176
  • runtime-core: hydrate a lazy async component deactivated by KeepAlive before it resolved (#16199)

升级与自查

这是 RC,不是稳定版,生产别上。3.5.x 还在收补丁,需要稳定行为就留在 3.5。

要在项目里试,按官方建议:先不启用 Vapor,只替换 reactivity 内核;选一两个页面小范围验证,不要全量切换。升级到 rc.15 之后先跑一遍组件测试,重点看两类用例:异常路径(getter 抛错、try/catch 吞错后的恢复)和作用域清理(effectScope stop、组件卸载后源变更是否还触发回调)。上面那段 effectScope 断言可以直接加进现有测试。

复制全文 生成海报 Vue 响应式 前端 调试

推荐文章

程序员茄子在线接单