File tree Expand file tree Collapse file tree 2 files changed +37
-12
lines changed
test/unit/modules/observer Expand file tree Collapse file tree 2 files changed +37
-12
lines changed Original file line number Diff line number Diff line change @@ -39,13 +39,8 @@ function resetSchedulerState () {
39
39
*/
40
40
function flushSchedulerQueue ( ) {
41
41
flushing = true
42
- runSchedulerQueue ( queue . sort ( queueSorter ) )
43
42
runSchedulerQueue ( userQueue )
44
- // user watchers triggered more watchers,
45
- // keep flushing until it depletes
46
- if ( queue . length ) {
47
- return flushSchedulerQueue ( )
48
- }
43
+ runSchedulerQueue ( queue . sort ( queueSorter ) )
49
44
// devtool hook
50
45
/* istanbul ignore if */
51
46
if ( devtools && config . devtools ) {
@@ -103,11 +98,16 @@ function runSchedulerQueue (queue: Array<Watcher>) {
103
98
export function queueWatcher ( watcher : Watcher ) {
104
99
const id = watcher . id
105
100
if ( has [ id ] == null ) {
101
+ // if already flushing, and all user watchers have already been run,
102
+ // run the new user watcher immediately.
103
+ if ( flushing && watcher . user && ! userQueue . length ) {
104
+ return watcher . run ( )
105
+ }
106
106
// push watcher into appropriate queue
107
+ has [ id ] = true
107
108
const q = watcher . user
108
109
? userQueue
109
110
: queue
110
- has [ id ] = true
111
111
if ( ! flushing ) {
112
112
q . push ( watcher )
113
113
} else {
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ describe('Scheduler', () => {
45
45
} ) . then ( done )
46
46
} )
47
47
48
- it ( 'calls user watchers after directive updates ' , done => {
48
+ it ( 'call user watchers before component re-render ' , done => {
49
49
const vals = [ ]
50
50
function run ( ) {
51
51
vals . push ( this . id )
@@ -67,9 +67,34 @@ describe('Scheduler', () => {
67
67
run : run
68
68
} )
69
69
waitForUpdate ( ( ) => {
70
- expect ( vals [ 0 ] ) . toBe ( 1 )
71
- expect ( vals [ 1 ] ) . toBe ( 2 )
72
- expect ( vals [ 2 ] ) . toBe ( 3 )
70
+ expect ( vals ) . toEqual ( [ 2 , 1 , 3 ] )
71
+ } ) . then ( done )
72
+ } )
73
+
74
+ it ( 'call user watcher triggered by component re-render immediately' , done => {
75
+ // this happens when a component re-render updates the props of a child
76
+ const vals = [ ]
77
+ queueWatcher ( {
78
+ id : 1 ,
79
+ run ( ) {
80
+ vals . push ( 1 )
81
+ queueWatcher ( {
82
+ id : 3 ,
83
+ user : true ,
84
+ run ( ) {
85
+ vals . push ( 3 )
86
+ }
87
+ } )
88
+ }
89
+ } )
90
+ queueWatcher ( {
91
+ id : 2 ,
92
+ run ( ) {
93
+ vals . push ( 2 )
94
+ }
95
+ } )
96
+ waitForUpdate ( ( ) => {
97
+ expect ( vals ) . toEqual ( [ 1 , 3 , 2 ] )
73
98
} ) . then ( done )
74
99
} )
75
100
@@ -106,7 +131,7 @@ describe('Scheduler', () => {
106
131
}
107
132
} )
108
133
waitForUpdate ( ( ) => {
109
- expect ( callOrder . join ( ) ) . toBe ( '1,2,3' )
134
+ expect ( callOrder ) . toEqual ( [ 1 , 2 , 3 ] )
110
135
} ) . then ( done )
111
136
} )
112
137
} )
You can’t perform that action at this time.
0 commit comments