Split the queue code out from data.js into a dedicated queue.js file (also split tests accordingly).

This commit is contained in:
John Resig 2009-12-06 17:19:33 -05:00
parent 87ba780c69
commit ebad701751
8 changed files with 230 additions and 242 deletions

View file

@ -6,6 +6,7 @@ DIST_DIR = ${PREFIX}/dist
BASE_FILES = ${SRC_DIR}/core.js\ BASE_FILES = ${SRC_DIR}/core.js\
${SRC_DIR}/data.js\ ${SRC_DIR}/data.js\
${SRC_DIR}/queue.js\
${SRC_DIR}/event.js\ ${SRC_DIR}/event.js\
${SRC_DIR}/support.js\ ${SRC_DIR}/support.js\
${SRC_DIR}/selector.js\ ${SRC_DIR}/selector.js\

View file

@ -1,5 +1,5 @@
# Basic Rakefile for building jQuery # Basic Rakefile for building jQuery
files = [ "intro", "core", "data", "event", "support", "selector", "traversing", "attributes", "manipulation", "css", "ajax", "fx", "offset", "dimensions", "outro" ] files = [ "intro", "core", "data", "queue", "event", "support", "selector", "traversing", "attributes", "manipulation", "css", "ajax", "fx", "offset", "dimensions", "outro" ]
date = `git log -1 | grep Date: | sed 's/[^:]*: *//'`.gsub(/\n/, "") date = `git log -1 | grep Date: | sed 's/[^:]*: *//'`.gsub(/\n/, "")
version = `cat version.txt`.gsub(/\n/, "") version = `cat version.txt`.gsub(/\n/, "")

View file

@ -38,6 +38,7 @@
<fileset dir="${SRC_DIR}" includes="intro.js" /> <fileset dir="${SRC_DIR}" includes="intro.js" />
<fileset dir="${SRC_DIR}" includes="core.js" /> <fileset dir="${SRC_DIR}" includes="core.js" />
<fileset dir="${SRC_DIR}" includes="data.js" /> <fileset dir="${SRC_DIR}" includes="data.js" />
<fileset dir="${SRC_DIR}" includes="queue.js" />
<fileset dir="${SRC_DIR}" includes="event.js" /> <fileset dir="${SRC_DIR}" includes="event.js" />
<fileset dir="${SRC_DIR}" includes="support.js" /> <fileset dir="${SRC_DIR}" includes="support.js" />
<fileset dir="${SRC_DIR}" includes="selector.js" /> <fileset dir="${SRC_DIR}" includes="selector.js" />

View file

@ -77,40 +77,6 @@ jQuery.extend({
// Completely remove the data cache // Completely remove the data cache
delete cache[ id ]; delete cache[ id ];
} }
},
queue: function( elem, type, data ) {
if ( !elem ) { return; }
type = (type || "fx") + "queue";
var q = jQuery.data( elem, type );
// Speed up dequeue by getting out quickly if this is just a lookup
if ( !data ) { return q || []; }
if ( !q || jQuery.isArray(data) ) {
q = jQuery.data( elem, type, jQuery.makeArray(data) );
} else {
q.push( data );
}
return q;
},
dequeue: function( elem, type ){
type = type || "fx";
var queue = jQuery.queue( elem, type ), fn = queue.shift();
// If the fx queue is dequeued, always remove the progress sentinel
if ( fn === "inprogress" ) { fn = queue.shift(); }
if ( fn ) {
// Add a progress sentinel to prevent the fx queue from being
// automatically dequeued
if ( type == "fx" ) { queue.unshift("inprogress"); }
fn.call(elem, function() { jQuery.dequeue(elem, type); });
}
} }
}); });
@ -143,45 +109,5 @@ jQuery.fn.extend({
return this.each(function(){ return this.each(function(){
jQuery.removeData( this, key ); jQuery.removeData( this, key );
}); });
},
queue: function(type, data){
if ( typeof type !== "string" ) {
data = type;
type = "fx";
}
if ( data === undefined ) {
return jQuery.queue( this[0], type );
}
return this.each(function(i, elem){
var queue = jQuery.queue( this, type, data );
if ( type == "fx" && queue[0] !== "inprogress" ) {
jQuery.dequeue( this, type );
}
});
},
dequeue: function(type){
return this.each(function(){
jQuery.dequeue( this, type );
});
},
// Based off of the plugin by Clint Helfers, with permission.
// http://blindsignals.com/index.php/2009/07/jquery-delay/
delay: function( time, type ) {
time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
type = type || "fx";
return this.queue( type, function() {
var elem = this;
setTimeout(function() {
jQuery.dequeue( elem, type );
}, time );
});
},
clearQueue: function(type){
return this.queue( type || "fx", [] );
} }
}); });

78
src/queue.js Normal file
View file

@ -0,0 +1,78 @@
jQuery.extend({
queue: function( elem, type, data ) {
if ( !elem ) { return; }
type = (type || "fx") + "queue";
var q = jQuery.data( elem, type );
// Speed up dequeue by getting out quickly if this is just a lookup
if ( !data ) { return q || []; }
if ( !q || jQuery.isArray(data) ) {
q = jQuery.data( elem, type, jQuery.makeArray(data) );
} else {
q.push( data );
}
return q;
},
dequeue: function( elem, type ){
type = type || "fx";
var queue = jQuery.queue( elem, type ), fn = queue.shift();
// If the fx queue is dequeued, always remove the progress sentinel
if ( fn === "inprogress" ) { fn = queue.shift(); }
if ( fn ) {
// Add a progress sentinel to prevent the fx queue from being
// automatically dequeued
if ( type == "fx" ) { queue.unshift("inprogress"); }
fn.call(elem, function() { jQuery.dequeue(elem, type); });
}
}
});
jQuery.fn.extend({
queue: function(type, data){
if ( typeof type !== "string" ) {
data = type;
type = "fx";
}
if ( data === undefined ) {
return jQuery.queue( this[0], type );
}
return this.each(function(i, elem){
var queue = jQuery.queue( this, type, data );
if ( type == "fx" && queue[0] !== "inprogress" ) {
jQuery.dequeue( this, type );
}
});
},
dequeue: function(type){
return this.each(function(){
jQuery.dequeue( this, type );
});
},
// Based off of the plugin by Clint Helfers, with permission.
// http://blindsignals.com/index.php/2009/07/jquery-delay/
delay: function( time, type ) {
time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
type = type || "fx";
return this.queue( type, function() {
var elem = this;
setTimeout(function() {
jQuery.dequeue( elem, type );
}, time );
});
},
clearQueue: function(type){
return this.queue( type || "fx", [] );
}
});

View file

@ -12,6 +12,7 @@
<script type="text/javascript" src="data/testrunner.js"></script> <script type="text/javascript" src="data/testrunner.js"></script>
<script type="text/javascript" src="unit/core.js"></script> <script type="text/javascript" src="unit/core.js"></script>
<script type="text/javascript" src="unit/data.js"></script> <script type="text/javascript" src="unit/data.js"></script>
<script type="text/javascript" src="unit/queue.js"></script>
<script type="text/javascript" src="unit/attributes.js"></script> <script type="text/javascript" src="unit/attributes.js"></script>
<script type="text/javascript" src="unit/css.js"></script> <script type="text/javascript" src="unit/css.js"></script>
<script type="text/javascript" src="unit/traversing.js"></script> <script type="text/javascript" src="unit/traversing.js"></script>

View file

@ -142,170 +142,3 @@ test(".removeData()", function() {
div.removeData("test.foo"); div.removeData("test.foo");
equals( div.data("test.foo"), undefined, "Make sure data is intact" ); equals( div.data("test.foo"), undefined, "Make sure data is intact" );
}); });
test("queue() defaults to 'fx' type", function () {
expect(1);
stop();
var counter = 0;
var $foo = jQuery("#foo");
$foo.queue(function() {
var self = this;
setTimeout(function() {
jQuery(self).dequeue("fx");
start();
}, 200);
}).queue(function() {
ok( "dequeuing 'fx' calls queues created with no name" )
});
});
test("queue() with other types",function() {
expect(9);
var counter = 0;
var $div = jQuery({});
$div
.queue('foo',function(){
equals( ++counter, 1, "Dequeuing" );
jQuery.dequeue(this,'foo');
})
.queue('foo',function(){
equals( ++counter, 2, "Dequeuing" );
jQuery(this).dequeue('foo');
})
.queue('foo',function(){
equals( ++counter, 3, "Dequeuing" );
})
.queue('foo',function(){
equals( ++counter, 4, "Dequeuing" );
});
equals( $div.queue('foo').length, 4, "Testing queue length" );
$div.dequeue('foo');
equals( counter, 3, "Testing previous call to dequeue" );
equals( $div.queue('foo').length, 1, "Testing queue length" );
$div.dequeue('foo');
equals( counter, 4, "Testing previous call to dequeue" );
equals( $div.queue('foo').length, 0, "Testing queue length" );
});
test("queue(name) passes in the next item in the queue as a parameter", function() {
expect(2);
var div = jQuery({});
var counter = 0;
div.queue("foo", function(next) {
equals(++counter, 1, "Dequeueing");
next();
}).queue("foo", function(next) {
equals(++counter, 2, "Next was called");
next();
}).queue("bar", function() {
equals(++counter, 3, "Other queues are not triggered by next()")
});
div.dequeue("foo");
});
test("queue(name) passes in the next item in the queue as a parameter", function() {
expect(2);
var div = jQuery({});
var counter = 0;
div.queue("foo", function(next) {
equals(++counter, 1, "Dequeueing");
next();
}).queue("foo", function(next) {
equals(++counter, 2, "Next was called");
next();
}).queue("bar", function() {
equals(++counter, 3, "Other queues are not triggered by next()")
});
div.dequeue("foo");
});
test("queue() passes in the next item in the queue as a parameter to fx queues", function() {
expect(2);
stop();
var div = jQuery({});
var counter = 0;
div.queue(function(next) {
equals(++counter, 1, "Dequeueing");
var self = this;
setTimeout(function() { next() }, 500);
}).queue(function(next) {
equals(++counter, 2, "Next was called");
next();
start();
}).queue("bar", function() {
equals(++counter, 3, "Other queues are not triggered by next()")
});
});
test("delay()", function() {
expect(2);
stop();
var foo = jQuery({}), run = 0;
foo.delay(100).queue(function(){
run = 1;
ok( true, "The function was dequeued." );
start();
});
equals( run, 0, "The delay delayed the next function from running." );
});
test("clearQueue(name) clears the queue", function() {
expect(1);
var div = jQuery({});
var counter = 0;
div.queue("foo", function(next) {
counter++;
jQuery(this).clearQueue("foo");
next();
}).queue("foo", function(next) {
counter++;
});
div.dequeue("foo");
equals(counter, 1, "the queue was cleared");
});
test("clearQueue() clears the fx queue", function() {
expect(1);
var div = jQuery({});
var counter = 0;
div.queue(function(next) {
counter++;
var self = this;
setTimeout(function() { jQuery(self).clearQueue(); next(); }, 50);
}).queue(function(next) {
counter++;
});
equals(counter, 1, "the queue was cleared");
div.removeData();
});

148
test/unit/queue.js Normal file
View file

@ -0,0 +1,148 @@
module("queue");
test("queue() with other types",function() {
expect(9);
var counter = 0;
var $div = jQuery({});
$div
.queue('foo',function(){
equals( ++counter, 1, "Dequeuing" );
jQuery.dequeue(this,'foo');
})
.queue('foo',function(){
equals( ++counter, 2, "Dequeuing" );
jQuery(this).dequeue('foo');
})
.queue('foo',function(){
equals( ++counter, 3, "Dequeuing" );
})
.queue('foo',function(){
equals( ++counter, 4, "Dequeuing" );
});
equals( $div.queue('foo').length, 4, "Testing queue length" );
$div.dequeue('foo');
equals( counter, 3, "Testing previous call to dequeue" );
equals( $div.queue('foo').length, 1, "Testing queue length" );
$div.dequeue('foo');
equals( counter, 4, "Testing previous call to dequeue" );
equals( $div.queue('foo').length, 0, "Testing queue length" );
});
test("queue(name) passes in the next item in the queue as a parameter", function() {
expect(2);
var div = jQuery({});
var counter = 0;
div.queue("foo", function(next) {
equals(++counter, 1, "Dequeueing");
next();
}).queue("foo", function(next) {
equals(++counter, 2, "Next was called");
next();
}).queue("bar", function() {
equals(++counter, 3, "Other queues are not triggered by next()")
});
div.dequeue("foo");
});
test("queue(name) passes in the next item in the queue as a parameter", function() {
expect(2);
var div = jQuery({});
var counter = 0;
div.queue("foo", function(next) {
equals(++counter, 1, "Dequeueing");
next();
}).queue("foo", function(next) {
equals(++counter, 2, "Next was called");
next();
}).queue("bar", function() {
equals(++counter, 3, "Other queues are not triggered by next()")
});
div.dequeue("foo");
});
test("queue() passes in the next item in the queue as a parameter to fx queues", function() {
expect(2);
stop();
var div = jQuery({});
var counter = 0;
div.queue(function(next) {
equals(++counter, 1, "Dequeueing");
var self = this;
setTimeout(function() { next() }, 500);
}).queue(function(next) {
equals(++counter, 2, "Next was called");
next();
start();
}).queue("bar", function() {
equals(++counter, 3, "Other queues are not triggered by next()")
});
});
test("delay()", function() {
expect(2);
stop();
var foo = jQuery({}), run = 0;
foo.delay(100).queue(function(){
run = 1;
ok( true, "The function was dequeued." );
start();
});
equals( run, 0, "The delay delayed the next function from running." );
});
test("clearQueue(name) clears the queue", function() {
expect(1);
var div = jQuery({});
var counter = 0;
div.queue("foo", function(next) {
counter++;
jQuery(this).clearQueue("foo");
next();
}).queue("foo", function(next) {
counter++;
});
div.dequeue("foo");
equals(counter, 1, "the queue was cleared");
});
test("clearQueue() clears the fx queue", function() {
expect(1);
var div = jQuery({});
var counter = 0;
div.queue(function(next) {
counter++;
var self = this;
setTimeout(function() { jQuery(self).clearQueue(); next(); }, 50);
}).queue(function(next) {
counter++;
});
equals(counter, 1, "the queue was cleared");
div.removeData();
});