A set of react hooks that allows for a mutually exclusive access to a resource.
-
useMutex<
T
>(initialValue
):Mutex
<T
>useMutex
creates 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 initialValue
T
The initial value of the resource. -
useMutex<
T
,P
>(initializer
,params
):Mutex
<T
>useMutex
creates 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 functioninitializer
w ith parametersparams
. This will only be done to initialize the reference, when the component is first mounted.This version of
useMutex
is useful when the provided initial value is a large object, or its creation triggers some stateful behavior.Name Type Description initializer
( params
:P
) =>T
A function that produces the initial value. params
[ P
]The parameter passed to the initializer
function.
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 . |
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. |