Skip to content
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

fix(defineModel): ensure trigger effect when prop changed #9841

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
refactor: simplify implementation
  • Loading branch information
yyx990803 committed Dec 16, 2023
commit 02ebffde540f953f5dd7bd64a9821c81c4f6b9e6
1 change: 1 addition & 0 deletions packages/runtime-core/__tests__/apiSetupHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ describe('SFC <script setup> helpers', () => {
expect(serializeInner(root)).toBe('2')
})

// #9838
test('pass modelValue to slot (optimized mode) ', async () => {
let foo: any
const update = () => {
Expand Down
42 changes: 19 additions & 23 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,34 +364,30 @@ export function useModel(props: Record<string, any>, name: string): Ref {
return ref() as any
}

let localValue: any
let propChangeEffect: Function | undefined
watchSyncEffect(() => {
localValue = props[name]
if (propChangeEffect) {
propChangeEffect()
propChangeEffect = undefined
}
})

return customRef((track, trigger) => ({
get() {
track()
return localValue
},
set(value) {
const rawProps = i.vnode!.props
if (hasChanged(value, localValue)) {
if (!(rawProps && name in rawProps)) {
return customRef((track, trigger) => {
let localValue: any
watchSyncEffect(() => {
const propValue = props[name]
if (hasChanged(localValue, propValue)) {
localValue = propValue
trigger()
}
})
return {
get() {
track()
return localValue
},
set(value) {
const rawProps = i.vnode!.props
if (!(rawProps && name in rawProps) && hasChanged(value, localValue)) {
localValue = value
trigger()
} else {
propChangeEffect = trigger
}
i.emit(`update:${name}`, value)
}
i.emit(`update:${name}`, value)
}
}))
})
}

function getContext(): SetupContext {
Expand Down