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

Function createWatcher

packages/runtime-core/src/componentOptions.ts:849–920  ·  view source on GitHub ↗
(
  raw: ComponentWatchOptionItem,
  ctx: Data,
  publicThis: ComponentPublicInstance,
  key: string,
)

Source from the content-addressed store, hash-verified

847}
848
849export function createWatcher(
850 raw: ComponentWatchOptionItem,
851 ctx: Data,
852 publicThis: ComponentPublicInstance,
853 key: string,
854): void {
855 let getter = key.includes('.')
856 ? createPathGetter(publicThis, key)
857 : () => publicThis[key as keyof typeof publicThis]
858
859 const options: WatchOptions = {}
860 if (__COMPAT__) {
861 const instance =
862 currentInstance && getCurrentScope() === currentInstance.scope
863 ? currentInstance
864 : null
865
866 const newValue = getter()
867 if (
868 isArray(newValue) &&
869 isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
870 ) {
871 options.deep = true
872 }
873
874 const baseGetter = getter
875 getter = () => {
876 const val = baseGetter()
877 if (
878 isArray(val) &&
879 checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
880 ) {
881 traverse(val)
882 }
883 return val
884 }
885 }
886
887 if (isString(raw)) {
888 const handler = ctx[raw]
889 if (isFunction(handler)) {
890 if (__COMPAT__) {
891 watch(getter, handler as WatchCallback, options)
892 } else {
893 watch(getter, handler as WatchCallback)
894 }
895 } else if (__DEV__) {
896 warn(`Invalid watch handler specified by key "${raw}"`, handler)
897 }
898 } else if (isFunction(raw)) {
899 if (__COMPAT__) {
900 watch(getter, raw.bind(publicThis), options)
901 } else {
902 watch(getter, raw.bind(publicThis))
903 }
904 } else if (isObject(raw)) {
905 if (isArray(raw)) {
906 raw.forEach(r => createWatcher(r, ctx, publicThis, key))

Callers 1

applyOptionsFunction · 0.85

Calls 12

createPathGetterFunction · 0.90
getCurrentScopeFunction · 0.90
isCompatEnabledFunction · 0.90
checkCompatEnabledFunction · 0.90
traverseFunction · 0.90
isStringFunction · 0.90
isFunctionFunction · 0.90
watchFunction · 0.90
warnFunction · 0.90
isObjectFunction · 0.90
forEachMethod · 0.80
getterFunction · 0.50

Tested by

no test coverage detected