Last active
December 30, 2015 11:19
-
-
Save xcambar/7821789 to your computer and use it in GitHub Desktop.
A simple lock for when you ned to perform an action after a set of unrelated conditions are met
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
createLock = (locks, openDoorFn)-> | |
_locks = [].concat locks | |
(lock)-> | |
index = _locks.indexOf lock | |
openDoorFn() if !!~index and _locks.splice(index, 1).length and !_locks.length | |
unlock = createLock ['init', 'require'], -> alert("Unlocked") | |
unlock('abc') # ignored | |
unlock('bcd') # ignored | |
unlock('init') # Unlocks the 'init' lock | |
unlock('cde') # ignored | |
unlock('def') # ignored | |
unlock('require') # unlocks the 'require' lock, opens the door (ie, runs the callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simpler than https://github.com/bredele/doors/blob/master/index.js :)