@xstd/lock
v0.1.0
Published
Classes to manage locks on execution contexts
Readme
@xstd/lock
Classes to manage locks on execution contexts.
📦 Installation
yarn add @xstd/lock
# or
npm install @xstd/lock --save📜 Documentation
// resource.ts
import { Lockable } from '@xstd/lock';
class Resource {
readonly closeClock: Lockable = new Lockable();
close(): void {
this.closeClock.throwIfLocked();
// ... close the Resource
}
}// main.ts
import { Resource } from './resource';
const resource = new Resource();
// => currently, everyone may call `resource.close()`
// let's create a lock on `resource.close` to prevent this behavior
const lock = resource.closeClock.lock();
// => now, calling `resource.close()` will throw an error
// thanks to our lock, we may safely share `resource` and be sure that `resource.close()` will never be called
consume(resource);
// ... later ...
// to close the resource, we must run it inside the `lock.unlock` context
lock.unlock(() => resource.close());
// finally, we release the lock
lock.release();