From ebad701751689b773afe29d1e3c4532c7810d02a Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 6 Dec 2009 17:19:33 -0500 Subject: [PATCH] Split the queue code out from data.js into a dedicated queue.js file (also split tests accordingly). --- Makefile | 1 + Rakefile | 2 +- build.xml | 1 + src/data.js | 74 -------------------- src/queue.js | 78 +++++++++++++++++++++ test/index.html | 1 + test/unit/data.js | 167 --------------------------------------------- test/unit/queue.js | 148 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 230 insertions(+), 242 deletions(-) create mode 100644 src/queue.js create mode 100644 test/unit/queue.js diff --git a/Makefile b/Makefile index ac14d7ac6..a57b3c0dd 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ DIST_DIR = ${PREFIX}/dist BASE_FILES = ${SRC_DIR}/core.js\ ${SRC_DIR}/data.js\ + ${SRC_DIR}/queue.js\ ${SRC_DIR}/event.js\ ${SRC_DIR}/support.js\ ${SRC_DIR}/selector.js\ diff --git a/Rakefile b/Rakefile index af3171426..54fa3e8aa 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ # 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/, "") version = `cat version.txt`.gsub(/\n/, "") diff --git a/build.xml b/build.xml index 5235347b4..8efd387c3 100644 --- a/build.xml +++ b/build.xml @@ -38,6 +38,7 @@ + diff --git a/src/data.js b/src/data.js index 3e6eb19f6..1d993fe58 100644 --- a/src/data.js +++ b/src/data.js @@ -77,40 +77,6 @@ jQuery.extend({ // Completely remove the data cache 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(){ 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", [] ); } }); diff --git a/src/queue.js b/src/queue.js new file mode 100644 index 000000000..e636ffb41 --- /dev/null +++ b/src/queue.js @@ -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", [] ); + } +}); diff --git a/test/index.html b/test/index.html index 5c18f4f6a..6d34672d3 100644 --- a/test/index.html +++ b/test/index.html @@ -12,6 +12,7 @@ + diff --git a/test/unit/data.js b/test/unit/data.js index d18377d59..3916534c1 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -142,170 +142,3 @@ test(".removeData()", function() { div.removeData("test.foo"); 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(); -}); diff --git a/test/unit/queue.js b/test/unit/queue.js new file mode 100644 index 000000000..79b753c11 --- /dev/null +++ b/test/unit/queue.js @@ -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(); +}); -- 2.39.5