Skip to content

Commit f1fe01e

Browse files
committed
refactor: externalized COMPAT case
1 parent 9cbb277 commit f1fe01e

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

packages/runtime-core/src/apiWatch.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ import {
3939
} from './errorHandling'
4040
import { queuePostRenderEffect } from './renderer'
4141
import { warn } from './warning'
42-
import { DeprecationTypes } from './compat/compatConfig'
43-
import { checkCompatEnabled, isCompatEnabled } from './compat/compatConfig'
4442
import { ObjectWatchOptionItem } from './componentOptions'
4543
import { useSSRContext } from '@vue/runtime-core'
4644

@@ -268,21 +266,6 @@ function doWatch(
268266
__DEV__ && warnInvalidSource(source)
269267
}
270268

271-
// 2.x array mutation watch compat
272-
if (__COMPAT__ && cb && !deep) {
273-
const baseGetter = getter
274-
getter = () => {
275-
const val = baseGetter()
276-
if (
277-
isArray(val) &&
278-
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
279-
) {
280-
traverse(val)
281-
}
282-
return val
283-
}
284-
}
285-
286269
if (cb && deep) {
287270
const baseGetter = getter
288271
getter = () => traverse(baseGetter())
@@ -334,10 +317,7 @@ function doWatch(
334317
forceTrigger ||
335318
(isMultiSource
336319
? (newValue as any[]).some((v, i) => hasChanged(v, oldValue[i]))
337-
: hasChanged(newValue, oldValue)) ||
338-
(__COMPAT__ &&
339-
isArray(newValue) &&
340-
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance))
320+
: hasChanged(newValue, oldValue))
341321
) {
342322
// cleanup before running cb again
343323
if (cleanup) {

packages/runtime-core/src/componentOptions.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
ComponentInternalOptions,
66
Component,
77
ConcreteComponent,
8-
InternalRenderFunction
8+
InternalRenderFunction,
9+
currentInstance
910
} from './component'
1011
import {
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'
2223
import { computed } from './apiComputed'
2324
import {
2425
watch,
2526
WatchOptions,
2627
WatchCallback,
27-
createPathGetter
28+
createPathGetter,
29+
traverse
2830
} from './apiWatch'
2931
import { provide, inject } from './apiInject'
3032
import {
@@ -67,7 +69,7 @@ import { warn } from './warning'
6769
import { VNodeChild } from './vnode'
6870
import { callWithAsyncErrorHandling } from './errorHandling'
6971
import { deepMergeData } from './compat/data'
70-
import { DeprecationTypes } from './compat/compatConfig'
72+
import { DeprecationTypes, checkCompatEnabled } from './compat/compatConfig'
7173
import {
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

Comments
 (0)