Add lock by key
gajus opened this issue · 3 comments
mutex library has a way to lock by key
https://www.npmjs.com/package/mutex#mutexlockstring-key-object-options-function-callback--lock
It is not clear how to implement equivalent strategy using this library.
Ended up developing https://github.com/gajus/mutual-exclusion
That's not a use case I have been using this package for so far, but you could do the same thing by keeping a map of locks, creating a new one if there is no existing lock, and removing it after release if ::isLocked()
returns false.
I want to keep the actual Mutex
and Semaphore
classes as simple as possible, but I can add wrapper (similar to withTimeout
) that hides this functionality behind the same interface, augmented with a key argument.
Well, my particular use case has been addressed by mutual-exclusion
library, but maybe others will find it useful.