feat(reactivity): add triggerRef API
Also make shallowRef assignment behavior consistent with normal ref
This commit is contained in:
parent
3e60288a6d
commit
2acf3e84b9
@ -9,7 +9,7 @@ import {
|
|||||||
isReactive
|
isReactive
|
||||||
} from '../src/index'
|
} from '../src/index'
|
||||||
import { computed } from '@vue/runtime-dom'
|
import { computed } from '@vue/runtime-dom'
|
||||||
import { shallowRef, unref, customRef } from '../src/ref'
|
import { shallowRef, unref, customRef, triggerRef } from '../src/ref'
|
||||||
|
|
||||||
describe('reactivity/ref', () => {
|
describe('reactivity/ref', () => {
|
||||||
it('should hold a value', () => {
|
it('should hold a value', () => {
|
||||||
@ -194,7 +194,7 @@ describe('reactivity/ref', () => {
|
|||||||
expect(dummy).toBe(1) // should not trigger yet
|
expect(dummy).toBe(1) // should not trigger yet
|
||||||
|
|
||||||
// force trigger
|
// force trigger
|
||||||
sref.value = sref.value
|
triggerRef(sref)
|
||||||
expect(dummy).toBe(2)
|
expect(dummy).toBe(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ export {
|
|||||||
toRef,
|
toRef,
|
||||||
toRefs,
|
toRefs,
|
||||||
customRef,
|
customRef,
|
||||||
|
triggerRef,
|
||||||
Ref,
|
Ref,
|
||||||
UnwrapRef
|
UnwrapRef
|
||||||
} from './ref'
|
} from './ref'
|
||||||
|
@ -55,7 +55,7 @@ function createRef(rawValue: unknown, shallow = false) {
|
|||||||
return value
|
return value
|
||||||
},
|
},
|
||||||
set value(newVal) {
|
set value(newVal) {
|
||||||
if (shallow || hasChanged(toRaw(newVal), rawValue)) {
|
if (hasChanged(toRaw(newVal), rawValue)) {
|
||||||
rawValue = newVal
|
rawValue = newVal
|
||||||
value = shallow ? newVal : convert(newVal)
|
value = shallow ? newVal : convert(newVal)
|
||||||
trigger(
|
trigger(
|
||||||
@ -70,6 +70,15 @@ function createRef(rawValue: unknown, shallow = false) {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function triggerRef(ref: Ref) {
|
||||||
|
trigger(
|
||||||
|
ref,
|
||||||
|
TriggerOpTypes.SET,
|
||||||
|
'value',
|
||||||
|
__DEV__ ? { newValue: ref.value } : void 0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export function unref<T>(ref: T): T extends Ref<infer V> ? V : T {
|
export function unref<T>(ref: T): T extends Ref<infer V> ? V : T {
|
||||||
return isRef(ref) ? (ref.value as any) : ref
|
return isRef(ref) ? (ref.value as any) : ref
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ export {
|
|||||||
isReadonly,
|
isReadonly,
|
||||||
// advanced
|
// advanced
|
||||||
customRef,
|
customRef,
|
||||||
|
triggerRef,
|
||||||
shallowRef,
|
shallowRef,
|
||||||
shallowReactive,
|
shallowReactive,
|
||||||
shallowReadonly,
|
shallowReadonly,
|
||||||
|
Loading…
Reference in New Issue
Block a user