File tree Expand file tree Collapse file tree 2 files changed +14
-19
lines changed Expand file tree Collapse file tree 2 files changed +14
-19
lines changed Original file line number Diff line number Diff line change @@ -13,29 +13,23 @@ var p = Batcher.prototype
13
13
14
14
/**
15
15
* Push a job into the job queue.
16
- * Jobs with duplicate IDs will be skipped, however we can
17
- * use the `override` option to override existing jobs .
16
+ * Jobs with duplicate IDs will be skipped unless it's
17
+ * pushed when the queue is being flushed .
18
18
*
19
19
* @param {Object } job
20
20
* properties:
21
21
* - {String|Number} id
22
- * - {Boolean} override
23
22
* - {Function} run
24
23
*/
25
24
26
25
p . push = function ( job ) {
27
- if ( ! job . id || ! this . has [ job . id ] ) {
26
+ if ( ! job . id || ! this . has [ job . id ] || this . flushing ) {
28
27
this . queue . push ( job )
29
28
this . has [ job . id ] = job
30
29
if ( ! this . waiting ) {
31
30
this . waiting = true
32
31
_ . nextTick ( this . flush , this )
33
32
}
34
- } else if ( job . override ) {
35
- var oldJob = this . has [ job . id ]
36
- oldJob . cancelled = true
37
- this . queue . push ( job )
38
- this . has [ job . id ] = job
39
33
}
40
34
}
41
35
@@ -45,6 +39,7 @@ p.push = function (job) {
45
39
*/
46
40
47
41
p . flush = function ( ) {
42
+ this . flushing = true
48
43
// do not cache length because more jobs might be pushed
49
44
// as we run existing jobs
50
45
for ( var i = 0 ; i < this . queue . length ; i ++ ) {
@@ -64,6 +59,7 @@ p.reset = function () {
64
59
this . has = { }
65
60
this . queue = [ ]
66
61
this . waiting = false
62
+ this . flushing = false
67
63
}
68
64
69
65
module . exports = Batcher
Original file line number Diff line number Diff line change @@ -35,20 +35,19 @@ describe('Batcher', function () {
35
35
} )
36
36
} )
37
37
38
- it ( 'override' , function ( done ) {
39
- var spy2 = jasmine . createSpy ( 'batcher' )
38
+ it ( 'allow diplicate when flushing' , function ( done ) {
40
39
batcher . push ( {
41
40
id : 1 ,
42
- run : spy
43
- } )
44
- batcher . push ( {
45
- id : 1 ,
46
- run : spy2 ,
47
- override : true
41
+ run : function ( ) {
42
+ spy ( )
43
+ batcher . push ( {
44
+ id : 1 ,
45
+ run : spy
46
+ } )
47
+ }
48
48
} )
49
49
nextTick ( function ( ) {
50
- expect ( spy ) . not . toHaveBeenCalled ( )
51
- expect ( spy2 . calls . count ( ) ) . toBe ( 1 )
50
+ expect ( spy . calls . count ( ) ) . toBe ( 2 )
52
51
done ( )
53
52
} )
54
53
} )
You can’t perform that action at this time.
0 commit comments