Created
May 28, 2011 16:57
-
-
Save topliceanu/997031 to your computer and use it in GitHub Desktop.
posix barrier-like functionality for events
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
// 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