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 | ||||
| } 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) | ||||
|   }) | ||||
| 
 | ||||
|  | ||||
| @ -6,6 +6,7 @@ export { | ||||
|   toRef, | ||||
|   toRefs, | ||||
|   customRef, | ||||
|   triggerRef, | ||||
|   Ref, | ||||
|   UnwrapRef | ||||
| } from './ref' | ||||
|  | ||||
| @ -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<T>(ref: T): T extends Ref<infer V> ? V : T { | ||||
|   return isRef(ref) ? (ref.value as any) : ref | ||||
| } | ||||
|  | ||||
| @ -16,6 +16,7 @@ export { | ||||
|   isReadonly, | ||||
|   // advanced
 | ||||
|   customRef, | ||||
|   triggerRef, | ||||
|   shallowRef, | ||||
|   shallowReactive, | ||||
|   shallowReadonly, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user