Skip to content

Instantly share code, notes, and snippets.

@topliceanu
Created May 28, 2011 16:57
Show Gist options
  • Save topliceanu/997031 to your computer and use it in GitHub Desktop.
Save topliceanu/997031 to your computer and use it in GitHub Desktop.
posix barrier-like functionality for events
// js barrier, waits for all events to be fired before executing the callback
// dependencies:
// 1. jquery 1.4.2+ - jquery.com
// 2. ben alman's tiny jquery pub/sub - https://gist.github.com/661855
(function($){
$.barrier = function( evs , callback ){
var events = evs.split(' ') ;
var flags = {} ;
var collector = {} ;
$.each( events , function( i, ev ){
flags[ev] = false ;
}) ;
$.each( events , function( i, ev ){
$.subscribe( ev, function( data ){
collector[ev] = data ;
flags[ev] = true ;
var finnish = true ;
$.each( flags, function( f ){
finnish = ( flags[f] !== false );
}) ;
if( finnish === true ){
callback( collector ) ;
}
}) ;
}) ;
} ;
})(jQuery) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment