@@ -7,32 +7,39 @@ import {
7
7
getVueInternalClasses ,
8
8
} from '../utils'
9
9
10
- interface Option < T > {
11
- get : ( ) => T
12
- set : ( value : T ) => void
13
- }
14
-
15
10
export interface ComputedRef < T = any > extends WritableComputedRef < T > {
16
11
readonly value : T
17
12
}
18
13
19
14
export interface WritableComputedRef < T > extends Ref < T > { }
20
15
16
+ export type ComputedGetter < T > = ( ctx ?: any ) => T
17
+ export type ComputedSetter < T > = ( v : T ) => void
18
+
19
+ export interface WritableComputedOptions < T > {
20
+ get : ComputedGetter < T >
21
+ set : ComputedSetter < T >
22
+ }
23
+
21
24
// read-only
22
- export function computed < T > ( getter : Option < T > [ 'get' ] ) : ComputedRef < T >
25
+ export function computed < T > ( getter : ComputedGetter < T > ) : ComputedRef < T >
23
26
// writable
24
- export function computed < T > ( options : Option < T > ) : WritableComputedRef < T >
27
+ export function computed < T > (
28
+ options : WritableComputedOptions < T >
29
+ ) : WritableComputedRef < T >
25
30
// implement
26
31
export function computed < T > (
27
- options : Option < T > [ 'get' ] | Option < T >
32
+ getterOrOptions : ComputedGetter < T > | WritableComputedOptions < T >
28
33
) : ComputedRef < T > | WritableComputedRef < T > {
29
34
const vm = getCurrentInstance ( ) ?. proxy
30
- let get : Option < T > [ 'get' ] , set : Option < T > [ 'set' ] | undefined
31
- if ( typeof options === 'function' ) {
32
- get = options
35
+ let getter : ComputedGetter < T >
36
+ let setter : ComputedSetter < T > | undefined
37
+
38
+ if ( typeof getterOrOptions === 'function' ) {
39
+ getter = getterOrOptions
33
40
} else {
34
- get = options . get
35
- set = options . set
41
+ getter = getterOrOptions . get
42
+ setter = getterOrOptions . set
36
43
}
37
44
38
45
let computedSetter
@@ -43,7 +50,7 @@ export function computed<T>(
43
50
let watcher : any
44
51
computedGetter = ( ) => {
45
52
if ( ! watcher ) {
46
- watcher = new Watcher ( vm , get , noopFn , { lazy : true } )
53
+ watcher = new Watcher ( vm , getter , noopFn , { lazy : true } )
47
54
}
48
55
if ( watcher . dirty ) {
49
56
watcher . evaluate ( )
@@ -55,22 +62,22 @@ export function computed<T>(
55
62
}
56
63
57
64
computedSetter = ( v : T ) => {
58
- if ( __DEV__ && ! set ) {
65
+ if ( __DEV__ && ! setter ) {
59
66
warn ( 'Write operation failed: computed value is readonly.' , vm ! )
60
67
return
61
68
}
62
69
63
- if ( set ) {
64
- set ( v )
70
+ if ( setter ) {
71
+ setter ( v )
65
72
}
66
73
}
67
74
} else {
68
75
// fallback
69
76
const computedHost = defineComponentInstance ( getVueConstructor ( ) , {
70
77
computed : {
71
78
$$state : {
72
- get,
73
- set,
79
+ get : getter ,
80
+ set : setter ,
74
81
} ,
75
82
} ,
76
83
} )
@@ -79,7 +86,7 @@ export function computed<T>(
79
86
80
87
computedGetter = ( ) => ( computedHost as any ) . $$state
81
88
computedSetter = ( v : T ) => {
82
- if ( __DEV__ && ! set ) {
89
+ if ( __DEV__ && ! setter ) {
83
90
warn ( 'Write operation failed: computed value is readonly.' , vm ! )
84
91
return
85
92
}
@@ -93,6 +100,6 @@ export function computed<T>(
93
100
get : computedGetter ,
94
101
set : computedSetter ,
95
102
} ,
96
- ! set
103
+ ! setter
97
104
)
98
105
}
0 commit comments