Created
June 15, 2012 05:31
-
-
Save brianswisher/2934846 to your computer and use it in GitHub Desktop.
Queue pattern in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var util = (function(){ | |
var Queue = function Queue(){ | |
var methods = [], | |
flushed = false, | |
add = function( fn ){ | |
if( flushed ) { | |
fn( response ); | |
} else { | |
methods.push( fn ); | |
} | |
}, | |
flush = function(){ | |
if( flushed ) { | |
return; | |
} | |
flushed = true; | |
while ( methods[0] ) { | |
methods.shift().apply( this, arguments ); | |
} | |
}, | |
step = function(){ | |
var fn = methods.shift(); | |
if ( fn ) { fn.apply( this, arguments ); } | |
}; | |
Queue.prototype.add = add; | |
Queue.prototype.flush = flush; | |
Queue.prototype.step = step; | |
}, | |
queue = new Queue(); | |
return { | |
queue: function( q ){ | |
if ( q != null ){ | |
if ( typeof( q ) === 'function' ) { | |
queue.add( q ); | |
} else { | |
queue.flush(); | |
} | |
} else { | |
queue.step(); | |
} | |
return this; | |
} | |
}; | |
}( function( target, node ) { return target.appendChild( node ); }, | |
function( tag ) { return document.createElement( tag ); } )); | |
util | |
.queue( function(){ document.body.innerHTML = 'One<br>' } ) | |
.queue( function(){ document.body.innerHTML += 'Two<br>' } ) | |
.queue( function(){ document.body.innerHTML += 'Three<br>' } ) | |
.queue( true ); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment