MCPcopy
hub / github.com/vuejs/core / refreshComputed

Function refreshComputed

packages/reactivity/src/effect.ts:376–430  ·  view source on GitHub ↗
(computed: ComputedRefImpl)

Source from the content-addressed store, hash-verified

374 * @internal
375 */
376export function refreshComputed(computed: ComputedRefImpl): undefined {
377 if (
378 computed.flags & EffectFlags.TRACKING &&
379 !(computed.flags & EffectFlags.DIRTY)
380 ) {
381 return
382 }
383 computed.flags &= ~EffectFlags.DIRTY
384
385 // Global version fast path when no reactive changes has happened since
386 // last refresh.
387 if (computed.globalVersion === globalVersion) {
388 return
389 }
390 computed.globalVersion = globalVersion
391
392 // In SSR there will be no render effect, so the computed has no subscriber
393 // and therefore tracks no deps, thus we cannot rely on the dirty check.
394 // Instead, computed always re-evaluate and relies on the globalVersion
395 // fast path above for caching.
396 // #12337 if computed has no deps (does not rely on any reactive data) and evaluated,
397 // there is no need to re-evaluate.
398 if (
399 !computed.isSSR &&
400 computed.flags & EffectFlags.EVALUATED &&
401 ((!computed.deps && !(computed as any)._dirty) || !isDirty(computed))
402 ) {
403 return
404 }
405 computed.flags |= EffectFlags.RUNNING
406
407 const dep = computed.dep
408 const prevSub = activeSub
409 const prevShouldTrack = shouldTrack
410 activeSub = computed
411 shouldTrack = true
412
413 try {
414 prepareDeps(computed)
415 const value = computed.fn(computed._value)
416 if (dep.version === 0 || hasChanged(value, computed._value)) {
417 computed.flags |= EffectFlags.EVALUATED
418 computed._value = value
419 dep.version++
420 }
421 } catch (err) {
422 dep.version++
423 throw err
424 } finally {
425 activeSub = prevSub
426 shouldTrack = prevShouldTrack
427 cleanupDeps(computed)
428 computed.flags &= ~EffectFlags.RUNNING
429 }
430}
431
432function removeSub(link: Link, soft = false) {
433 const { dep, prevSub, nextSub } = link

Callers 2

valueMethod · 0.90
isDirtyFunction · 0.85

Calls 4

hasChangedFunction · 0.90
isDirtyFunction · 0.85
prepareDepsFunction · 0.85
cleanupDepsFunction · 0.85

Tested by

no test coverage detected