File tree 3 files changed +41
-1
lines changed
3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { ReactiveFlags, toRaw } from './reactive'
5
5
6
6
export interface ComputedRef < T = any > extends WritableComputedRef < T > {
7
7
readonly value : T
8
+ defer ?: ( fn : ( ) => void ) => void
8
9
}
9
10
10
11
export interface WritableComputedRef < T > extends Ref < T > {
@@ -19,6 +20,16 @@ export interface WritableComputedOptions<T> {
19
20
set : ComputedSetter < T >
20
21
}
21
22
23
+ type ComputedScheduler = ( fn : ( ) => void ) => void
24
+ let scheduler : ComputedScheduler | undefined
25
+
26
+ /**
27
+ * Set a scheduler for deferring computed computations
28
+ */
29
+ export const setComputedScheduler = ( s : ComputedScheduler | undefined ) => {
30
+ scheduler = s
31
+ }
32
+
22
33
class ComputedRefImpl < T > {
23
34
public dep ?: Set < ReactiveEffect > = undefined
24
35
@@ -35,10 +46,34 @@ class ComputedRefImpl<T> {
35
46
private readonly _setter : ComputedSetter < T > ,
36
47
isReadonly : boolean
37
48
) {
49
+ let deferFn : ( ) => void
50
+ let scheduled = false
38
51
this . effect = new ReactiveEffect ( getter , ( ) => {
39
52
if ( ! this . _dirty ) {
40
53
this . _dirty = true
41
- triggerRefValue ( this )
54
+ if ( scheduler ) {
55
+ if ( ! scheduled ) {
56
+ scheduled = true
57
+ scheduler (
58
+ deferFn ||
59
+ ( deferFn = ( ) => {
60
+ scheduled = false
61
+ if ( this . _dirty ) {
62
+ this . _dirty = false
63
+ const newValue = this . effect . run ( ) !
64
+ if ( this . _value !== newValue ) {
65
+ this . _value = newValue
66
+ triggerRefValue ( this )
67
+ }
68
+ } else {
69
+ triggerRefValue ( this )
70
+ }
71
+ } )
72
+ )
73
+ }
74
+ } else {
75
+ triggerRefValue ( this )
76
+ }
42
77
}
43
78
} )
44
79
this [ ReactiveFlags . IS_READONLY ] = isReadonly
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ export {
30
30
} from './reactive'
31
31
export {
32
32
computed ,
33
+ setComputedScheduler ,
33
34
ComputedRef ,
34
35
WritableComputedRef ,
35
36
WritableComputedOptions ,
Original file line number Diff line number Diff line change @@ -2,6 +2,10 @@ import { ErrorCodes, callWithErrorHandling } from './errorHandling'
2
2
import { isArray } from '@vue/shared'
3
3
import { ComponentInternalInstance , getComponentName } from './component'
4
4
import { warn } from './warning'
5
+ import { setComputedScheduler } from '@vue/reactivity'
6
+
7
+ // set scheduler for computed
8
+ setComputedScheduler ( queueJob )
5
9
6
10
export interface SchedulerJob extends Function {
7
11
id ?: number
You can’t perform that action at this time.
0 commit comments