55 ComponentInternalOptions ,
66 Component ,
77 ConcreteComponent ,
8- InternalRenderFunction
8+ InternalRenderFunction ,
9+ currentInstance
910} from './component'
1011import {
1112 isFunction ,
@@ -18,13 +19,14 @@ import {
1819 LooseRequired ,
1920 Prettify
2021} from '@vue/shared'
21- import { isRef , Ref } from '@vue/reactivity'
22+ import { getCurrentScope , isRef , Ref } from '@vue/reactivity'
2223import { computed } from './apiComputed'
2324import {
2425 watch ,
2526 WatchOptions ,
2627 WatchCallback ,
27- createPathGetter
28+ createPathGetter ,
29+ traverse
2830} from './apiWatch'
2931import { provide , inject } from './apiInject'
3032import {
@@ -67,7 +69,7 @@ import { warn } from './warning'
6769import { VNodeChild } from './vnode'
6870import { callWithAsyncErrorHandling } from './errorHandling'
6971import { deepMergeData } from './compat/data'
70- import { DeprecationTypes } from './compat/compatConfig'
72+ import { DeprecationTypes , checkCompatEnabled } from './compat/compatConfig'
7173import {
7274 CompatConfig ,
7375 isCompatEnabled ,
@@ -937,18 +939,46 @@ export function createWatcher(
937939 publicThis : ComponentPublicInstance ,
938940 key : string
939941) {
940- const getter = key . includes ( '.' )
942+ let getter = key . includes ( '.' )
941943 ? createPathGetter ( publicThis , key )
942944 : ( ) => ( publicThis as any ) [ key ]
945+
946+ const options : WatchOptions < false > = { }
947+ if ( __COMPAT__ ) {
948+ const instance =
949+ getCurrentScope ( ) === currentInstance ?. scope ? currentInstance : null
950+
951+ console . log ( 'createWatcher' )
952+ const newValue = getter ( )
953+ if (
954+ isArray ( newValue ) &&
955+ isCompatEnabled ( DeprecationTypes . WATCH_ARRAY , instance )
956+ ) {
957+ options . deep = true
958+ }
959+
960+ const baseGetter = getter
961+ getter = ( ) => {
962+ const val = baseGetter ( )
963+ if (
964+ isArray ( val ) &&
965+ checkCompatEnabled ( DeprecationTypes . WATCH_ARRAY , instance )
966+ ) {
967+ traverse ( val )
968+ }
969+ return val
970+ }
971+ }
972+
943973 if ( isString ( raw ) ) {
944974 const handler = ctx [ raw ]
945975 if ( isFunction ( handler ) ) {
946- watch ( getter , handler as WatchCallback )
976+ watch ( getter , handler as WatchCallback , options )
947977 } else if ( __DEV__ ) {
948978 warn ( `Invalid watch handler specified by key "${ raw } "` , handler )
949979 }
950980 } else if ( isFunction ( raw ) ) {
951- watch ( getter , raw . bind ( publicThis ) )
981+ watch ( getter , raw . bind ( publicThis ) , options )
952982 } else if ( isObject ( raw ) ) {
953983 if ( isArray ( raw ) ) {
954984 raw . forEach ( r => createWatcher ( r , ctx , publicThis , key ) )
@@ -957,7 +987,7 @@ export function createWatcher(
957987 ? raw . handler . bind ( publicThis )
958988 : ( ctx [ raw . handler ] as WatchCallback )
959989 if ( isFunction ( handler ) ) {
960- watch ( getter , handler , raw )
990+ watch ( getter , handler , extend ( raw , options ) )
961991 } else if ( __DEV__ ) {
962992 warn ( `Invalid watch handler specified by key "${ raw . handler } "` , handler )
963993 }
0 commit comments