Skip to content

Instantly share code, notes, and snippets.

@xcambar
Last active December 30, 2015 11:19
Show Gist options
  • Save xcambar/7821789 to your computer and use it in GitHub Desktop.
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
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)
@floriancargoet
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment