File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed
packages/core/computedInject Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -23,4 +23,24 @@ describe('computedInject', () => {
23
23
expect ( anotherComputedNum . value ) . toBe ( 11 )
24
24
} )
25
25
} )
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
+ } )
26
46
} )
Original file line number Diff line number Diff line change 1
1
import type { ComputedRef , InjectionKey } from 'vue'
2
2
import { computed , inject } from 'vue'
3
3
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
6
6
export type ComputedInjectSetter < T > = ( v : T ) => void
7
7
8
8
export interface WritableComputedInjectOptions < T , K > {
@@ -48,11 +48,11 @@ export function computedInject<T, K = any>(
48
48
source = inject ( key , defaultSource , treatDefaultAsFactory ) as T
49
49
50
50
if ( typeof options === 'function' ) {
51
- return computed ( ctx => options ( source , ctx ) )
51
+ return computed ( oldValue => options ( source , oldValue as K | undefined ) )
52
52
}
53
53
else {
54
54
return computed ( {
55
- get : ctx => options . get ( source , ctx ) ,
55
+ get : oldValue => options . get ( source , oldValue as K | undefined ) ,
56
56
set : options . set ,
57
57
} )
58
58
}
You can’t perform that action at this time.
0 commit comments