Skip to content

Commit f8bd932

Browse files
authored
refactor(computedInject): rename ctx param to oldValue (#4829)
1 parent 2f7e489 commit f8bd932

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/core/computedInject/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,24 @@ describe('computedInject', () => {
2323
expect(anotherComputedNum.value).toBe(11)
2424
})
2525
})
26+
27+
it('should pass oldValue to computed getter', () => {
28+
useInjectedSetup(() => {
29+
const curValue = shallowRef(0)
30+
const oldValue = shallowRef()
31+
32+
const computedNum = computedInject(Key, (source, previous) => {
33+
oldValue.value = previous
34+
return curValue.value + (source?.value ?? 0)
35+
})
36+
37+
expect(computedNum.value).toBe(1)
38+
expect(oldValue.value).toBeUndefined()
39+
40+
curValue.value = 1
41+
42+
expect(computedNum.value).toBe(2)
43+
expect(oldValue.value).toBe(1)
44+
})
45+
})
2646
})

packages/core/computedInject/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ComputedRef, InjectionKey } from 'vue'
22
import { computed, inject } from 'vue'
33

4-
export type ComputedInjectGetter<T, K> = (source: T | undefined, ctx?: any) => K
5-
export type ComputedInjectGetterWithDefault<T, K> = (source: T, ctx?: any) => K
4+
export type ComputedInjectGetter<T, K> = (source: T | undefined, oldValue?: K) => K
5+
export type ComputedInjectGetterWithDefault<T, K> = (source: T, oldValue?: K) => K
66
export type ComputedInjectSetter<T> = (v: T) => void
77

88
export interface WritableComputedInjectOptions<T, K> {
@@ -48,11 +48,11 @@ export function computedInject<T, K = any>(
4848
source = inject(key, defaultSource, treatDefaultAsFactory) as T
4949

5050
if (typeof options === 'function') {
51-
return computed(ctx => options(source, ctx))
51+
return computed(oldValue => options(source, oldValue as K | undefined))
5252
}
5353
else {
5454
return computed({
55-
get: ctx => options.get(source, ctx),
55+
get: oldValue => options.get(source, oldValue as K | undefined),
5656
set: options.set,
5757
})
5858
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy