diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index f6fc7f23..e0929e53 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -9,7 +9,7 @@ import { isReactive } from '../src/index' import { computed } from '@vue/runtime-dom' -import { shallowRef, unref, customRef } from '../src/ref' +import { shallowRef, unref, customRef, triggerRef } from '../src/ref' describe('reactivity/ref', () => { it('should hold a value', () => { @@ -194,7 +194,7 @@ describe('reactivity/ref', () => { expect(dummy).toBe(1) // should not trigger yet // force trigger - sref.value = sref.value + triggerRef(sref) expect(dummy).toBe(2) }) diff --git a/packages/reactivity/src/index.ts b/packages/reactivity/src/index.ts index ea0a9592..0ac9e5f3 100644 --- a/packages/reactivity/src/index.ts +++ b/packages/reactivity/src/index.ts @@ -6,6 +6,7 @@ export { toRef, toRefs, customRef, + triggerRef, Ref, UnwrapRef } from './ref' diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 59badad4..22d9c233 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -55,7 +55,7 @@ function createRef(rawValue: unknown, shallow = false) { return value }, set value(newVal) { - if (shallow || hasChanged(toRaw(newVal), rawValue)) { + if (hasChanged(toRaw(newVal), rawValue)) { rawValue = newVal value = shallow ? newVal : convert(newVal) trigger( @@ -70,6 +70,15 @@ function createRef(rawValue: unknown, shallow = false) { return r } +export function triggerRef(ref: Ref) { + trigger( + ref, + TriggerOpTypes.SET, + 'value', + __DEV__ ? { newValue: ref.value } : void 0 + ) +} + export function unref(ref: T): T extends Ref ? V : T { return isRef(ref) ? (ref.value as any) : ref } diff --git a/packages/runtime-core/src/index.ts b/packages/runtime-core/src/index.ts index 316e1c7d..3bf99a71 100644 --- a/packages/runtime-core/src/index.ts +++ b/packages/runtime-core/src/index.ts @@ -16,6 +16,7 @@ export { isReadonly, // advanced customRef, + triggerRef, shallowRef, shallowReactive, shallowReadonly,