Locker.ts 456 B

123456789101112131415161718
  1. export class Locker<T extends string> {
  2. private locks = new Map<T, true>();
  3. lock = (lockType: T) => {
  4. this.locks.set(lockType, true);
  5. };
  6. /** @returns whether no locks remaining */
  7. unlock = (lockType: T) => {
  8. this.locks.delete(lockType);
  9. return !this.isLocked();
  10. };
  11. /** @returns whether some (or specific) locks are present */
  12. isLocked(lockType?: T) {
  13. return lockType ? this.locks.has(lockType) : !!this.locks.size;
  14. }
  15. }