0.1.2 • Published 10 months ago

react-mutex v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

useMutex

A set of react hooks that allows for a mutually exclusive access to a resource.

Documentation

Hooks

  • useMutex<T>(initialValue): Mutex<T>

    useMutex creates a reference containing some data of type T, and then returns a mutex that controls access to said resource. Its initial value is provided by the parameter initialValue, and will only be used to initialize the reference, when the component is first mounted.

    NameTypeDescription
    initialValueTThe initial value of the resource.
  • useMutex<T, P>(initializer, params): Mutex<T>

    useMutex creates a reference containing some data of type T, and then returns a mutex that controls access to said resource. Its initial value is computed by calling the user-supplied function initializer w ith parameters params. 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.

    NameTypeDescription
    initializer(params: P) => TA function that produces the initial value.
    paramsPThe parameter passed to the initializer function.

Types

Mutex<T>

A mutex protecting a resource from simultaneous access.

NameTypeDescription
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() => booleanIf 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.

NameTypeDescription
isReleased() => boolean
release() => voidReleases the resource, and allows other agents to acquire the mutex. Throws MutexAccessRevokedError iff this access has already been released.
get() => TReturns the value stored in the mutex. Throws MutexAccessRevokedError iff this access has already been released.
set(value: T) => voidMutates the value stored in the mutex. Throws MutexAccessRevokedError iff this access has already been released.
0.1.2

10 months ago

0.1.0

10 months ago