react-mutex v0.1.2
useMutex
A set of react hooks that allows for a mutually exclusive access to a resource.
Documentation
Hooks
useMutex<
T>(initialValue):Mutex<T>useMutexcreates a reference containing some data of typeT, and then returns a mutex that controls access to said resource. Its initial value is provided by the parameterinitialValue, and will only be used to initialize the reference, when the component is first mounted.Name Type Description initialValueTThe initial value of the resource.
useMutex<
T,P>(initializer,params):Mutex<T>useMutexcreates a reference containing some data of typeT, and then returns a mutex that controls access to said resource. Its initial value is computed by calling the user-supplied functioninitializerw ith parametersparams. This will only be done to initialize the reference, when the component is first mounted.This version of
useMutexis useful when the provided initial value is a large object, or its creation triggers some stateful behavior.Name Type Description initializer( params:P) =>TA function that produces the initial value. paramsPThe parameter passed to the initializerfunction.
Types
Mutex<T>
A mutex protecting a resource from simultaneous access.
| Name | Type | Description |
|---|---|---|
acquire | () => MutexResource<T> | Lock the mutex, and acquire exclusive read/write access to the underlying resource. If the mutex is already locked, throws MutexLockedError. |
isAvailable | () => boolean | If the mutex is already locked, throws MutexLockedError. |
MutexResource<T>
An access to the resource protected by a mutex. Must be released once the user has finished accessing the resource.
| Name | Type | Description |
|---|---|---|
isReleased | () => boolean | |
release | () => void | Releases the resource, and allows other agents to acquire the mutex. Throws MutexAccessRevokedError iff this access has already been released. |
get | () => T | Returns the value stored in the mutex. Throws MutexAccessRevokedError iff this access has already been released. |
set | (value: T) => void | Mutates the value stored in the mutex. Throws MutexAccessRevokedError iff this access has already been released. |