-
-
Save tbranyen/1017588 to your computer and use it in GitHub Desktop.
This file contains 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
// Initially zero. When decremented to zero, fires callback | |
function semaphore( callback ) { | |
var fn = callback, | |
count = 0; | |
return { | |
increment: function() { | |
count++; | |
}, | |
decrement: function() { | |
if( count > 0 ) { | |
if(--count == 0 && fn) { | |
fn(); | |
fn = null; | |
} | |
} else { | |
throw new Error("Semaphore decremented more than incremented!"); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment