}
}
}
+
+export function getDepFromReactive(object: any, key: string | number | symbol) {
+ return targetMap.get(object)?.get(key)
+}
import {
activeEffect,
+ getDepFromReactive,
shouldTrack,
trackEffects,
triggerEffects
export function triggerRefValue(ref: RefBase<any>, newVal?: any) {
ref = toRaw(ref)
- if (ref.dep) {
+ const dep = ref.dep
+ if (dep) {
if (__DEV__) {
- triggerEffects(ref.dep, {
+ triggerEffects(dep, {
target: ref,
type: TriggerOpTypes.SET,
key: 'value',
newValue: newVal
})
} else {
- triggerEffects(ref.dep)
+ triggerEffects(dep)
}
}
}
set value(newVal) {
this._object[this._key] = newVal
}
+
+ get dep(): Dep | undefined {
+ return getDepFromReactive(toRaw(this._object), this._key)
+ }
}
export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
triggerRef,
shallowRef,
Ref,
- effectScope
+ effectScope,
+ toRef
} from '@vue/reactivity'
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
expect(spy).toHaveBeenCalledTimes(1)
})
+ test('should force trigger on triggerRef with toRef from reactive', async () => {
+ const foo = reactive({ bar: 1 })
+ const bar = toRef(foo, 'bar')
+ const spy = jest.fn()
+
+ watchEffect(() => {
+ bar.value
+ spy()
+ })
+
+ expect(spy).toHaveBeenCalledTimes(1)
+
+ triggerRef(bar)
+
+ await nextTick()
+ // should trigger now
+ expect(spy).toHaveBeenCalledTimes(2)
+ })
+
// #2125
test('watchEffect should not recursively trigger itself', async () => {
const spy = vi.fn()