Home / File/ computed.ts — vue Source File

computed.ts — vue Source File

Architecture documentation for computed.ts, a typescript file in the vue codebase. 12 imports, 1 dependents.

File typescript CompilerSFC StyleProcessor 12 imports 1 dependents 3 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  50bb6353_35b0_d08f_72d3_b5e73e75dfee["computed.ts"]
  ada43310_8d32_ee2a_f18b_1e24fd4f8a1d["ref.ts"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> ada43310_8d32_ee2a_f18b_1e24fd4f8a1d
  b2dbf772_f39b_f942_7a94_3bc9c73b0a11["Ref"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> b2dbf772_f39b_f942_7a94_3bc9c73b0a11
  08d717e5_b9dd_701c_e7a2_51d83681d28a["currentInstance.ts"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 08d717e5_b9dd_701c_e7a2_51d83681d28a
  abfe3078_bfec_252c_2107_7cdf0dd06f08["reactive.ts"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> abfe3078_bfec_252c_2107_7cdf0dd06f08
  2b181521_4bf2_5868_7efc_af9addf489bf["ReactiveFlags"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 2b181521_4bf2_5868_7efc_af9addf489bf
  faea712d_dcaf_968d_10de_977c7fd1c363["operations.ts"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> faea712d_dcaf_968d_10de_977c7fd1c363
  8c6d2de9_125d_2555_5536_17f5a6ef143f["TrackOpTypes"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 8c6d2de9_125d_2555_5536_17f5a6ef143f
  304b1edc_7b13_538e_3309_079eb06f0a14["debug.ts"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 304b1edc_7b13_538e_3309_079eb06f0a14
  2b853d15_9736_d4e6_eadb_f4f6db398606["DebuggerOptions"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 2b853d15_9736_d4e6_eadb_f4f6db398606
  8a5fb776_a8f4_ce8a_8549_67af07f2e1e9["util"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 8a5fb776_a8f4_ce8a_8549_67af07f2e1e9
  816bcd3b_1a7f_7b21_ab25_7a23681aea66["watcher"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 816bcd3b_1a7f_7b21_ab25_7a23681aea66
  249e9921_5d55_45a6_e589_ee123f22bc67["dep"]
  50bb6353_35b0_d08f_72d3_b5e73e75dfee --> 249e9921_5d55_45a6_e589_ee123f22bc67
  38aa88fe_678d_7728_4258_240bdb85b32b["apiWatch.ts"]
  38aa88fe_678d_7728_4258_240bdb85b32b --> 50bb6353_35b0_d08f_72d3_b5e73e75dfee
  style 50bb6353_35b0_d08f_72d3_b5e73e75dfee fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { isServerRendering, noop, warn, def, isFunction } from 'core/util'
import { Ref, RefFlag } from './ref'
import Watcher from 'core/observer/watcher'
import Dep from 'core/observer/dep'
import { currentInstance } from '../currentInstance'
import { ReactiveFlags } from './reactive'
import { TrackOpTypes } from './operations'
import { DebuggerOptions } from '../debug'

declare const ComputedRefSymbol: unique symbol

export interface ComputedRef<T = any> extends WritableComputedRef<T> {
  readonly value: T
  [ComputedRefSymbol]: true
}

export interface WritableComputedRef<T> extends Ref<T> {
  readonly effect: any /* Watcher */
}

export type ComputedGetter<T> = (...args: any[]) => T
export type ComputedSetter<T> = (v: T) => void

export interface WritableComputedOptions<T> {
  get: ComputedGetter<T>
  set: ComputedSetter<T>
}

export function computed<T>(
  getter: ComputedGetter<T>,
  debugOptions?: DebuggerOptions
): ComputedRef<T>
export function computed<T>(
  options: WritableComputedOptions<T>,
  debugOptions?: DebuggerOptions
): WritableComputedRef<T>
export function computed<T>(
  getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
  debugOptions?: DebuggerOptions
) {
  let getter: ComputedGetter<T>
  let setter: ComputedSetter<T>

  const onlyGetter = isFunction(getterOrOptions)
  if (onlyGetter) {
    getter = getterOrOptions
    setter = __DEV__
      ? () => {
          warn('Write operation failed: computed value is readonly')
        }
      : noop
  } else {
    getter = getterOrOptions.get
    setter = getterOrOptions.set
  }

  const watcher = isServerRendering()
    ? null
    : new Watcher(currentInstance, getter, noop, { lazy: true })

  if (__DEV__ && watcher && debugOptions) {
    watcher.onTrack = debugOptions.onTrack
    watcher.onTrigger = debugOptions.onTrigger
  }

  const ref = {
    // some libs rely on the presence effect for checking computed refs
    // from normal refs, but the implementation doesn't matter
    effect: watcher,
    get value() {
      if (watcher) {
        if (watcher.dirty) {
          watcher.evaluate()
        }
        if (Dep.target) {
          if (__DEV__ && Dep.target.onTrack) {
            Dep.target.onTrack({
              effect: Dep.target,
              target: ref,
              type: TrackOpTypes.GET,
              key: 'value'
            })
          }
          watcher.depend()
        }
        return watcher.value
      } else {
        return getter()
      }
    },
    set value(newVal) {
      setter(newVal)
    }
  } as any

  def(ref, RefFlag, true)
  def(ref, ReactiveFlags.IS_READONLY, onlyGetter)

  return ref
}

Domain

Subdomains

Functions

Imported By

Frequently Asked Questions

What does computed.ts do?
computed.ts is a source file in the vue codebase, written in typescript. It belongs to the CompilerSFC domain, StyleProcessor subdomain.
What functions are defined in computed.ts?
computed.ts defines 3 function(s): T, computed, v.
What does computed.ts depend on?
computed.ts imports 12 module(s): DebuggerOptions, ReactiveFlags, Ref, TrackOpTypes, currentInstance.ts, debug.ts, dep, operations.ts, and 4 more.
What files import computed.ts?
computed.ts is imported by 1 file(s): apiWatch.ts.
Where is computed.ts in the architecture?
computed.ts is located at src/v3/reactivity/computed.ts (domain: CompilerSFC, subdomain: StyleProcessor, directory: src/v3/reactivity).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free