@@ -10,8 +10,8 @@ var config = require('./config')
10
10
var queue = [ ]
11
11
var userQueue = [ ]
12
12
var has = { }
13
+ var circular = { }
13
14
var waiting = false
14
- var flushing = false
15
15
var internalQueueDepleted = false
16
16
17
17
/**
@@ -22,15 +22,15 @@ function reset () {
22
22
queue = [ ]
23
23
userQueue = [ ]
24
24
has = { }
25
- waiting = flushing = internalQueueDepleted = false
25
+ circular = { }
26
+ waiting = internalQueueDepleted = false
26
27
}
27
28
28
29
/**
29
30
* Flush both queues and run the jobs.
30
31
*/
31
32
32
33
function flush ( ) {
33
- flushing = true
34
34
run ( queue )
35
35
internalQueueDepleted = true
36
36
run ( userQueue )
@@ -47,7 +47,20 @@ function run (queue) {
47
47
// do not cache length because more jobs might be pushed
48
48
// as we run existing jobs
49
49
for ( var i = 0 ; i < queue . length ; i ++ ) {
50
- queue [ i ] . run ( )
50
+ var job = queue [ i ]
51
+ var id = job . id
52
+ has [ id ] = null
53
+ job . run ( )
54
+ if ( process . env . NODE_ENV !== 'production' && has [ id ] != null ) {
55
+ circular [ id ] = ( circular [ id ] || 0 ) + 1
56
+ if ( circular [ id ] > config . _maxUpdateCount ) {
57
+ queue . splice ( has [ id ] , 1 )
58
+ _ . warn (
59
+ 'You may have an infinite update loop for watcher ' +
60
+ 'with expression: ' + job . expression
61
+ )
62
+ }
63
+ }
51
64
}
52
65
}
53
66
@@ -64,29 +77,14 @@ function run (queue) {
64
77
65
78
exports . push = function ( job ) {
66
79
var id = job . id
67
- if ( ! id || ! has [ id ] || flushing ) {
68
- if ( ! has [ id ] ) {
69
- has [ id ] = 1
70
- } else {
71
- has [ id ] ++
72
- // detect possible infinite update loops
73
- if ( has [ id ] > config . _maxUpdateCount ) {
74
- process . env . NODE_ENV !== 'production' && _ . warn (
75
- 'You may have an infinite update loop for the ' +
76
- 'watcher with expression: "' + job . expression + '".'
77
- )
78
- return
79
- }
80
- }
81
- // A user watcher callback could trigger another
82
- // directive update during the flushing; at that time
83
- // the directive queue would already have been run, so
84
- // we call that update immediately as it is pushed.
85
- if ( flushing && ! job . user && internalQueueDepleted ) {
80
+ if ( has [ id ] == null ) {
81
+ if ( internalQueueDepleted && ! job . user ) {
86
82
job . run ( )
87
83
return
88
84
}
89
- ; ( job . user ? userQueue : queue ) . push ( job )
85
+ var q = job . user ? userQueue : queue
86
+ has [ id ] = q . length
87
+ q . push ( job )
90
88
if ( ! waiting ) {
91
89
waiting = true
92
90
_ . nextTick ( flush )
0 commit comments