Skip to content

Commit 3812f94

Browse files
gnarfdmethvin
authored andcommitted
Fix #12278. Promises on non-default queue wait until a dequeue is attempted on an empty queue. Close jquerygh-893.
1 parent 6b9fde1 commit 3812f94

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/queue.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jQuery.extend({
2222
type = type || "fx";
2323

2424
var queue = jQuery.queue( elem, type ),
25+
startLength = queue.length,
2526
fn = queue.shift(),
2627
hooks = jQuery._queueHooks( elem, type ),
2728
next = function() {
@@ -31,6 +32,7 @@ jQuery.extend({
3132
// If the fx queue is dequeued, always remove the progress sentinel
3233
if ( fn === "inprogress" ) {
3334
fn = queue.shift();
35+
startLength--;
3436
}
3537

3638
if ( fn ) {
@@ -45,7 +47,8 @@ jQuery.extend({
4547
delete hooks.stop;
4648
fn.call( elem, next, hooks );
4749
}
48-
if ( !queue.length && hooks ) {
50+
51+
if ( !startLength && hooks ) {
4952
hooks.empty.fire();
5053
}
5154
},
@@ -131,7 +134,8 @@ jQuery.fn.extend({
131134
type = type || "fx";
132135

133136
while( i-- ) {
134-
if ( (tmp = jQuery._data( elements[ i ], type + "queueHooks" )) && tmp.empty ) {
137+
tmp = jQuery._data( elements[ i ], type + "queueHooks" );
138+
if ( tmp && tmp.empty ) {
135139
count++;
136140
tmp.empty.add( resolve );
137141
}

test/unit/queue.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module( "queue", { teardown: moduleTeardown });
22

3-
test( "queue() with other types", 12, function() {
3+
test( "queue() with other types", 14, function() {
44
var counter = 0;
55

66
stop();
@@ -45,6 +45,12 @@ test( "queue() with other types", 12, function() {
4545

4646
equal( counter, 4, "Testing previous call to dequeue" );
4747
equal( $div.queue("foo").length, 0, "Testing queue length" );
48+
49+
$div.dequeue("foo");
50+
51+
equal( counter, 4, "Testing previous call to dequeue" );
52+
equal( $div.queue("foo").length, 0, "Testing queue length" );
53+
4854
});
4955

5056
test("queue(name) passes in the next item in the queue as a parameter", function() {
@@ -206,8 +212,8 @@ asyncTest( "fn.promise( \"queue\" ) - called whenever last queue function is deq
206212
}).queue( "queue", function( next ) {
207213
strictEqual( test++, 2, "step two" );
208214
setTimeout( function() {
209-
strictEqual( test++, 4, "step four" );
210215
next();
216+
strictEqual( test++, 4, "step four" );
211217
start();
212218
}, 10 );
213219
}).promise( "queue" ).done( function() {
@@ -217,6 +223,27 @@ asyncTest( "fn.promise( \"queue\" ) - called whenever last queue function is deq
217223
foo.dequeue( "queue" );
218224
});
219225

226+
asyncTest( "fn.promise( \"queue\" ) - waits for animation to complete before resolving", 2, function() {
227+
var foo = jQuery( "#foo" ),
228+
test = 1;
229+
230+
foo.animate({
231+
top: 100
232+
}, {
233+
duration: 1,
234+
queue: "queue",
235+
complete: function() {
236+
strictEqual( test++, 1, "step one" );
237+
}
238+
}).dequeue( "queue" );
239+
240+
foo.promise( "queue" ).done( function() {
241+
strictEqual( test++, 2, "step two" );
242+
start();
243+
});
244+
245+
});
246+
220247
test( ".promise(obj)", function() {
221248
expect(2);
222249

0 commit comments

Comments
 (0)