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(shallowReactive): don't trigger watchers for oldVal === newVal #894

Merged
merged 2 commits into from Jan 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/reactivity/reactive.ts
Expand Up @@ -213,7 +213,8 @@ export function shallowReactive(obj: any) {
return value
},
set: function setterHandler(newVal: any) {
if (getter && !setter) return
const value = getter ? getter.call(obj) : val
if (newVal === value || (getter && !setter)) return
if (setter) {
setter.call(obj, newVal)
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/reactivity/ref.ts
Expand Up @@ -184,7 +184,9 @@ export function shallowRef(raw?: unknown) {
export function triggerRef(value: any) {
if (!isRef(value)) return

value.value = value.value
const v = value.value
value.value = !v
value.value = v
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might need some advice here... this is not ideal for sync: true watchers, but I can't call ob.notify() without making bigger changes as the reference is not retrievable from value

}

export function proxyRefs<T extends object>(
Expand Down
20 changes: 19 additions & 1 deletion test/v3/reactivity/ref.spec.ts
Expand Up @@ -250,11 +250,29 @@ describe('reactivity/ref', () => {
expect(dummy).toBe(1) // should not trigger yet

// force trigger
// sref.value = sref.value;
triggerRef(sref)
expect(dummy).toBe(2)
})

test('shallowRef noop when assignment is the same', () => {
const sref = shallowRef(1)
let calls = 0
watchEffect(
() => {
sref.value
calls++
},
{ flush: 'sync' }
)
expect(calls).toBe(1)

sref.value = 2
expect(calls).toBe(2)

sref.value = 2
expect(calls).toBe(2)
})

test('isRef', () => {
expect(isRef(ref(1))).toBe(true)
expect(isRef(computed(() => 1))).toBe(true)
Expand Down