Skip to content

fix(compat): watch arrays as deep: 1 #13377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ export function watch(
forceTrigger ||
(isMultiSource
? (newValue as any[]).some((v, i) => hasChanged(v, oldValue[i]))
: hasChanged(newValue, oldValue))
: hasChanged(newValue, oldValue)) ||
(__COMPAT__ && (options as any).compatWatchArray && isArray(newValue))
) {
// cleanup before running cb again
if (cleanup) {
Expand Down
46 changes: 44 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import {
type WatchHandle,
type WatchSource,
watch as baseWatch,
traverse,
} from '@vue/reactivity'
import { type SchedulerJob, SchedulerJobFlags, queueJob } from './scheduler'
import { EMPTY_OBJ, NOOP, extend, isFunction, isString } from '@vue/shared'
import {
EMPTY_OBJ,
NOOP,
extend,
isArray,
isFunction,
isString,
} from '@vue/shared'
import {
type ComponentInternalInstance,
currentInstance,
Expand All @@ -19,6 +27,11 @@ import {
import { callWithAsyncErrorHandling } from './errorHandling'
import { queuePostRenderEffect } from './renderer'
import { warn } from './warning'
import {
DeprecationTypes,
checkCompatEnabled,
isCompatEnabled,
} from './compat/compatConfig'
import type { ObjectWatchOptionItem } from './componentOptions'
import { useSSRContext } from './helpers/useSsrContext'

Expand Down Expand Up @@ -236,6 +249,22 @@ function doWatch(
return watchHandle
}

export function createCompatWatchGetter(
baseGetter: () => any,
instance: ComponentInternalInstance,
) {
return (): any => {
const val = baseGetter()
if (
isArray(val) &&
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) {
traverse(val, 1)
}
return val
}
}

// this.$watch
export function instanceWatch(
this: ComponentInternalInstance,
Expand All @@ -244,7 +273,7 @@ export function instanceWatch(
options?: WatchOptions,
): WatchHandle {
const publicThis = this.proxy as any
const getter = isString(source)
let getter = isString(source)
? source.includes('.')
? createPathGetter(publicThis, source)
: () => publicThis[source]
Expand All @@ -256,6 +285,19 @@ export function instanceWatch(
cb = value.handler as Function
options = value
}

if (
__COMPAT__ &&
isString(source) &&
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, this)
) {
const deep = options && options.deep
if (!deep) {
options = extend({ compatWatchArray: true }, options)
getter = createCompatWatchGetter(getter, this)
}
}

const reset = setCurrentInstance(this)
const res = doWatch(getter, cb.bind(publicThis), options)
reset()
Expand Down
56 changes: 20 additions & 36 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
type Component,
type ComponentInternalInstance,
type ComponentInternalOptions,
type ConcreteComponent,
type Data,
type InternalRenderFunction,
type SetupContext,
currentInstance,
import type {
Component,
ComponentInternalInstance,
ComponentInternalOptions,
ConcreteComponent,
Data,
InternalRenderFunction,
SetupContext,
} from './component'
import {
type LooseRequired,
Expand All @@ -19,11 +18,12 @@ import {
isPromise,
isString,
} from '@vue/shared'
import { type Ref, getCurrentScope, isRef, traverse } from '@vue/reactivity'
import { type Ref, isRef } from '@vue/reactivity'
import { computed } from './apiComputed'
import {
type WatchCallback,
type WatchOptions,
createCompatWatchGetter,
createPathGetter,
watch,
} from './apiWatch'
Expand Down Expand Up @@ -72,9 +72,9 @@ import { warn } from './warning'
import type { VNodeChild } from './vnode'
import { callWithAsyncErrorHandling } from './errorHandling'
import { deepMergeData } from './compat/data'
import { DeprecationTypes, checkCompatEnabled } from './compat/compatConfig'
import {
type CompatConfig,
DeprecationTypes,
isCompatEnabled,
softAssertCompatEnabled,
} from './compat/compatConfig'
Expand Down Expand Up @@ -843,7 +843,7 @@ function callHook(
)
}

export function createWatcher(
function createWatcher(
raw: ComponentWatchOptionItem,
ctx: Data,
publicThis: ComponentPublicInstance,
Expand All @@ -854,30 +854,14 @@ export function createWatcher(
: () => (publicThis as any)[key]

const options: WatchOptions = {}
if (__COMPAT__) {
const instance =
currentInstance && getCurrentScope() === currentInstance.scope
? currentInstance
: null

const newValue = getter()
if (
isArray(newValue) &&
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) {
options.deep = true
}

const baseGetter = getter
getter = () => {
const val = baseGetter()
if (
isArray(val) &&
checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) {
traverse(val)
}
return val
if (
__COMPAT__ &&
isCompatEnabled(DeprecationTypes.WATCH_ARRAY, publicThis.$)
) {
const deep = isObject(raw) && !isArray(raw) && !isFunction(raw) && raw.deep
if (!deep) {
;(options as any).compatWatchArray = true
getter = createCompatWatchGetter(getter, publicThis.$)
}
}

Expand Down
Loading