Skip to content

Commit e044b6e

Browse files
feat(reactivity): store value cache on CustomRefs impls (#11539)
Co-authored-by: Evan You <[email protected]>
1 parent 5753a10 commit e044b6e

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/reactivity/__tests__/ref.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,38 @@ describe('reactivity/ref', () => {
478478
expect(toValue(c)).toBe(3)
479479
expect(toValue(d)).toBe(4)
480480
})
481+
482+
test('ref w/ customRef w/ getterRef w/ objectRef should store value cache', () => {
483+
const refValue = ref(1)
484+
// @ts-expect-error private field
485+
expect(refValue._value).toBe(1)
486+
487+
let customRefValueCache = 0
488+
const customRefValue = customRef((track, trigger) => {
489+
return {
490+
get() {
491+
track()
492+
return customRefValueCache
493+
},
494+
set(value: number) {
495+
customRefValueCache = value
496+
trigger()
497+
},
498+
}
499+
})
500+
customRefValue.value
501+
502+
// @ts-expect-error internal field
503+
expect(customRefValue._value).toBe(0)
504+
505+
const getterRefValue = toRef(() => 1)
506+
getterRefValue.value
507+
// @ts-expect-error internal field
508+
expect(getterRefValue._value).toBe(1)
509+
510+
const objectRefValue = toRef({ value: 1 }, 'value')
511+
objectRefValue.value
512+
// @ts-expect-error internal field
513+
expect(objectRefValue._value).toBe(1)
514+
})
481515
})

packages/reactivity/src/ref.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ class CustomRefImpl<T> {
287287

288288
public readonly [ReactiveFlags.IS_REF] = true
289289

290+
public _value: T = undefined!
291+
290292
constructor(factory: CustomRefFactory<T>) {
291293
const dep = (this.dep = new Dep())
292294
const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep))
@@ -295,7 +297,7 @@ class CustomRefImpl<T> {
295297
}
296298

297299
get value() {
298-
return this._get()
300+
return (this._value = this._get())
299301
}
300302

301303
set value(newVal) {
@@ -339,6 +341,7 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
339341

340342
class ObjectRefImpl<T extends object, K extends keyof T> {
341343
public readonly [ReactiveFlags.IS_REF] = true
344+
public _value: T[K] = undefined!
342345

343346
constructor(
344347
private readonly _object: T,
@@ -348,7 +351,7 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
348351

349352
get value() {
350353
const val = this._object[this._key]
351-
return val === undefined ? this._defaultValue! : val
354+
return (this._value = val === undefined ? this._defaultValue! : val)
352355
}
353356

354357
set value(newVal) {
@@ -363,9 +366,11 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
363366
class GetterRefImpl<T> {
364367
public readonly [ReactiveFlags.IS_REF] = true
365368
public readonly [ReactiveFlags.IS_READONLY] = true
369+
public _value: T = undefined!
370+
366371
constructor(private readonly _getter: () => T) {}
367372
get value() {
368-
return this._getter()
373+
return (this._value = this._getter())
369374
}
370375
}
371376

0 commit comments

Comments
 (0)