5
5
ComponentInternalOptions ,
6
6
Component ,
7
7
ConcreteComponent ,
8
- InternalRenderFunction
8
+ InternalRenderFunction ,
9
+ currentInstance
9
10
} from './component'
10
11
import {
11
12
isFunction ,
@@ -18,13 +19,14 @@ import {
18
19
LooseRequired ,
19
20
Prettify
20
21
} from '@vue/shared'
21
- import { isRef , Ref } from '@vue/reactivity'
22
+ import { getCurrentScope , isRef , Ref } from '@vue/reactivity'
22
23
import { computed } from './apiComputed'
23
24
import {
24
25
watch ,
25
26
WatchOptions ,
26
27
WatchCallback ,
27
- createPathGetter
28
+ createPathGetter ,
29
+ traverse
28
30
} from './apiWatch'
29
31
import { provide , inject } from './apiInject'
30
32
import {
@@ -67,7 +69,7 @@ import { warn } from './warning'
67
69
import { VNodeChild } from './vnode'
68
70
import { callWithAsyncErrorHandling } from './errorHandling'
69
71
import { deepMergeData } from './compat/data'
70
- import { DeprecationTypes } from './compat/compatConfig'
72
+ import { DeprecationTypes , checkCompatEnabled } from './compat/compatConfig'
71
73
import {
72
74
CompatConfig ,
73
75
isCompatEnabled ,
@@ -937,18 +939,46 @@ export function createWatcher(
937
939
publicThis : ComponentPublicInstance ,
938
940
key : string
939
941
) {
940
- const getter = key . includes ( '.' )
942
+ let getter = key . includes ( '.' )
941
943
? createPathGetter ( publicThis , key )
942
944
: ( ) => ( 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
+
943
973
if ( isString ( raw ) ) {
944
974
const handler = ctx [ raw ]
945
975
if ( isFunction ( handler ) ) {
946
- watch ( getter , handler as WatchCallback )
976
+ watch ( getter , handler as WatchCallback , options )
947
977
} else if ( __DEV__ ) {
948
978
warn ( `Invalid watch handler specified by key "${ raw } "` , handler )
949
979
}
950
980
} else if ( isFunction ( raw ) ) {
951
- watch ( getter , raw . bind ( publicThis ) )
981
+ watch ( getter , raw . bind ( publicThis ) , options )
952
982
} else if ( isObject ( raw ) ) {
953
983
if ( isArray ( raw ) ) {
954
984
raw . forEach ( r => createWatcher ( r , ctx , publicThis , key ) )
@@ -957,7 +987,7 @@ export function createWatcher(
957
987
? raw . handler . bind ( publicThis )
958
988
: ( ctx [ raw . handler ] as WatchCallback )
959
989
if ( isFunction ( handler ) ) {
960
- watch ( getter , handler , raw )
990
+ watch ( getter , handler , extend ( raw , options ) )
961
991
} else if ( __DEV__ ) {
962
992
warn ( `Invalid watch handler specified by key "${ raw . handler } "` , handler )
963
993
}
0 commit comments